This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_info.go
executable file
·88 lines (72 loc) · 2.54 KB
/
print_info.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
package bite
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// if true then commands will not output info messages, like "Processor ___ created".
// Look the `PrintInfo` func for more, it's not a global flag but it's a common one, all commands that return info messages
// set that via command flag binding.
//
// Defaults to false.
const silentFlagKey = "silent"
// GetSilentFlag returns the value(true/false) of the `--silent` flag,
// however if not found it returns false too.
func GetSilentFlag(cmd *cobra.Command) bool {
b, _ := cmd.Flags().GetBool(silentFlagKey)
return b
}
// CanBeSilent registeres the `--silent` flag to the "cmd" command.
func CanBeSilent(cmd *cobra.Command) *cobra.Command {
cmd.Flags().Bool(silentFlagKey, false, "run in silent mode. No printing info messages for CRUD except errors, defaults to false")
return cmd
}
// HasFlag returns true if "flagName" can be found in the "cmd" cobra or its parents, otherwise false.
func HasFlag(cmd *cobra.Command, flagName string) (found bool) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Name == flagName {
found = true
}
})
if !found {
cmd.VisitParents(func(parentCmd *cobra.Command) {
if HasFlag(parentCmd, flagName) {
found = true
}
})
}
return
}
// HasSilentFlag returns true if the "cmd" command has registered the `--silent` flag.
func HasSilentFlag(cmd *cobra.Command) bool {
return HasFlag(cmd, silentFlagKey)
}
// ExpectsFeedback returns true if the "cmd" command's `--silent` flag is registered and it's true, or the `--output` is table.
func ExpectsFeedback(cmd *cobra.Command) bool {
output := GetOutPutFlag(cmd)
canPrintInfo := false
if (strings.ToUpper(output) == "TABLE") {
canPrintInfo = true
}
if HasSilentFlag(cmd) {
canPrintInfo = !GetSilentFlag(cmd)
}
return canPrintInfo
}
// PrintInfo prints an info message to the command's standard output.
// If the `--silent`` flag is a REGISTERED flag for that command, then it will check if it's false or not set in order to print, otherwise
// it will check the `--machine-friendly` flag, if true not print.
//
// Useful when you want to have --machine-friendly on but want to print an important info message to the user as well but user can also disable that message via
// a second flag, the --silent one.
func PrintInfo(cmd *cobra.Command, format string, a ...interface{}) error {
if !ExpectsFeedback(cmd) {
return nil
}
if !strings.HasSuffix(format, "\n") {
format += "\n" // add a new line.
}
_, err := fmt.Fprintf(cmd.OutOrStdout(), format, a...)
return err
}