-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.go
50 lines (44 loc) · 885 Bytes
/
base.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 command
import (
"io"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Opt has options for command
type Opt struct {
Name string
Args []string
Version string
Out io.Writer
Err io.Writer
}
// NewBaseCmd initializes the root command
func NewBaseCmd(opt *Opt) *cobra.Command {
cmd := &cobra.Command{
Use: opt.Name,
Short: "building terraform-registry in local",
SilenceErrors: true,
SilenceUsage: true,
}
cmd.AddCommand(
NewVersionCmd(opt.Version),
NewServerCmd(),
)
cobra.OnInitialize(initConfig)
viper.BindPFlags(cmd.PersistentFlags())
cmd.SetOut(opt.Out)
cmd.SetErr(opt.Err)
return cmd
}
func initConfig() {
viper.AutomaticEnv()
}
// Execute executes this command
func Execute(opt *Opt) int {
cmd := NewBaseCmd(opt)
if err := cmd.Execute(); err != nil {
cmd.PrintErr(err)
return 1
}
return 0
}