Skip to content

Commit d40f13a

Browse files
Display Default App URL on Get (#4)
closes github/spark#235
1 parent 1b070ca commit d40f13a

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

cmd/get.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"encoding/json"
54
"fmt"
65

76
"github.com/MakeNowJust/heredoc"
@@ -13,6 +12,10 @@ type getCmdFlags struct {
1312
app string
1413
}
1514

15+
type serverResponse struct {
16+
AppUrl string `json:"app_url"`
17+
}
18+
1619
func init() {
1720
getCmdFlags := getCmdFlags{}
1821
getCmd := &cobra.Command{
@@ -25,27 +28,25 @@ func init() {
2528
$ gh runtime get --app my-app
2629
# => Retrieves details of the app named 'my-app'
2730
`),
28-
Run: func(cmd *cobra.Command, args []string) {
31+
RunE: func(cmd *cobra.Command, args []string) error {
2932
if getCmdFlags.app == "" {
30-
fmt.Println("Error: --app flag is required")
31-
return
33+
return fmt.Errorf("--app flag is required")
3234
}
3335

3436
getUrl := fmt.Sprintf("runtime/%s/deployment", getCmdFlags.app)
3537
client, err := api.DefaultRESTClient()
3638
if err != nil {
37-
fmt.Println(err)
38-
return
39+
return fmt.Errorf("failed creating REST client: %v", err)
3940
}
4041

41-
response := json.RawMessage{}
42+
response := serverResponse{}
4243
err = client.Get(getUrl, &response)
4344
if err != nil {
44-
fmt.Printf("Error retrieving app details: %v\n", err)
45-
return
45+
return fmt.Errorf("retrieving app details: %v", err)
4646
}
4747

48-
fmt.Printf("App Details: %s\n", response)
48+
fmt.Printf("%s\n", response.AppUrl)
49+
return nil
4950
},
5051
}
5152

cmd/root.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ var rootCmd = &cobra.Command{
1919
},
2020
}
2121

22-
func Execute() {
22+
type exitCode int
23+
24+
const (
25+
exitOK exitCode = 0
26+
exitError exitCode = 1
27+
exitCancel exitCode = 2
28+
exitAuth exitCode = 4
29+
exitPending exitCode = 8
30+
)
31+
32+
func Execute() exitCode {
2333
if err := rootCmd.Execute(); err != nil {
2434
fmt.Fprintln(os.Stderr, err)
25-
os.Exit(1)
35+
return exitError
2636
}
37+
38+
return exitOK
2739
}

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/github/gh-runtime-cli/cmd"
57
)
68

79
func main() {
8-
cmd.Execute()
10+
code := cmd.Execute()
11+
os.Exit(int(code))
912
}

0 commit comments

Comments
 (0)