-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (76 loc) · 1.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"code.cloudfoundry.org/cli/plugin"
"os/exec"
"os"
"strings"
)
type CFHTTPPlugin struct{}
func main() {
plugin.Start(new(CFHTTPPlugin))
}
func (c *CFHTTPPlugin) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] == "http" {
c.http(cliConnection, args[1:])
}
}
func (c *CFHTTPPlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "cfhttp",
Version: plugin.VersionType{
Major: 0,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "http",
HelpText: "Invokes HTTPie within cf target context",
UsageDetails: plugin.Usage{
Usage: "cf http /v3/apps",
},
},
},
}
}
func (c *CFHTTPPlugin) http(cliConnection plugin.CliConnection, args []string) {
cmdArgs := []string{"--ignore-stdin"}
verb := "get"
path := args[0]
httpArgs := args[1:]
skipSSLValidation, _ := cliConnection.IsSSLDisabled()
if skipSSLValidation {
cmdArgs = append(cmdArgs, "--verify=no")
}
if isVerb(path) {
if len(args) < 2 {
fmt.Println("TODO PRINT USAGE: missing path")
os.Exit(1)
}
verb = path
path = args[1]
httpArgs = args[2:]
}
endpoint, _ := cliConnection.ApiEndpoint()
token, _ := cliConnection.AccessToken()
cmdArgs = append(cmdArgs, verb)
cmdArgs = append(cmdArgs, fmt.Sprintf("%s%s", endpoint, path))
cmdArgs = append(cmdArgs, fmt.Sprintf("Authorization:%s", token))
cmdArgs = append(cmdArgs, httpArgs...)
cmd := exec.Command("http", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func isVerb(candidate string) bool {
return contains([]string{"get", "post", "patch", "put", "head", "delete"}, candidate)
}
func contains(list []string, search string) bool {
for _, item := range list {
if strings.EqualFold(item, search) {
return true
}
}
return false
}