Skip to content

Commit 851a483

Browse files
committed
fix: function rename
1 parent aa9dff5 commit 851a483

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

go/understack/cmd/deploy/deploy.go

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,80 +16,81 @@ const (
1616
deployRepoFlag = "deploy-repo"
1717
)
1818

19+
// NewCmdDeploy returns the root "deploy" command for UnderStack deployments.
1920
func NewCmdDeploy() *cobra.Command {
2021
cmd := &cobra.Command{
2122
Use: "deploy [--deploy-repo UC_DEPLOY] command",
2223
Short: "UnderStack deployment",
23-
Long: ``,
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
// If no subcommand, show help
25+
// Show help if no subcommands are provided
2626
return cmd.Help()
2727
},
28-
PersistentPreRunE: preRun,
28+
PersistentPreRunE: ensureDeployRepo,
2929
}
3030

31-
setFlags(cmd)
32-
31+
addDeployRepoFlag(cmd)
3332
return cmd
3433
}
3534

36-
func expandPath(path string) (string, error) {
37-
if strings.HasPrefix(path, "~") {
38-
home, err := os.UserHomeDir()
39-
if err != nil {
40-
return "", err
41-
}
42-
return filepath.Join(home, path[1:]), nil
35+
// addDeployRepoFlag binds and configures the persistent --deploy-repo flag.
36+
func addDeployRepoFlag(cmd *cobra.Command) {
37+
// Bind environment variable
38+
if err := viper.BindEnv(deployRepoFlag, deployRepoEnvVar); err != nil {
39+
log.Fatal("failed to bind env var", "env", deployRepoEnvVar, "err", err)
40+
os.Exit(1)
41+
}
42+
43+
// Set default value from env or fallback
44+
defaultRepo := viper.GetString(deployRepoFlag)
45+
if defaultRepo == "" {
46+
defaultRepo = "."
47+
}
48+
49+
help := fmt.Sprintf("Path to your deployment repo (env: %s) (current: %s)", deployRepoEnvVar, defaultRepo)
50+
cmd.PersistentFlags().String(deployRepoFlag, "", help)
51+
52+
if err := viper.BindPFlag(deployRepoFlag, cmd.PersistentFlags().Lookup(deployRepoFlag)); err != nil {
53+
log.Fatal("failed to bind flag", "flag", deployRepoFlag, "err", err)
54+
os.Exit(1)
4355
}
44-
return path, nil
4556
}
4657

47-
func preRun(cmd *cobra.Command, args []string) error {
48-
deployRepo := viper.GetString(deployRepoFlag)
49-
if deployRepo == "" {
50-
return nil
58+
// ensureDeployRepo validates and switches to the deployment repo directory, if provided.
59+
func ensureDeployRepo(cmd *cobra.Command, args []string) error {
60+
repoPath := viper.GetString(deployRepoFlag)
61+
if repoPath == "" {
62+
return nil // nothing to do
5163
}
5264

53-
expandedRepoDir, err := expandPath(deployRepo)
65+
expandedPath, err := expandHomePath(repoPath)
5466
if err != nil {
5567
return fmt.Errorf("failed to expand path: %w", err)
5668
}
5769

58-
info, err := os.Stat(expandedRepoDir)
70+
info, err := os.Stat(expandedPath)
5971
if err != nil {
60-
return fmt.Errorf("could not access directory '%s': %w", expandedRepoDir, err)
72+
return fmt.Errorf("could not access directory '%s': %w", expandedPath, err)
6173
}
6274
if !info.IsDir() {
63-
return fmt.Errorf("path '%s' is not a directory", expandedRepoDir)
75+
return fmt.Errorf("path '%s' is not a directory", expandedPath)
6476
}
6577

66-
// Switch working directory
67-
if err := os.Chdir(expandedRepoDir); err != nil {
68-
return fmt.Errorf("failed to change working directory to '%s': %w", expandedRepoDir, err)
78+
if err := os.Chdir(expandedPath); err != nil {
79+
return fmt.Errorf("failed to change working directory to '%s': %w", expandedPath, err)
6980
}
7081

71-
log.Infof("deployment repo path: %s", expandedRepoDir)
72-
82+
log.Infof("Using deployment repo: %s", expandedPath)
7383
return nil
7484
}
7585

76-
func setFlags(cmd *cobra.Command) {
77-
// bind our flag
78-
if err := viper.BindEnv(deployRepoFlag, deployRepoEnvVar); err != nil {
79-
log.Fatal("failed to bind", "env", deployRepoFlag, "err", err)
80-
os.Exit(1)
81-
}
82-
deployRepo := viper.GetString(deployRepoFlag)
83-
if deployRepo == "" {
84-
deployRepo = "."
85-
}
86-
helpText := fmt.Sprintf(
87-
"Path to your deployment repo (env: %s) (current: %s)",
88-
deployRepoEnvVar, deployRepo,
89-
)
90-
cmd.PersistentFlags().String(deployRepoFlag, "", helpText)
91-
if err := viper.BindPFlag(deployRepoFlag, cmd.PersistentFlags().Lookup(deployRepoFlag)); err != nil {
92-
log.Fatal("failed to bind", "flag", deployRepoFlag, "err", err)
93-
os.Exit(1)
86+
// expandHomePath expands a path starting with "~" to the user's home directory.
87+
func expandHomePath(path string) (string, error) {
88+
if strings.HasPrefix(path, "~") {
89+
home, err := os.UserHomeDir()
90+
if err != nil {
91+
return "", err
92+
}
93+
return filepath.Join(home, path[1:]), nil
9494
}
95+
return path, nil
9596
}

0 commit comments

Comments
 (0)