@@ -16,22 +16,17 @@ func addCmd() *cobra.Command {
1616 Short : "Add a software repository" ,
1717 DisableFlagsInUseLine : true ,
1818 RunE : func (cmd * cobra.Command , args []string ) error {
19- if err := cobra . ExactArgs ( 4 )( cmd , args ); err != nil {
20- PrintHelp ( cmd )
21- return err
19+ addArgs , err := parseAddCommandArgs ( cmd , args )
20+ if err != nil {
21+ return fmt . Errorf ( "unable to parse command arguments: %w" , err )
2222 }
2323
24- repoName := args [0 ]
25- repoUrl := args [1 ]
26- rootVersionArg := args [2 ]
27- rootSha512 := args [3 ]
28-
29- if repoName == trdl .SelfUpdateDefaultRepo {
24+ if addArgs .repoName == trdl .SelfUpdateDefaultRepo {
3025 PrintHelp (cmd )
3126 return fmt .Errorf ("reserved repository name %q cannot be used" , trdl .SelfUpdateDefaultRepo )
3227 }
3328
34- rootVersion , err := parseRootVersionArgument (rootVersionArg )
29+ rootVersion , err := parseRootVersionArgument (addArgs . rootVersionArg )
3530 if err != nil {
3631 PrintHelp (cmd )
3732 return fmt .Errorf ("unable to parse required argument \" ROOT_VERSION\" : %w" , err )
@@ -42,7 +37,7 @@ func addCmd() *cobra.Command {
4237 return fmt .Errorf ("unable to initialize trdl client: %w" , err )
4338 }
4439
45- if err := c .AddRepo (repoName , repoUrl , rootVersion , rootSha512 ); err != nil {
40+ if err := c .AddRepo (addArgs . repoName , addArgs . repoUrl , rootVersion , addArgs . rootSha512 ); err != nil {
4641 return err
4742 }
4843
@@ -53,6 +48,47 @@ func addCmd() *cobra.Command {
5348 return cmd
5449}
5550
51+ type addCommandArgs struct {
52+ repoName string
53+ repoUrl string
54+ rootVersionArg string
55+ rootSha512 string
56+ }
57+
58+ func parseAddCommandArgs (cmd * cobra.Command , args []string ) (* addCommandArgs , error ) {
59+ if len (args ) != 2 && len (args ) != 4 {
60+ PrintHelp (cmd )
61+ return nil , fmt .Errorf ("expected 2 or 4 arguments: REPO URL [ROOT_VERSION ROOT_SHA512], got %d" , len (args ))
62+ }
63+
64+ repoName := args [0 ]
65+ repoUrl := args [1 ]
66+
67+ var rootVersionArg , rootSha512 string
68+
69+ if len (args ) == 4 {
70+ rootVersionArg = args [2 ]
71+ rootSha512 = args [3 ]
72+
73+ if ! govalidator .IsNumeric (rootVersionArg ) {
74+ PrintHelp (cmd )
75+ return nil , fmt .Errorf ("ROOT_VERSION must be numeric: %q" , rootVersionArg )
76+ }
77+
78+ if rootSha512 == "" {
79+ PrintHelp (cmd )
80+ return nil , fmt .Errorf ("ROOT_SHA512 must not be empty" )
81+ }
82+ }
83+
84+ return & addCommandArgs {
85+ repoName : repoName ,
86+ repoUrl : repoUrl ,
87+ rootVersionArg : rootVersionArg ,
88+ rootSha512 : rootSha512 ,
89+ }, nil
90+ }
91+
5692func parseRootVersionArgument (arg string ) (int64 , error ) {
5793 if arg == "" {
5894 return 0 , nil
0 commit comments