Skip to content

Commit 9c2a855

Browse files
committed
--init parameter on create command
1 parent 27aa409 commit 9c2a855

2 files changed

Lines changed: 44 additions & 31 deletions

File tree

cmd/create.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type createCmdFlags struct {
1717
EnvironmentVariables []string
1818
Secrets []string
1919
RevisionName string
20+
Init bool
2021
}
2122

2223
type createReq struct {
@@ -104,12 +105,21 @@ func init() {
104105
}
105106

106107
fmt.Printf("App created: %s\n", response.AppUrl) // TODO pretty print details
108+
109+
if createCmdFlags.Init {
110+
err = writeRuntimeConfig(createCmdFlags.app, "")
111+
if err != nil {
112+
fmt.Printf("Error initializing config: %v\n", err)
113+
return
114+
}
115+
}
107116
},
108117
}
109118

110119
createCmd.Flags().StringVarP(&createCmdFlags.app, "app", "a", "", "The app to create")
111120
createCmd.Flags().StringSliceVarP(&createCmdFlags.EnvironmentVariables, "env", "e", []string{}, "Environment variables to set on the app in the form 'key=value'")
112121
createCmd.Flags().StringSliceVarP(&createCmdFlags.Secrets, "secret", "s", []string{}, "Secrets to set on the app in the form 'key=value'")
113122
createCmd.Flags().StringVarP(&createCmdFlags.RevisionName, "revision-name", "r", "", "The revision name to use for the app")
123+
createCmd.Flags().BoolVar(&createCmdFlags.Init, "init", false, "Initialize a runtime.config.json file in the current directory after creating the app")
114124
rootCmd.AddCommand(createCmd)
115125
}

cmd/init.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,39 @@ type appResponse struct {
2121
AppUrl string `json:"app_url"`
2222
}
2323

24+
// writeRuntimeConfig writes a runtime.config.json file for the given app.
25+
// If outPath is empty, it defaults to "runtime.config.json" in the current directory.
26+
func writeRuntimeConfig(app string, outPath string) error {
27+
configStruct := config.RuntimeConfig{
28+
App: app,
29+
}
30+
31+
configPath := "runtime.config.json"
32+
if outPath != "" {
33+
configPath = outPath
34+
outputDir := filepath.Dir(configPath)
35+
if outputDir != "." {
36+
err := os.MkdirAll(outputDir, 0755)
37+
if err != nil {
38+
return fmt.Errorf("error creating directory '%s': %v", outputDir, err)
39+
}
40+
}
41+
}
42+
43+
configBytes, err := json.MarshalIndent(configStruct, "", " ")
44+
if err != nil {
45+
return fmt.Errorf("error creating configuration: %v", err)
46+
}
47+
48+
err = os.WriteFile(configPath, configBytes, 0644)
49+
if err != nil {
50+
return fmt.Errorf("error writing configuration file: %v", err)
51+
}
52+
53+
fmt.Printf("Successfully initialized local project for Spark app '%s' at '%s'\n", app, configPath)
54+
return nil
55+
}
56+
2457
func init() {
2558
initCmdFlags := initCmdFlags{}
2659
initCmd := &cobra.Command{
@@ -47,7 +80,6 @@ func init() {
4780
return fmt.Errorf("--app flag is required")
4881
}
4982

50-
// Determine the identifier to use for the API call
5183
identifier := initCmdFlags.app
5284

5385
getUrl := fmt.Sprintf("runtime/%s/deployment", identifier)
@@ -63,36 +95,7 @@ func init() {
6395
return fmt.Errorf("app '%s' does not exist or is not accessible: %v", identifier, err)
6496
}
6597

66-
// Create runtime config
67-
configStruct := config.RuntimeConfig{
68-
App: initCmdFlags.app,
69-
}
70-
71-
configPath := "runtime.config.json"
72-
if initCmdFlags.out != "" {
73-
configPath = initCmdFlags.out
74-
// Create directory if it doesn't exist
75-
outputDir := filepath.Dir(configPath)
76-
if outputDir != "." {
77-
err = os.MkdirAll(outputDir, 0755)
78-
if err != nil {
79-
return fmt.Errorf("error creating directory '%s': %v", outputDir, err)
80-
}
81-
}
82-
}
83-
84-
configBytes, err := json.MarshalIndent(configStruct, "", " ")
85-
if err != nil {
86-
return fmt.Errorf("error creating configuration: %v", err)
87-
}
88-
89-
err = os.WriteFile(configPath, configBytes, 0644)
90-
if err != nil {
91-
return fmt.Errorf("error writing configuration file: %v", err)
92-
}
93-
94-
fmt.Printf("Successfully initialized local project for Spark app '%s' at '%s'\n", identifier, configPath)
95-
return nil
98+
return writeRuntimeConfig(initCmdFlags.app, initCmdFlags.out)
9699
},
97100
}
98101

0 commit comments

Comments
 (0)