-
Notifications
You must be signed in to change notification settings - Fork 5
/
html5_plugin.go
106 lines (92 loc) · 2.56 KB
/
html5_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
98
99
100
101
102
103
104
105
106
// HTML5 Applications Repository CLI Plugin is a plugin for Cloud Foundry CLI tool
// that aims to provide easy command line access to APIs exposed by HTML5 Application
package main
import (
"fmt"
"io"
defaultlog "log"
"os"
"strconv"
"strings"
"cf-html5-apps-repo-cli-plugin/commands"
"cf-html5-apps-repo-cli-plugin/log"
"cf-html5-apps-repo-cli-plugin/ui"
"github.com/cloudfoundry/cli/plugin"
)
// Version is the version of the CLI plugin.
var Version = "1.4.9"
// HTML5Plugin represents a cf CLI plugin for working with HTML5 Application Repository service
type HTML5Plugin struct{}
// Commands contains the commands supported by this plugin
var Commands = []commands.Command{
&commands.ListCommand{},
&commands.GetCommand{},
&commands.PushCommand{},
&commands.DeleteCommand{},
&commands.InfoCommand{},
}
// Run runs this plugin
func (p *HTML5Plugin) 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)
}
log.Tracef("Running CloudFoundry html5-plugin %s\n", Version)
err = command.Initialize(command.GetPluginCommand().Name, cliConnection)
if err != nil {
ui.Failed(err.Error())
os.Exit(1)
}
status := command.Execute(args[1:])
if status == commands.Failure {
os.Exit(1)
} else {
command.Dispose(command.GetPluginCommand().Name)
}
}
// GetMetadata returns the metadata of this plugin
func (p *HTML5Plugin) GetMetadata() plugin.PluginMetadata {
metadata := plugin.PluginMetadata{
Name: "html5-plugin",
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(HTML5Plugin))
}
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 '%s'", 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,
}
}