-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
61 lines (52 loc) · 1.01 KB
/
main.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
package main
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)
var rootCmd = &cobra.Command{
Use: "uppercase",
Short: "Transform the input to uppercase letters",
Long: `Simple demo of the usage of linux pipes
Transform the input (pipe of file) to uppercase letters`,
RunE: func(cmd *cobra.Command, args []string) error {
print = logNoop
if flags.verbose {
print = logOut
}
return runCommand()
},
}
var flags struct{
filepath string
verbose bool
}
var flagsName = struct{
file, fileShort string
verbose, verboseShort string
} {
"file", "f",
"verbose", "v",
}
var print func(s string)
func main() {
rootCmd.Flags().StringVarP(
&flags.filepath,
flagsName.file,
flagsName.fileShort,
"", "path to the file")
rootCmd.PersistentFlags().BoolVarP(
&flags.verbose,
flagsName.verbose,
flagsName.verboseShort,
false, "log verbose output")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func logNoop(s string) {}
func logOut(s string) {
log.Println(s)
}