-
Notifications
You must be signed in to change notification settings - Fork 41
/
multiapps_plugin.go
97 lines (85 loc) · 2.38 KB
/
multiapps_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
97
package main
import (
"fmt"
"io"
defaultlog "log"
"os"
"strconv"
"strings"
"code.cloudfoundry.org/cli/plugin"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/commands"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
)
// Version is the version of the CLI plugin. It is injected on linking time.
var Version string = "0.0.0"
// MultiappsPlugin represents a cf CLI plugin for executing operations on MTAs
type MultiappsPlugin struct{}
// Commands contains the commands supported by this plugin
var Commands = []commands.Command{
commands.NewDeployCommand(),
commands.NewBlueGreenDeployCommand(),
commands.NewMtasCommand(),
commands.NewDmolCommand(),
commands.NewUndeployCommand(),
commands.NewMtaCommand(),
commands.NewMtaOperationsCommand(),
commands.NewPurgeConfigCommand(),
}
// Run runs this plugin
func (p *MultiappsPlugin) Run(cliConnection plugin.CliConnection, args []string) {
disableStdOut()
if args[0] == "CLI-MESSAGE-UNINSTALL" {
return
}
command, err := findCommand(args[0])
if err != nil {
log.Fatalln(err)
}
command.Initialize(command.GetPluginCommand().Name, cliConnection)
status := command.Execute(args[1:])
if status == commands.Failure {
os.Exit(1)
}
}
// GetMetadata returns the metadata of this plugin
func (p *MultiappsPlugin) GetMetadata() plugin.PluginMetadata {
metadata := plugin.PluginMetadata{
Name: "multiapps",
Version: parseSemver(Version),
MinCliVersion: plugin.VersionType{Major: 6, Minor: 7, Build: 0},
}
for _, command := range Commands {
metadata.Commands = append(metadata.Commands, command.GetPluginCommand())
}
return metadata
}
func main() {
plugin.Start(new(MultiappsPlugin))
}
func disableStdOut() {
defaultlog.SetFlags(0)
defaultlog.SetOutput(io.Discard)
}
func findCommand(name string) (commands.Command, error) {
for _, command := range Commands {
pluginCommand := command.GetPluginCommand()
if pluginCommand.Name == name || pluginCommand.Alias == name {
return command, nil
}
}
return nil, fmt.Errorf("Could not find command with name %q", name)
}
func parseSemver(version string) plugin.VersionType {
mmb := strings.Split(version, ".")
if len(mmb) != 3 {
panic("invalid version: " + version)
}
major, _ := strconv.Atoi(mmb[0])
minor, _ := strconv.Atoi(mmb[1])
build, _ := strconv.Atoi(mmb[2])
return plugin.VersionType{
Major: major,
Minor: minor,
Build: build,
}
}