-
Notifications
You must be signed in to change notification settings - Fork 63
/
args.go
91 lines (74 loc) · 1.84 KB
/
args.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
// SPDX-License-Identifier: MIT
// Copyright (c) 2019, 2019 GitHub Inc.
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the MIT License (the "License").
// You may not use this file expect in compliance with the License.
package cmdfactory
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func MinimumArgs(n int, msg string) cobra.PositionalArgs {
if msg == "" {
return cobra.MinimumNArgs(1)
}
return func(_ *cobra.Command, args []string) error {
if len(args) < n {
return FlagErrorf("%s", msg)
}
return nil
}
}
func ExactArgs(n int, msg string) cobra.PositionalArgs {
return func(_ *cobra.Command, args []string) error {
if len(args) > n {
return FlagErrorf("too many arguments")
}
if len(args) < n {
return FlagErrorf("%s", msg)
}
return nil
}
}
func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return nil
}
errMsg := fmt.Sprintf("unknown argument %q", args[0])
if len(args) > 1 {
errMsg = fmt.Sprintf("unknown arguments %q", args)
}
hasValueFlag := false
cmd.Flags().Visit(func(f *pflag.Flag) {
if f.Value.Type() != "bool" {
hasValueFlag = true
}
})
if hasValueFlag {
errMsg += "; please quote all values that have spaces"
}
return FlagErrorf("%s", errMsg)
}
func MaxDirArgs(n int) cobra.PositionalArgs {
return func(_ *cobra.Command, args []string) error {
if len(args) > n {
return FlagErrorf("expected no more than %d paths received %d", n, len(args))
// Treat no path as current working directory
} else if len(args) == 0 {
cwd, err := os.Getwd()
if err != nil {
return err
}
args = []string{cwd}
}
for _, path := range args {
f, err := os.Stat(path)
if err != nil || !f.IsDir() {
return FlagErrorf("path is not a valid directory: %s", path)
}
}
return nil
}
}