forked from alphagov/paas-cf-conduit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
96 lines (87 loc) · 1.9 KB
/
plugin.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
91
92
93
94
95
96
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"code.cloudfoundry.org/cli/plugin"
"github.com/alphagov/paas-cf-conduit/conduit"
)
type Plugin struct {
cmd *cobra.Command
}
func (p *Plugin) Run(conn plugin.CliConnection, args []string) {
// set defaults from plugin info
org, err := conn.GetCurrentOrg()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
p.cmd.PersistentFlags().Lookup("org").Value.Set(org.Name)
space, err := conn.GetCurrentSpace()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
p.cmd.PersistentFlags().Lookup("space").Value.Set(space.Name)
api, err := conn.ApiEndpoint()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
p.cmd.PersistentFlags().Lookup("endpoint").Value.Set(api)
token, err := conn.AccessToken()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
p.cmd.PersistentFlags().Lookup("token").Value.Set(token)
insecure, err := conn.IsSSLDisabled()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if insecure {
p.cmd.PersistentFlags().Lookup("insecure").Value.Set("true")
}
// parse
p.cmd.SetArgs(args)
exitCode := 1
if err := p.cmd.Execute(); err != nil {
if exitError, ok := err.(conduit.AppExecution); ok {
exitCode = exitError.ExitCode
}
os.Exit(exitCode)
}
os.Exit(0)
}
func (p *Plugin) GetMetadata() plugin.PluginMetadata {
meta := plugin.PluginMetadata{
Name: "conduit",
Version: plugin.VersionType{
Major: 0,
Minor: 1,
Build: 1,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 26,
Build: 0,
},
Commands: []plugin.Command{},
}
for _, cmd := range p.cmd.Commands() {
if cmd.Hidden {
continue
}
opts := map[string]string{}
meta.Commands = append(meta.Commands, plugin.Command{
Name: cmd.Name(),
HelpText: cmd.Long,
UsageDetails: plugin.Usage{
Usage: cmd.UsageString(),
Options: opts,
},
})
}
return meta
}