Skip to content

Commit 32a5ae9

Browse files
Refactor app name resolution logic
1 parent c3f5c29 commit 32a5ae9

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

cmd/deploy.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,37 +112,16 @@ func init() {
112112
return fmt.Errorf("--dir flag is required")
113113
}
114114

115-
appName := deployCmdFlags.app
116-
117-
// If config file is provided, read app name from it
118-
if deployCmdFlags.config != "" {
119-
configApp, err := config.ReadRuntimeConfig(deployCmdFlags.config)
120-
if err != nil {
121-
return err
122-
}
123-
if appName == "" {
124-
appName = configApp
125-
}
126-
} else if appName == "" {
127-
// Try to read from default config file if neither --app nor --config is provided
128-
if _, err := os.Stat("runtime.config.json"); err == nil {
129-
configApp, err := config.ReadRuntimeConfig("runtime.config.json")
130-
if err != nil {
131-
return fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
132-
}
133-
appName = configApp
134-
}
135-
}
136-
137-
if appName == "" {
138-
return fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
115+
appName, err := config.ResolveAppName(deployCmdFlags.app, deployCmdFlags.config)
116+
if err != nil {
117+
return err
139118
}
140119

141120
if _, err := os.Stat(deployCmdFlags.dir); os.IsNotExist(err) {
142121
return fmt.Errorf("directory '%s' does not exist", deployCmdFlags.dir)
143122
}
144123

145-
_, err := os.ReadDir(deployCmdFlags.dir)
124+
_, err = os.ReadDir(deployCmdFlags.dir)
146125
if err != nil {
147126
return fmt.Errorf("error reading directory '%s': %v", deployCmdFlags.dir, err)
148127
}

cmd/get.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"net/url"
6-
"os"
76

87
"github.com/MakeNowJust/heredoc"
98
"github.com/cli/go-gh/v2/pkg/api"
@@ -42,30 +41,9 @@ func init() {
4241
# => Retrieves details using app name from runtime.config.json in current directory (if it exists).
4342
`),
4443
RunE: func(cmd *cobra.Command, args []string) error {
45-
appName := getCmdFlags.app
46-
47-
// If config file is provided, read app name from it
48-
if getCmdFlags.config != "" {
49-
configApp, err := config.ReadRuntimeConfig(getCmdFlags.config)
50-
if err != nil {
51-
return err
52-
}
53-
if appName == "" {
54-
appName = configApp
55-
}
56-
} else if appName == "" {
57-
// Try to read from default config file if neither --app nor --config is provided
58-
if _, err := os.Stat("runtime.config.json"); err == nil {
59-
configApp, err := config.ReadRuntimeConfig("runtime.config.json")
60-
if err != nil {
61-
return fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
62-
}
63-
appName = configApp
64-
}
65-
}
66-
67-
if appName == "" {
68-
return fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
44+
appName, err := config.ResolveAppName(getCmdFlags.app, getCmdFlags.config)
45+
if err != nil {
46+
return err
6947
}
7048

7149
getUrl := fmt.Sprintf("runtime/%s/deployment", appName)

internal/config/config.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,32 @@ func ReadRuntimeConfig(configPath string) (string, error) {
2626

2727
return config.App, nil
2828
}
29+
30+
// ResolveAppName resolves the app name using the priority order:
31+
// 1. appFlag (--app) if provided
32+
// 2. configPath (--config) if provided
33+
// 3. runtime.config.json in current directory if it exists
34+
// Returns an error if no app name can be resolved
35+
func ResolveAppName(appFlag, configPath string) (string, error) {
36+
// Priority 1: Use --app flag if provided
37+
if appFlag != "" {
38+
return appFlag, nil
39+
}
40+
41+
// Priority 2: Use --config file if provided
42+
if configPath != "" {
43+
return ReadRuntimeConfig(configPath)
44+
}
45+
46+
// Priority 3: Try default runtime.config.json
47+
if _, err := os.Stat("runtime.config.json"); err == nil {
48+
appName, err := ReadRuntimeConfig("runtime.config.json")
49+
if err != nil {
50+
return "", fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
51+
}
52+
return appName, nil
53+
}
54+
55+
// No app name could be resolved
56+
return "", fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
57+
}

0 commit comments

Comments
 (0)