15
15
package cmd
16
16
17
17
import (
18
+ "errors"
18
19
"fmt"
19
20
"log"
20
21
"os"
@@ -25,22 +26,27 @@ import (
25
26
"runecs.io/v1/pkg/ecs"
26
27
)
27
28
28
- var service string
29
+ const (
30
+ defaultLastNumberOfTasks = 50
31
+ defaultLastDays = 0
32
+ )
29
33
30
34
var rootCmd = & cobra.Command {
31
35
PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
32
36
commandsWithoutService := []string {"completion" , "help" , "list" , "version" }
37
+ serviceValue := cmd .Flag ("service" ).Value .String ()
33
38
34
39
serviceRequired := true
35
40
for _ , c := range commandsWithoutService {
36
41
if cmd .Name () == c {
37
42
serviceRequired = false
43
+
38
44
break
39
45
}
40
46
}
41
47
42
- if serviceRequired && service == "" {
43
- return fmt . Errorf ("--service flag is required for this command" )
48
+ if serviceRequired && serviceValue == "" {
49
+ return errors . New ("--service flag is required for this command" )
44
50
}
45
51
46
52
return nil
@@ -90,8 +96,8 @@ func init() {
90
96
}
91
97
92
98
pruneCmd .PersistentFlags ().BoolP ("dry-run" , "" , false , "dry run" )
93
- pruneCmd .PersistentFlags ().IntP ("keep-last" , "" , 50 , "keep last N task definitions" )
94
- pruneCmd .PersistentFlags ().IntP ("keep-days" , "" , 0 , "keep task definitions older than N days" )
99
+ pruneCmd .PersistentFlags ().IntP ("keep-last" , "" , defaultLastNumberOfTasks , "keep last N task definitions" )
100
+ pruneCmd .PersistentFlags ().IntP ("keep-days" , "" , defaultLastDays , "keep task definitions older than N days" )
95
101
rootCmd .AddCommand (pruneCmd )
96
102
97
103
////////////
@@ -104,7 +110,7 @@ func init() {
104
110
DisableFlagsInUseLine : true ,
105
111
PreRunE : func (cmd * cobra.Command , args []string ) error {
106
112
if dockerImageTag == "" {
107
- return fmt . Errorf ("--image-tag flag is required" )
113
+ return errors . New ("--image-tag flag is required" )
108
114
}
109
115
110
116
return nil
@@ -205,18 +211,19 @@ func init() {
205
211
rootCmd .AddCommand (completionCmd )
206
212
207
213
rootCmd .CompletionOptions .DisableDefaultCmd = true
208
- rootCmd .PersistentFlags ().StringVar ( & service , "service" , "" , "service name (cluster/service)" )
214
+ rootCmd .PersistentFlags ().String ( "service" , "" , "service name (cluster/service)" )
209
215
}
210
216
211
217
func initService () * ecs.Service {
212
218
svc := ecs.Service {}
219
+ serviceValue := rootCmd .Flag ("service" ).Value .String ()
213
220
214
- parsed := strings .Split (service , "/" )
221
+ parsed := strings .Split (serviceValue , "/" )
215
222
if len (parsed ) == 2 {
216
223
svc .Cluster = parsed [0 ]
217
224
svc .Service = parsed [1 ]
218
- } else if service != "" {
219
- log .Fatalf ("Invalid service name %s\n " , service )
225
+ } else if serviceValue != "" {
226
+ log .Fatalf ("Invalid service name %s\n " , serviceValue )
220
227
}
221
228
222
229
validate := validator .New ()
0 commit comments