-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdutils.go
56 lines (41 loc) · 1.23 KB
/
cmdutils.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
// SPDX-License-Identifier: Apache-2.0
package cmdutils
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/loopholelabs/logging/types"
"github.com/loopholelabs/cmdutils/pkg/config"
"github.com/loopholelabs/cmdutils/pkg/printer"
)
// Helper is passed to every single command and is used by individual
// subcommands.
type Helper[T config.Config] struct {
// Config contains globally sourced configuration
Config T
// Printer is used to print output of a command to stdout.
Printer *printer.Printer
// Logger is used to provide structured logging for a command.
Logger types.RootLogger
// debug defines the debug mode
debug *bool
}
func (h *Helper[T]) SetDebug(debug *bool) {
h.debug = debug
}
func (h *Helper[T]) Debug() bool { return *h.debug }
// RequiredArgs - required arguments are not available.
func RequiredArgs(reqArgs ...string) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
n := len(reqArgs)
if len(args) >= n {
return nil
}
missing := reqArgs[len(args):]
a := fmt.Sprintf("arguments <%s>", strings.Join(missing, ", "))
if len(missing) == 1 {
a = fmt.Sprintf("argument <%s>", missing[0])
}
return fmt.Errorf("missing %s \n\n%s", a, cmd.UsageString())
}
}