-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhelpers.go
50 lines (40 loc) · 1.52 KB
/
helpers.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
package util
import (
"fmt"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
Flag_File_Key = "file"
Flag_OutputDir_Key = "output-dir"
Flag_Distro_Key = "distro"
)
func UsageError(cmd *cobra.Command, format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
return fmt.Errorf("%s\nSee '%s -h' for help and examples.", msg, cmd.CommandPath())
}
func BindViperNames(v *viper.Viper, fs *flag.FlagSet, viperName string, cobraName string) {
// errors here are mistakes in the code and cobra will panic in similar conditions; let's not handle it differently here right now
flag := fs.Lookup(cobraName)
if flag == nil {
panic(fmt.Sprintf("Viper can't bind flag: %s", cobraName))
}
err := v.BindPFlag(viperName, flag)
if err != nil {
panic(err)
}
}
func BindViper(v *viper.Viper, fs *flag.FlagSet, name string) {
BindViperNames(v, fs, name, name)
}
func AddIOFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringSliceP(Flag_File_Key, "f", []string{}, "Specify alternative OpenCompose file(s)")
cmd.PersistentFlags().StringP(Flag_OutputDir_Key, "o", "./", "Specify output directory for genrated Kubernetes (and OpenShift) definitions")
cmd.PersistentFlags().StringP(Flag_Distro_Key, "d", "kubernetes", "Choose a target distribution")
}
func AddIOFlagsViper(v *viper.Viper, cmd *cobra.Command) {
BindViper(v, cmd.PersistentFlags(), Flag_File_Key)
BindViper(v, cmd.PersistentFlags(), Flag_OutputDir_Key)
BindViper(v, cmd.PersistentFlags(), Flag_Distro_Key)
}