-
Notifications
You must be signed in to change notification settings - Fork 12
/
add.go
62 lines (49 loc) · 1.42 KB
/
add.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
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/spf13/cobra"
trdlClient "github.com/werf/trdl/pkg/client"
"github.com/werf/trdl/pkg/trdl"
)
func addCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add REPO URL ROOT_VERSION ROOT_SHA512",
Short: "Add repository",
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(4)(cmd, args); err != nil {
PrintHelp(cmd)
return err
}
repoName := args[0]
repoUrl := args[1]
rootVersionArg := args[2]
rootSha512 := args[3]
if repoName == trdl.SelfUpdateDefaultRepo {
PrintHelp(cmd)
return fmt.Errorf("reserved repository name %q cannot be used", trdl.SelfUpdateDefaultRepo)
}
rootVersion, err := parseRootVersionArgument(rootVersionArg)
if err != nil {
PrintHelp(cmd)
return fmt.Errorf("unable to parse required argument \"ROOT_VERSION\": %s", err)
}
c, err := trdlClient.NewClient(homeDir)
if err != nil {
return fmt.Errorf("unable to initialize trdl client: %s", err)
}
if err := c.AddRepo(repoName, repoUrl, rootVersion, rootSha512); err != nil {
return err
}
return nil
},
}
return cmd
}
func parseRootVersionArgument(arg string) (int64, error) {
if !govalidator.IsNumeric(arg) {
return 0, fmt.Errorf("value (%q) must be an integer", arg)
}
return govalidator.ToInt(arg)
}