-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_user_cmd.go
More file actions
121 lines (98 loc) · 2.65 KB
/
Copy pathtest_user_cmd.go
File metadata and controls
121 lines (98 loc) · 2.65 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/cloudfoundry/cli/plugin"
"github.com/mitchellh/colorstring"
)
func main() {
plugin.Start(new(TestUser))
}
func (c *TestUser) Run(cliConnection plugin.CliConnection, args []string) {
if len(args) < 3 {
fmt.Println("Incorrect usage")
fmt.Println(c.GetMetadata().Commands[0].UsageDetails.Usage)
} else {
c.setProperties(args)
c.setCommands()
// so we can iterate on the map in the desired order
var keys []int
for k := range c.Command {
keys = append(keys, k)
}
sort.Ints(keys)
for _, key := range keys {
output, status := c.execCommand(cliConnection, c.Command[key])
c.printMessages(output, status)
if foundError(status) == true {
return
}
}
}
}
func (c *TestUser) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "TestUser",
Version: plugin.VersionType{
Major: 0,
Minor: 1,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "test-user",
HelpText: "Create a user and assign all possible permissions, organisation and space are created if they do not already exist as well. If no organisation or space name are specified then the default value of 'development' is used",
UsageDetails: plugin.Usage{
Usage: "cf test-user <username> <password> <optional org name> <optional space name>",
},
},
},
}
}
func (t TestUser) execCommand(cliConnection plugin.CliConnection, command CommandData) (output []string, status []int) {
output = append(output, command.Message)
response, err := cliConnection.CliCommandWithoutTerminalOutput(command.ExecutionArguments...)
if response != nil && strings.Contains(response[2], "already exists") {
status = append(status, 2)
} else if err != nil {
status = append(status, 0)
} else {
status = append(status, 1)
}
return
}
func (t *TestUser) printMessages(output []string, status []int) {
for i, v := range output {
switch status[i] {
case 0:
fmt.Println(colorstring.Color("[red]" + t.commandCounter() + v))
case 1:
fmt.Println(colorstring.Color("[green]" + t.commandCounter() + v))
case 2:
fmt.Println(colorstring.Color("[cyan]" + t.commandCounter() + v))
}
t.CmdRunCount++
}
}
func (t TestUser) commandCounter() (count string) {
current := strconv.Itoa(t.CmdRunCount)
total := strconv.Itoa(CmdTotalCount)
return "[" + current + "/" + total + "] "
}
func (c *TestUser) setProperties(args []string) {
c.UserName = args[1]
c.Password = args[2]
c.CmdRunCount = 1
if len(args) >= 4 {
c.OrgName = args[3]
} else {
c.OrgName = DefaultOrg
}
if len(args) >= 5 {
c.SpaceName = args[4]
} else {
c.SpaceName = DefaultSpace
}
}