Skip to content

Commit 4798aa2

Browse files
committed
added completion command
1 parent 96a4931 commit 4798aa2

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

cmd/gss/main.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var gitBranch string
3131
var gitCommit string
3232

3333
func main() {
34-
root := &cobra.Command{
34+
rootCommand := &cobra.Command{
3535
Use: "gss",
3636
Short: "gss",
3737
Long: `gss is a simple program for serializing/deserializing data.`,
@@ -84,7 +84,7 @@ func main() {
8484
return nil
8585
},
8686
}
87-
flags := root.Flags()
87+
flags := rootCommand.Flags()
8888
flags.StringP("input-format", "i", "", "The input format: "+strings.Join(gss.Formats, ", "))
8989
flags.StringSlice("input-header", []string{}, "The input header if the stdin input has no header.")
9090
flags.StringP("input-comment", "c", "", "The input comment character, e.g., #. Commented lines are not sent to output.")
@@ -98,6 +98,28 @@ func main() {
9898
flags.BoolP("async", "a", false, "async processing")
9999
flags.Bool("verbose", false, "Print debug info to stdout")
100100

101+
102+
completionCommandLong := ""
103+
if _, err := os.Stat("/etc/bash_completion.d/"); !os.IsNotExist(err) {
104+
completionCommandLong = "To install completion scripts run:\ngss completion > /etc/bash_completion.d/gss"
105+
} else {
106+
if _, err := os.Stat("/usr/local/etc/bash_completion.d/"); !os.IsNotExist(err) {
107+
completionCommandLong = "To install completion scripts run:\ngss completion > /usr/local/etc/bash_completion.d/gss"
108+
} else {
109+
completionCommandLong = "To install completion scripts run:\ngss completion > .../bash_completion.d/gss"
110+
}
111+
}
112+
113+
completionCommand := &cobra.Command{
114+
Use: "completion",
115+
Short: "Generates bash completion scripts",
116+
Long: completionCommandLong,
117+
RunE: func(cmd *cobra.Command, args []string) error {
118+
return rootCommand.GenBashCompletion(os.Stdout)
119+
},
120+
}
121+
rootCommand.AddCommand(completionCommand)
122+
101123
version := &cobra.Command{
102124
Use: "version",
103125
Short: "print version information to stdout",
@@ -116,9 +138,9 @@ func main() {
116138
},
117139
}
118140

119-
root.AddCommand(version)
141+
rootCommand.AddCommand(version)
120142

121-
if err := root.Execute(); err != nil {
143+
if err := rootCommand.Execute(); err != nil {
122144
fmt.Fprintf(os.Stderr, err.Error()+"\n")
123145
os.Exit(1)
124146
}

0 commit comments

Comments
 (0)