Skip to content

Commit 3c59697

Browse files
authored
Add Exit 1 on Deploy Failures (#8)
For item: github/spark#816. Exit on deploy failures so the error is propagated. The deploy script has `set e` so it'll exit 1 if the cli command fails and show an error in the UI on deployment
1 parent 3563aea commit 3c59697

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

cmd/deploy.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8+
"net/url"
89
"os"
910
"path/filepath"
10-
"net/url"
11+
1112
"github.com/MakeNowJust/heredoc"
1213
"github.com/cli/go-gh/v2/pkg/api"
1314
"github.com/spf13/cobra"
1415
)
1516

1617
type deployCmdFlags struct {
17-
dir string
18-
app string
18+
dir string
19+
app string
1920
revisionName string
2021
}
2122

@@ -95,40 +96,34 @@ func init() {
9596
$ gh runtime deploy --dir ./dist --app my-app
9697
# => Deploys the contents of the 'dist' directory to the app named 'my-app'
9798
`),
98-
Run: func(cmd *cobra.Command, args []string) {
99+
RunE: func(cmd *cobra.Command, args []string) error {
99100
if deployCmdFlags.dir == "" {
100-
fmt.Println("Error: --dir flag is required")
101-
return
101+
return fmt.Errorf("--dir flag is required")
102102
}
103103
if deployCmdFlags.app == "" {
104-
fmt.Println("Error: --app flag is required")
105-
return
104+
return fmt.Errorf("--app flag is required")
106105
}
107106

108107
if _, err := os.Stat(deployCmdFlags.dir); os.IsNotExist(err) {
109-
fmt.Printf("Error: directory '%s' does not exist\n", deployCmdFlags.dir)
110-
return
108+
return fmt.Errorf("directory '%s' does not exist", deployCmdFlags.dir)
111109
}
112110

113111
_, err := os.ReadDir(deployCmdFlags.dir)
114112
if err != nil {
115-
fmt.Printf("Error reading directory '%s': %v\n", deployCmdFlags.dir, err)
116-
return
113+
return fmt.Errorf("error reading directory '%s': %v", deployCmdFlags.dir, err)
117114
}
118115

119116
// Zip the directory
120117
zipPath := fmt.Sprintf("%s.zip", deployCmdFlags.dir)
121118
err = zipDirectory(deployCmdFlags.dir, zipPath)
122119
if err != nil {
123-
fmt.Printf("Error zipping directory '%s': %v\n", deployCmdFlags.dir, err)
124-
return
120+
return fmt.Errorf("error zipping directory '%s': %v", deployCmdFlags.dir, err)
125121
}
126122
defer os.Remove(zipPath)
127123

128124
client, err := api.DefaultRESTClient()
129125
if err != nil {
130-
fmt.Println(err)
131-
return
126+
return fmt.Errorf("error creating REST client: %v", err)
132127
}
133128

134129
deploymentsUrl := fmt.Sprintf("runtime/%s/deployment/bundle", deployCmdFlags.app)
@@ -145,17 +140,16 @@ func init() {
145140
// body is the full zip RAW
146141
body, err := os.ReadFile(zipPath)
147142
if err != nil {
148-
fmt.Printf("Error reading zip file '%s': %v\n", zipPath, err)
149-
return
143+
return fmt.Errorf("error reading zip file '%s': %v", zipPath, err)
150144
}
151145

152146
err = client.Post(deploymentsUrl, bytes.NewReader(body), nil)
153147
if err != nil {
154-
fmt.Printf("Error deploying app: %v\n", err)
155-
return
148+
return fmt.Errorf("error deploying app: %v", err)
156149
}
157150

158151
fmt.Printf("Successfully deployed app\n")
152+
return nil
159153
},
160154
}
161155
deployCmd.Flags().StringVarP(&deployCmdFlags.dir, "dir", "d", "", "The directory to deploy")

0 commit comments

Comments
 (0)