forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
92 lines (81 loc) · 2.04 KB
/
cmd.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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/spf13/cobra"
)
func RunFixedArgs(numArgs int, run func([]string) error) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
if len(args) != numArgs {
fmt.Printf("Expected %d arguments, got %d.\n\n", numArgs, len(args))
cmd.Usage()
} else {
if err := run(args); err != nil {
ErrorAndExit("%v", err)
}
}
}
}
func RunBoundedArgs(min int, max int, run func([]string) error) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
if len(args) < min || len(args) > max {
fmt.Printf("Expected %d to %d arguments, got %d.\n\n", min, max, len(args))
cmd.Usage()
} else {
if err := run(args); err != nil {
ErrorAndExit("%v", err)
}
}
}
}
// ErrorAndExit errors with the given format and args, and then exits.
func ErrorAndExit(format string, args ...interface{}) {
if errString := strings.TrimSpace(fmt.Sprintf(format, args...)); errString != "" {
fmt.Fprintf(os.Stderr, "%s\n", errString)
}
os.Exit(1)
}
// ParseCommits takes a slice of arguments of the form "repo/commit-id" or
// "repo" (in which case we consider the commit ID to be empty), and returns
// a list of Commits
func ParseCommits(args []string) ([]*pfs.Commit, error) {
var commits []*pfs.Commit
for _, arg := range args {
parts := strings.Split(arg, "/")
if len(parts) > 2 {
return nil, fmt.Errorf("Invalid argument: %s", arg)
}
commit := &pfs.Commit{
Repo: &pfs.Repo{
Name: parts[0],
},
}
if len(parts) == 2 {
commit.ID = parts[1]
} else {
commit.ID = ""
}
commits = append(commits, commit)
}
return commits, nil
}
type RepeatedStringArg []string
func (r *RepeatedStringArg) String() string {
result := "["
for i, s := range *r {
if i != 0 {
result += ", "
}
result += s
}
return result + "]"
}
func (r *RepeatedStringArg) Set(s string) error {
*r = append(*r, s)
return nil
}
func (r *RepeatedStringArg) Type() string {
return "[]string"
}