@@ -16,80 +16,81 @@ const (
16
16
deployRepoFlag = "deploy-repo"
17
17
)
18
18
19
+ // NewCmdDeploy returns the root "deploy" command for UnderStack deployments.
19
20
func NewCmdDeploy () * cobra.Command {
20
21
cmd := & cobra.Command {
21
22
Use : "deploy [--deploy-repo UC_DEPLOY] command" ,
22
23
Short : "UnderStack deployment" ,
23
- Long : `` ,
24
24
RunE : func (cmd * cobra.Command , args []string ) error {
25
- // If no subcommand, show help
25
+ // Show help if no subcommands are provided
26
26
return cmd .Help ()
27
27
},
28
- PersistentPreRunE : preRun ,
28
+ PersistentPreRunE : ensureDeployRepo ,
29
29
}
30
30
31
- setFlags (cmd )
32
-
31
+ addDeployRepoFlag (cmd )
33
32
return cmd
34
33
}
35
34
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 )
43
55
}
44
- return path , nil
45
56
}
46
57
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
51
63
}
52
64
53
- expandedRepoDir , err := expandPath ( deployRepo )
65
+ expandedPath , err := expandHomePath ( repoPath )
54
66
if err != nil {
55
67
return fmt .Errorf ("failed to expand path: %w" , err )
56
68
}
57
69
58
- info , err := os .Stat (expandedRepoDir )
70
+ info , err := os .Stat (expandedPath )
59
71
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 )
61
73
}
62
74
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 )
64
76
}
65
77
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 )
69
80
}
70
81
71
- log .Infof ("deployment repo path: %s" , expandedRepoDir )
72
-
82
+ log .Infof ("Using deployment repo: %s" , expandedPath )
73
83
return nil
74
84
}
75
85
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
94
94
}
95
+ return path , nil
95
96
}
0 commit comments