-
Notifications
You must be signed in to change notification settings - Fork 12
/
root.go
164 lines (136 loc) · 4.11 KB
/
root.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/drlogout/iredmail-cli/iredmail"
"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
const (
arrowRight = "➞"
printFlags = true
passwordMinLength = 10
)
var (
cfgFile string
prettyPrint = true
)
var rootCmd = &cobra.Command{
Use: "iredmail-cli",
Short: "A command line inteface to manage iRedMail server",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.my.cnf-vmailadmin)")
rootCmd.PersistentFlags().BoolVarP(&prettyPrint, "pretty", "", true, "")
rootCmd.PersistentFlags().MarkHidden("pretty")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
iredmail.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".iredmail-cli" (without extension).
iredmail.SetConfigFile(filepath.Join(home, ".my.cnf-vmailadmin"))
}
err := iredmail.ReadInConfig()
if err != nil {
log.Fatal("No config file found (default ~/.my.cnf-vmailadmin)")
}
}
func askForConfirmation() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
fatal("%v\n", err)
}
okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
nokayResponses := []string{"n", "N", "no", "No", "NO"}
if containsString(okayResponses, response) {
return true
} else if containsString(nokayResponses, response) {
return false
} else {
fmt.Println("Please type yes or no and then press enter:")
return askForConfirmation()
}
}
// posString returns the first index of element in slice.
// If slice does not contain element, returns -1.
func posString(slice []string, element string) int {
for index, elem := range slice {
if elem == element {
return index
}
}
return -1
}
// containsString returns true iff slice contains element
func containsString(slice []string, element string) bool {
return !(posString(slice, element) == -1)
}
func success(format string, a ...interface{}) {
c := color.New(color.FgGreen, color.Bold)
c.Printf(format, a...)
}
func warning(format string, a ...interface{}) {
c := color.New(color.FgRed, color.Bold)
c.Printf(format, a...)
}
func info(format string, a ...interface{}) {
c := color.New()
c.Printf(format, a...)
}
func fatal(format string, a ...interface{}) {
c := color.New(color.FgRed, color.Bold)
c.Printf(format, a...)
os.Exit(1)
}
func usageTemplate(useLine string, args ...bool) string {
var printFlags bool
var flags string
if len(args) > 0 {
printFlags = args[0]
}
if printFlags {
flags = " [flags]"
}
return `Usage:{{if .Runnable}}
iredmail-cli ` + useLine + flags + `{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}