-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcobra-replacements.go
97 lines (78 loc) · 2.49 KB
/
cobra-replacements.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
93
94
95
96
97
package pcli
import (
"io"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// HelpFunc is a drop in replacement for spf13/cobra `HelpFunc`
func HelpFunc() func(*cobra.Command, []string) {
return func(cmd *cobra.Command, strings []string) {
var ret string
ret += generateTitleString(rootCmd)
ret += generateUsageTemplate(cmd)
ret += generateDescriptionTemplate(cmd.Long)
ret += generateExamplesTemplate(cmd)
ret += generateCommandsTemplate(cmd.Commands())
ret += generateFlagsTemplate(cmd.Flags())
ret += "\n"
pterm.Print(ret)
}
}
// FlagErrorFunc is a drop in replacement for spf13/cobra `FlagErrorFunc`
func FlagErrorFunc() func(*cobra.Command, error) error {
return func(cmd *cobra.Command, err error) error {
var ret string
ret += generateTitleString(rootCmd)
ret += generateUsageTemplate(cmd)
ret += generateDescriptionTemplate(cmd.Long)
ret += generateCommandsTemplate(cmd.Commands())
ret += generateFlagsTemplate(cmd.Flags())
ret += "\n\n"
ret += pterm.Error.WithShowLineNumber(false).Sprintln(err)
pterm.Print(ret)
return nil
}
}
// GlobalNormalizationFunc is a drop in replacement for spf13/cobra `GlobalNormalizationFunc`
func GlobalNormalizationFunc() func(f *pflag.FlagSet, name string) pflag.NormalizedName {
return rootCmd.GlobalNormalizationFunc()
}
// HelpTemplate is a drop in replacement for spf13/cobra `HelpTemplate`
func HelpTemplate() string {
return ""
}
// UsageFunc is a drop in replacement for spf13/cobra `UsageFunc`
func UsageFunc() func(*cobra.Command) error {
return rootCmd.UsageFunc()
}
// UsageTemplate is a drop in replacement for spf13/cobra `UsageTemplate`
func UsageTemplate() string {
return ""
}
// VersionTemplate is a drop in replacement for spf13/cobra `VersionTemplate`
func VersionTemplate() string {
return pterm.Info.Sprintfln("%s is on version: %s", rootCmd.Name(), pterm.Magenta(rootCmd.Version))
}
// Err is a drop in replacement for spf13/cobra `Err`
func Err() io.Writer {
return errorWriter{}
}
type errorWriter struct{}
func (errorWriter) Write(p []byte) (n int, err error) {
pterm.Error.WithMessageStyle(pterm.NewStyle()).WithShowLineNumber(false).Println(string(p))
return len(p), nil
}
// PcliOut is a drop in replacement for spf13/cobra `SetOut`
func PcliOut() io.Writer {
return pcliOut{}
}
type pcliOut struct{}
func (pcliOut) Write(p []byte) (n int, err error) {
str := string(p)
if strings.Contains(str, " is on version: ") {
pterm.Print(str)
}
return len(p), nil
}