-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlaunch.go
160 lines (140 loc) · 3.4 KB
/
launch.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cli
import (
"errors"
"fmt"
"strings"
"github.com/urfave/cli/v2"
"github.com/avarabyeu/goRP/v5/gorp"
)
var (
launchCommand = &cli.Command{
Name: "launch",
Usage: "Operations over launches",
Subcommands: cli.Commands{listLaunchesCommand, mergeCommand},
}
listLaunchesCommand = &cli.Command{
Name: "list",
Usage: "List launches",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filter-name",
Aliases: []string{"fn"},
Usage: "Filter Name",
EnvVars: []string{"FILTER_NAME"},
},
&cli.StringSliceFlag{
Name: "filter",
Aliases: []string{"f"},
Usage: "Filter",
EnvVars: []string{"Filter"},
},
},
Action: listLaunches,
}
mergeCommand = &cli.Command{
Name: "merge",
Usage: "Merge Launches",
Action: mergeLaunches,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filter",
Aliases: []string{"f"},
Usage: "Launches Filter",
EnvVars: []string{"MERGE_LAUNCH_FILTER"},
},
&cli.StringFlag{
Name: "filter-name",
Aliases: []string{"fn"},
Usage: "Filter Name",
EnvVars: []string{"FILTER_NAME"},
},
&cli.IntSliceFlag{
Name: "ids",
Usage: "Launch IDS to Merge",
EnvVars: []string{"MERGE_LAUNCH_IDS"},
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "New Launch Name",
EnvVars: []string{"MERGE_LAUNCH_NAME"},
Required: true,
},
&cli.StringFlag{
Name: "t",
Aliases: []string{"type"},
Usage: "Merge Type",
EnvVars: []string{"MERGE_TYPE"},
Value: "DEEP",
},
},
}
)
func mergeLaunches(c *cli.Context) error {
rpClient, err := buildClient(c)
if err != nil {
return err
}
ids, err := getMergeIDs(c, rpClient)
if err != nil {
return err
}
rq := &gorp.MergeLaunchesRQ{
Name: c.String("name"),
MergeType: gorp.MergeType(c.String("type")),
Launches: ids,
}
launchResource, err := rpClient.MergeLaunches(rq)
if err != nil {
return fmt.Errorf("unable to merge launches: %w", err)
}
fmt.Println(launchResource.ID)
return nil
}
func listLaunches(c *cli.Context) error {
rpClient, err := buildClient(c)
if err != nil {
return err
}
var launches *gorp.LaunchPage
if filters := c.StringSlice("filter"); len(filters) > 0 {
filter := strings.Join(filters, "&")
launches, err = rpClient.GetLaunchesByFilterString(filter)
} else if filterName := c.String("filter-name"); filterName != "" {
launches, err = rpClient.GetLaunchesByFilterName(filterName)
} else {
launches, err = rpClient.GetLaunches()
}
if err != nil {
return err
}
for _, launch := range launches.Content {
fmt.Printf("%d #%d \"%s\"\n", launch.ID, launch.Number, launch.Name)
}
return nil
}
func getMergeIDs(c *cli.Context, rpClient *gorp.Client) ([]int, error) {
if ids := c.IntSlice("ids"); len(ids) > 0 {
return ids, nil
}
var launches *gorp.LaunchPage
var err error
filter := c.String("filter")
filterName := c.String("filter-name")
switch {
case filter != "":
launches, err = rpClient.GetLaunchesByFilterString(filter)
case filterName != "":
launches, err = rpClient.GetLaunchesByFilterName(filterName)
default:
return nil, errors.New("no either IDs or filter provided")
}
if err != nil {
return nil, fmt.Errorf("unable to find launches by filter: %s", err.Error())
}
ids := make([]int, len(launches.Content))
for i, l := range launches.Content {
ids[i] = l.ID
}
return ids, nil
}