-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (54 loc) · 1.76 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
62
63
64
65
66
67
68
69
70
71
/*
*/
package main
import (
"fmt"
"os"
"strings"
"github.com/senzing-garage/go-cmdhelping/cmdhelper"
"github.com/senzing-garage/go-cmdhelping/constant"
"github.com/senzing-garage/go-cmdhelping/option"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
Use string = "Name of command"
Short string = "Short description of command"
Long string = "Long description of command."
)
func Run(cmd *cobra.Command, args []string) {
_ = cmd
_ = args
fmt.Printf("--%-12s %s: %s\n", option.DatabaseURL.Arg, fmt.Sprintf(option.DatabaseURL.Help, option.DatabaseURL.Envar), viper.GetString(option.DatabaseURL.Arg))
fmt.Printf("--%-12s %s: %d\n", option.HTTPPort.Arg, fmt.Sprintf(option.HTTPPort.Help, option.HTTPPort.Envar), viper.GetInt(option.HTTPPort.Arg))
fmt.Printf("--%-12s %s: %s\n", option.LogLevel.Arg, fmt.Sprintf(option.LogLevel.Help, option.LogLevel.Envar), viper.GetString(option.LogLevel.Arg))
}
func main() {
// Define the command-line options / environment variables used for context variables.
var ContextVariables = []option.ContextVariable{
option.DatabaseURL,
option.HTTPPort,
option.LogLevel,
}
// Initialize viper.
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetEnvPrefix(constant.SetEnvPrefix)
// Initialize cobra.
var cobraCommand = &cobra.Command{
Use: Use,
Short: Short,
Long: Long,
Run: Run,
Version: cmdhelper.Version("1234", "5"),
}
// Used in local initialization.
cmdhelper.Init(cobraCommand, ContextVariables)
// Usually used in cobra.Command.PreRun.
cmdhelper.PreRun(cobraCommand, os.Args, Use, ContextVariables)
// Execute the cobra command which prints the output from "Run:" function.
err := cobraCommand.Execute()
if err != nil {
panic(err)
}
}