-
Notifications
You must be signed in to change notification settings - Fork 20
/
firewall_create.go
66 lines (55 loc) · 1.41 KB
/
firewall_create.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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var firewallCreateCmd = &cobra.Command{
Use: "create <name>+",
Short: "Create security group",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
desc, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
syncTasks := []task{}
for _, arg := range args {
syncTasks = append(syncTasks, task{
egoscale.CreateSecurityGroup{Name: arg, Description: desc},
fmt.Sprintf("Create security group %q", arg),
})
}
taskResponses := asyncTasks(syncTasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
description := (desc != "")
table := table.NewTable(os.Stdout)
if !description {
table.SetHeader([]string{"Name", "ID"})
} else {
table.SetHeader([]string{"Name", "Description", "ID"})
}
for _, resp := range taskResponses {
r := resp.resp.(*egoscale.SecurityGroup)
if description {
table.Append([]string{r.Name, r.ID.String()})
continue
}
table.Append([]string{r.Name, r.Description, r.ID.String()})
}
table.Render()
return nil
},
}
func init() {
firewallCreateCmd.Flags().StringP("description", "d", "", "Security group description")
firewallCmd.AddCommand(firewallCreateCmd)
}