-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathroot.go
More file actions
63 lines (51 loc) · 1.67 KB
/
root.go
File metadata and controls
63 lines (51 loc) · 1.67 KB
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
package cmd
import (
"os"
"path"
"text/tabwriter"
hclog "github.com/hashicorp/go-hclog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/privateerproj/privateer-sdk/command"
"github.com/privateerproj/privateer-sdk/config"
)
var (
buildVersion string
buildGitCommitHash string
buildTime string
logger hclog.Logger // enables formatted logging (logger.Trace, etc)
writer *tabwriter.Writer // enables bare line writing (for use in list & version)
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "privateer",
Short: "privateer root command",
PersistentPreRun: persistentPreRun,
}
)
// 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(version, commitHash, builtAt string) {
buildVersion = version
buildGitCommitHash = commitHash
buildTime = builtAt
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
command.SetBase(rootCmd)
rootCmd.PersistentFlags().StringP("binaries-path", "b", defaultBinariesPath(), "Path to the directory where plugins are installed")
_ = viper.BindPFlag("binaries-path", rootCmd.PersistentFlags().Lookup("binaries-path"))
}
func persistentPreRun(cmd *cobra.Command, args []string) {
cfg := config.NewConfig(nil)
logger = cfg.Logger
// writer is used for output in the list & version commands
writer = tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
command.ReadConfig()
}
func defaultBinariesPath() string {
home, _ := os.UserHomeDir()
return path.Join(home, ".privateer", "bin")
}