-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexecute.go
More file actions
82 lines (70 loc) · 2.7 KB
/
execute.go
File metadata and controls
82 lines (70 loc) · 2.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package cmd provides the command-line interface for pvtr.
// It defines the root command and all subcommands, handles configuration,
// and manages the execution flow of the application.
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 holds the version string set at build time.
buildVersion string
// buildGitCommitHash holds the git commit hash set at build time.
buildGitCommitHash string
// buildTime holds the build timestamp set at build time.
buildTime string
// logger enables formatted logging with methods like logger.Trace, logger.Info, etc.
logger hclog.Logger
// writer enables formatted tabular output for use in list and version commands.
writer *tabwriter.Writer
// rootCmd represents the base command when called without any subcommands.
// It is the entry point for all pvtr commands and handles global configuration.
rootCmd = &cobra.Command{
Use: "pvtr",
Short: "pvtr 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.
//
// Parameters:
// - version: The version string to display in version command
// - commitHash: The git commit hash to display in version command
// - builtAt: The build timestamp to display in version command
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"))
}
// persistentPreRun initializes the logger and writer for use by all commands.
// It is called before every command execution and sets up the configuration
// and output formatting utilities.
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()
}
// defaultBinariesPath returns the default path where plugins are installed.
// It constructs a path in the user's home directory under .privateer/bin.
func defaultBinariesPath() string {
home, _ := os.UserHomeDir()
return path.Join(home, ".privateer", "bin")
}