Skip to content

Commit 1b070ca

Browse files
Support Creating Apps with Environment Variables (#3)
1 parent 7e6587e commit 1b070ca

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

cmd/create.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/MakeNowJust/heredoc"
910
"github.com/cli/go-gh/v2/pkg/api"
1011
"github.com/spf13/cobra"
1112
)
1213

1314
type createCmdFlags struct {
14-
app string
15+
app string
16+
EnvironmentVariables []string
17+
Secrets []string
1518
}
1619

1720
type createReq struct {
@@ -31,7 +34,7 @@ func init() {
3134
Create a GitHub Runtime app
3235
`),
3336
Example: heredoc.Doc(`
34-
$ gh runtime create --app my-app
37+
$ gh runtime create --app my-app --env key1=value1 --env key2=value2 --secret key3=value3 --secret key4=value4
3538
# => Creates the app named 'my-app'
3639
`),
3740
Run: func(cmd *cobra.Command, args []string) {
@@ -42,12 +45,32 @@ func init() {
4245

4346
// Construct the request body
4447
requestBody := createReq{
45-
EnvironmentVariables: map[string]string{
46-
"EXAMPLE_ENV": "value1",
47-
},
48-
Secrets: map[string]string{
49-
"SECRET_KEY": "secret_value",
50-
},
48+
EnvironmentVariables: map[string]string{},
49+
Secrets: map[string]string{},
50+
}
51+
52+
for _, pair := range createCmdFlags.EnvironmentVariables {
53+
parts := strings.SplitN(pair, "=", 2)
54+
if len(parts) == 2 {
55+
key := parts[0]
56+
value := parts[1]
57+
requestBody.EnvironmentVariables[key] = value
58+
} else {
59+
fmt.Printf("Error: Invalid environment variable format (%s). Must be in the form 'key=value'\n", pair)
60+
return
61+
}
62+
}
63+
64+
for _, pair := range createCmdFlags.Secrets {
65+
parts := strings.SplitN(pair, "=", 2)
66+
if len(parts) == 2 {
67+
key := parts[0]
68+
value := parts[1]
69+
requestBody.Secrets[key] = value
70+
} else {
71+
fmt.Printf("Error: Invalid secret format (%s). Must be in the form 'key=value'\n", pair)
72+
return
73+
}
5174
}
5275

5376
body, err := json.Marshal(requestBody)
@@ -74,5 +97,7 @@ func init() {
7497
}
7598

7699
createCmd.Flags().StringVarP(&createCmdFlags.app, "app", "a", "", "The app to create")
100+
createCmd.Flags().StringSliceVarP(&createCmdFlags.EnvironmentVariables, "env", "e", []string{}, "Environment variables to set on the app in the form 'key=value'")
101+
createCmd.Flags().StringSliceVarP(&createCmdFlags.Secrets, "secret", "s", []string{}, "Secrets to set on the app in the form 'key=value'")
77102
rootCmd.AddCommand(createCmd)
78103
}

0 commit comments

Comments
 (0)