Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor app name resolution logic
  • Loading branch information
salvador-barboza committed Sep 30, 2025
commit 32a5ae97098264727343d67e226ae0e063214352
29 changes: 4 additions & 25 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,37 +112,16 @@ func init() {
return fmt.Errorf("--dir flag is required")
}

appName := deployCmdFlags.app

// If config file is provided, read app name from it
if deployCmdFlags.config != "" {
configApp, err := config.ReadRuntimeConfig(deployCmdFlags.config)
if err != nil {
return err
}
if appName == "" {
appName = configApp
}
} else if appName == "" {
// Try to read from default config file if neither --app nor --config is provided
if _, err := os.Stat("runtime.config.json"); err == nil {
configApp, err := config.ReadRuntimeConfig("runtime.config.json")
if err != nil {
return fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
}
appName = configApp
}
}

if appName == "" {
return fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
appName, err := config.ResolveAppName(deployCmdFlags.app, deployCmdFlags.config)
if err != nil {
return err
}

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

_, err := os.ReadDir(deployCmdFlags.dir)
_, err = os.ReadDir(deployCmdFlags.dir)
if err != nil {
return fmt.Errorf("error reading directory '%s': %v", deployCmdFlags.dir, err)
}
Expand Down
28 changes: 3 additions & 25 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"net/url"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/cli/go-gh/v2/pkg/api"
Expand Down Expand Up @@ -42,30 +41,9 @@ func init() {
# => Retrieves details using app name from runtime.config.json in current directory (if it exists).
`),
RunE: func(cmd *cobra.Command, args []string) error {
appName := getCmdFlags.app

// If config file is provided, read app name from it
if getCmdFlags.config != "" {
configApp, err := config.ReadRuntimeConfig(getCmdFlags.config)
if err != nil {
return err
}
if appName == "" {
appName = configApp
}
} else if appName == "" {
// Try to read from default config file if neither --app nor --config is provided
if _, err := os.Stat("runtime.config.json"); err == nil {
configApp, err := config.ReadRuntimeConfig("runtime.config.json")
if err != nil {
return fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
}
appName = configApp
}
}

if appName == "" {
return fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
appName, err := config.ResolveAppName(getCmdFlags.app, getCmdFlags.config)
if err != nil {
return err
}

getUrl := fmt.Sprintf("runtime/%s/deployment", appName)
Expand Down
29 changes: 29 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,32 @@ func ReadRuntimeConfig(configPath string) (string, error) {

return config.App, nil
}

// ResolveAppName resolves the app name using the priority order:
// 1. appFlag (--app) if provided
// 2. configPath (--config) if provided
// 3. runtime.config.json in current directory if it exists
// Returns an error if no app name can be resolved
func ResolveAppName(appFlag, configPath string) (string, error) {
// Priority 1: Use --app flag if provided
if appFlag != "" {
return appFlag, nil
}

// Priority 2: Use --config file if provided
if configPath != "" {
return ReadRuntimeConfig(configPath)
}

// Priority 3: Try default runtime.config.json
if _, err := os.Stat("runtime.config.json"); err == nil {
appName, err := ReadRuntimeConfig("runtime.config.json")
if err != nil {
return "", fmt.Errorf("found runtime.config.json but failed to read it: %v", err)
}
return appName, nil
}

// No app name could be resolved
return "", fmt.Errorf("--app flag is required, --config must be specified, or runtime.config.json must exist in current directory")
}
Loading