Skip to content

Commit c4133df

Browse files
committed
Improve config propagation and consistency across commands.
1 parent af4c3da commit c4133df

File tree

13 files changed

+482
-374
lines changed

13 files changed

+482
-374
lines changed

api/client.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,22 @@ func (client *Client) DeleteDomain(project, domain string) error {
357357
return nil
358358
}
359359

360-
func (client *Client) LinkDomain(project, domain, service string, allowInsecureTraffic bool) error {
360+
type LinkDomainArgs struct {
361+
Project string
362+
Domain string
363+
Service string
364+
AllowInsecureTraffic bool
365+
}
366+
367+
func (client *Client) LinkDomain(args LinkDomainArgs) error {
361368
var (
362-
path = fmt.Sprintf("/projects/%s/domains/%s/link", project, domain)
369+
path = fmt.Sprintf("/projects/%s/domains/%s/link", args.Project, args.Domain)
363370
payload, _ = json.Marshal(struct {
364371
Service string `json:"service"`
365372
AllowInsecureTraffic bool `json:"allowInsecureTraffic"`
366373
}{
367-
Service: service,
368-
AllowInsecureTraffic: allowInsecureTraffic,
374+
Service: args.Service,
375+
AllowInsecureTraffic: args.AllowInsecureTraffic,
369376
})
370377
)
371378
if err := client.request(http.MethodPost, path, nil, bytes.NewReader(payload)); err != nil {
@@ -374,12 +381,20 @@ func (client *Client) LinkDomain(project, domain, service string, allowInsecureT
374381
return nil
375382
}
376383

377-
func (client *Client) UnlinkDomain(project, domain, service string) error {
384+
type UnlinkDomainArgs struct {
385+
Project string
386+
Domain string
387+
Service string
388+
}
389+
390+
func (client *Client) UnlinkDomain(args UnlinkDomainArgs) error {
378391
var (
379-
path = fmt.Sprintf("/projects/%s/domains/%s/link", project, domain)
392+
path = fmt.Sprintf("/projects/%s/domains/%s/link", args.Project, args.Domain)
380393
payload, _ = json.Marshal(struct {
381394
Service string `json:"service"`
382-
}{service})
395+
}{
396+
Service: args.Service,
397+
})
383398
)
384399
if err := client.request(http.MethodDelete, path, nil, bytes.NewReader(payload)); err != nil {
385400
return err

cmd/builds.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,29 @@ import (
77
"strings"
88
"text/tabwriter"
99

10-
"github.com/valar/cli/config"
11-
12-
"github.com/valar/cli/api"
13-
1410
"github.com/dustin/go-humanize"
1511
"github.com/fatih/color"
1612
"github.com/juju/ansiterm"
1713
"github.com/spf13/cobra"
14+
"github.com/valar/cli/api"
15+
"github.com/valar/cli/config"
1816
)
1917

18+
var buildService string
19+
2020
var buildCmd = &cobra.Command{
21-
Use: "builds [prefix]",
21+
Use: "build [--service service]",
22+
Short: "Manage the builds of a service",
23+
Aliases: []string{"builds", "b"},
24+
}
25+
26+
var buildListCmd = &cobra.Command{
27+
Use: "list [prefix]",
2228
Short: "List builds of the service",
2329
Args: cobra.MaximumNArgs(1),
2430
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
25-
cfg := &config.ServiceConfig{}
26-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
31+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
32+
if err != nil {
2733
return err
2834
}
2935
client, err := globalConfiguration.APIClient()
@@ -42,8 +48,8 @@ var buildAbortCmd = &cobra.Command{
4248
Short: "Abort a scheduled or running build",
4349
Args: cobra.MaximumNArgs(1),
4450
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
45-
cfg := &config.ServiceConfig{}
46-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
51+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
52+
if err != nil {
4753
return err
4854
}
4955
client, err := globalConfiguration.APIClient()
@@ -53,19 +59,19 @@ var buildAbortCmd = &cobra.Command{
5359
if len(args) < 1 {
5460
args = append(args, "")
5561
}
56-
return client.AbortBuild(cfg.Project, cfg.Service, args[0])
62+
return client.AbortBuild(cfg.Project(), cfg.Service(), args[0])
5763
}),
5864
}
5965

6066
var logsFollow = false
6167

6268
var buildLogsCmd = &cobra.Command{
63-
Use: "logs [task]",
69+
Use: "logs [buildid]",
6470
Short: "Show the build logs of the given task",
6571
Args: cobra.MaximumNArgs(1),
6672
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
67-
cfg := &config.ServiceConfig{}
68-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
73+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
74+
if err != nil {
6975
return err
7076
}
7177
client, err := globalConfiguration.APIClient()
@@ -77,7 +83,7 @@ var buildLogsCmd = &cobra.Command{
7783
if len(args) > 0 {
7884
prefix = args[0]
7985
}
80-
builds, err := client.ListBuilds(cfg.Project, cfg.Service, prefix)
86+
builds, err := client.ListBuilds(cfg.Project(), cfg.Service(), prefix)
8187
if err != nil {
8288
return err
8389
}
@@ -88,19 +94,19 @@ var buildLogsCmd = &cobra.Command{
8894
sort.Slice(builds, func(i, j int) bool { return builds[i].CreatedAt.After(builds[j].CreatedAt) })
8995
latestBuildID := builds[0].ID
9096
if logsFollow {
91-
return client.StreamBuildLogs(cfg.Project, cfg.Service, latestBuildID, os.Stdout)
97+
return client.StreamBuildLogs(cfg.Project(), cfg.Service(), latestBuildID, os.Stdout)
9298
}
93-
return client.ShowBuildLogs(cfg.Project, cfg.Service, latestBuildID, os.Stdout)
99+
return client.ShowBuildLogs(cfg.Project(), cfg.Service(), latestBuildID, os.Stdout)
94100
}),
95101
}
96102

97-
var inspectCmd = &cobra.Command{
103+
var buildInspectCmd = &cobra.Command{
98104
Use: "inspect [prefix]",
99105
Short: "Inspect the first matched task with the given ID prefix",
100106
Args: cobra.ExactArgs(1),
101107
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
102-
cfg := &config.ServiceConfig{}
103-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
108+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
109+
if err != nil {
104110
return err
105111
}
106112
client, err := globalConfiguration.APIClient()
@@ -111,13 +117,13 @@ var inspectCmd = &cobra.Command{
111117
}),
112118
}
113119

114-
var statusCmd = &cobra.Command{
120+
var buildStatusCmd = &cobra.Command{
115121
Use: "status [buildid]",
116122
Short: "Show the status of the given build",
117123
Args: cobra.ExactArgs(1),
118124
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
119-
cfg := &config.ServiceConfig{}
120-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
125+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
126+
if err != nil {
121127
return err
122128
}
123129
client, err := globalConfiguration.APIClient()
@@ -129,8 +135,8 @@ var statusCmd = &cobra.Command{
129135
}),
130136
}
131137

132-
func listBuilds(client *api.Client, cfg *config.ServiceConfig, id string) error {
133-
builds, err := client.ListBuilds(cfg.Project, cfg.Service, id)
138+
func listBuilds(client *api.Client, cfg config.ServiceConfig, id string) error {
139+
builds, err := client.ListBuilds(cfg.Project(), cfg.Service(), id)
134140
if err != nil {
135141
return err
136142
}
@@ -151,8 +157,8 @@ func listBuilds(client *api.Client, cfg *config.ServiceConfig, id string) error
151157
return nil
152158
}
153159

154-
func showBuildStatusAndExit(client *api.Client, cfg *config.ServiceConfig, id string) error {
155-
build, err := client.InspectBuild(cfg.Project, cfg.Service, id)
160+
func showBuildStatusAndExit(client *api.Client, cfg config.ServiceConfig, id string) error {
161+
build, err := client.InspectBuild(cfg.Project(), cfg.Service(), id)
156162
if err != nil {
157163
return err
158164
}
@@ -161,8 +167,8 @@ func showBuildStatusAndExit(client *api.Client, cfg *config.ServiceConfig, id st
161167
return nil
162168
}
163169

164-
func inspectBuild(client *api.Client, cfg *config.ServiceConfig, id string) error {
165-
build, err := client.InspectBuild(cfg.Project, cfg.Service, id)
170+
func inspectBuild(client *api.Client, cfg config.ServiceConfig, id string) error {
171+
build, err := client.InspectBuild(cfg.Project(), cfg.Service(), id)
166172
if err != nil {
167173
return err
168174
}
@@ -180,13 +186,13 @@ func inspectBuild(client *api.Client, cfg *config.ServiceConfig, id string) erro
180186
return nil
181187
}
182188

183-
func deployBuild(client *api.Client, cfg *config.ServiceConfig, id string) error {
189+
func deployBuild(client *api.Client, cfg config.ServiceConfig, id string) error {
184190
var deployReq api.DeployRequest
185191
deployReq.Build = id
186-
for _, kv := range cfg.Deployment.Environment {
192+
for _, kv := range cfg.Deployment().Environment {
187193
deployReq.Environment = append(deployReq.Environment, api.KVPair(kv))
188194
}
189-
deployment, err := client.SubmitDeploy(cfg.Project, cfg.Service, &deployReq)
195+
deployment, err := client.SubmitDeploy(cfg.Project(), cfg.Service(), &deployReq)
190196
if err != nil {
191197
return err
192198
}
@@ -219,10 +225,8 @@ func colorize(status string) string {
219225
}
220226

221227
func initBuildsCmd() {
222-
buildLogsCmd.PersistentFlags().BoolVarP(&logsFollow, "follow", "f", false, "follow the logs")
223-
buildCmd.AddCommand(inspectCmd)
224-
buildCmd.AddCommand(buildLogsCmd)
225-
buildCmd.AddCommand(buildAbortCmd)
226-
buildCmd.AddCommand(statusCmd)
228+
buildCmd.PersistentFlags().StringVarP(&buildService, "service", "s", "", "The service to inspect for builds")
229+
buildLogsCmd.PersistentFlags().BoolVarP(&logsFollow, "follow", "f", false, "Follow the logs")
230+
buildCmd.AddCommand(buildListCmd, buildInspectCmd, buildLogsCmd, buildAbortCmd, buildStatusCmd)
227231
rootCmd.AddCommand(buildCmd)
228232
}

cmd/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/juju/ansiterm"
88
"github.com/spf13/cobra"
9+
"github.com/valar/cli/config"
910
"gopkg.in/yaml.v3"
1011
)
1112

@@ -53,7 +54,7 @@ var configEndpointSetCmd = &cobra.Command{
5354
ep.URL = configEndpointSetUrl
5455
}
5556
if globalConfiguration.Endpoints == nil {
56-
globalConfiguration.Endpoints = make(map[string]valarEndpoint)
57+
globalConfiguration.Endpoints = map[string]config.APIEndpoint{}
5758
}
5859
globalConfiguration.Endpoints[args[0]] = ep
5960
if err := globalConfiguration.Write(); err != nil {

cmd/cron.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import (
66
"strings"
77
"text/tabwriter"
88

9-
"github.com/valar/cli/config"
10-
11-
"github.com/valar/cli/api"
12-
139
"github.com/dustin/go-humanize"
1410
"github.com/spf13/cobra"
11+
"github.com/valar/cli/api"
12+
"github.com/valar/cli/config"
1513
)
1614

1715
var (
@@ -20,16 +18,21 @@ var (
2018
cronCmd = &cobra.Command{
2119
Use: "cron",
2220
Short: "Manage scheduled invocations of a service",
21+
}
22+
23+
cronListCmd = &cobra.Command{
24+
Use: "list",
25+
Short: "List all cron schedules for a service",
2326
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
24-
cfg := &config.ServiceConfig{}
25-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
27+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
28+
if err != nil {
2629
return err
2730
}
2831
client, err := globalConfiguration.APIClient()
2932
if err != nil {
3033
return err
3134
}
32-
schedules, err := client.ListSchedules(cfg.Project, cfg.Service)
35+
schedules, err := client.ListSchedules(cfg.Project(), cfg.Service())
3336
if err != nil {
3437
return err
3538
}
@@ -50,15 +53,15 @@ var (
5053
Short: "Add or edit a service invocation schedule",
5154
Args: cobra.ExactArgs(2),
5255
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
53-
cfg := &config.ServiceConfig{}
54-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
56+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
57+
if err != nil {
5558
return err
5659
}
5760
client, err := globalConfiguration.APIClient()
5861
if err != nil {
5962
return err
6063
}
61-
if err := client.AddSchedule(cfg.Project, cfg.Service, api.Schedule{
64+
if err := client.AddSchedule(cfg.Project(), cfg.Service(), api.Schedule{
6265
Name: args[0],
6366
Timespec: args[1],
6467
Payload: cronAddPayload,
@@ -75,15 +78,15 @@ var (
7578
Short: "Manually triggers a scheduled invocation",
7679
Args: cobra.ExactArgs(1),
7780
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
78-
cfg := &config.ServiceConfig{}
79-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
81+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
82+
if err != nil {
8083
return err
8184
}
8285
client, err := globalConfiguration.APIClient()
8386
if err != nil {
8487
return err
8588
}
86-
if err := client.TriggerSchedule(cfg.Project, cfg.Service, args[0]); err != nil {
89+
if err := client.TriggerSchedule(cfg.Project(), cfg.Service(), args[0]); err != nil {
8790
return err
8891
}
8992
return nil
@@ -94,15 +97,15 @@ var (
9497
Short: "Remove a service invocation schedule",
9598
Args: cobra.ExactArgs(1),
9699
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
97-
cfg := &config.ServiceConfig{}
98-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
100+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
101+
if err != nil {
99102
return err
100103
}
101104
client, err := globalConfiguration.APIClient()
102105
if err != nil {
103106
return err
104107
}
105-
if err := client.RemoveSchedule(cfg.Project, cfg.Service, args[0]); err != nil {
108+
if err := client.RemoveSchedule(cfg.Project(), cfg.Service(), args[0]); err != nil {
106109
return err
107110
}
108111
return nil
@@ -113,15 +116,15 @@ var (
113116
Short: "Inspect the invocation history of a service schedule",
114117
Args: cobra.ExactArgs(1),
115118
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
116-
cfg := &config.ServiceConfig{}
117-
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
119+
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
120+
if err != nil {
118121
return err
119122
}
120123
client, err := globalConfiguration.APIClient()
121124
if err != nil {
122125
return err
123126
}
124-
invocations, err := client.InspectSchedule(cfg.Project, cfg.Service, args[0])
127+
invocations, err := client.InspectSchedule(cfg.Project(), cfg.Service(), args[0])
125128
if err != nil {
126129
return err
127130
}
@@ -153,7 +156,7 @@ var (
153156
func initCronCmd() {
154157
rootCmd.AddCommand(cronCmd)
155158
cronCmd.PersistentFlags().StringVarP(&cronService, "service", "s", "", "The service to manage cron schedules for")
156-
cronCmd.AddCommand(cronAddCmd, cronTriggerCmd, cronRemoveCmd, cronInspectCmd)
159+
cronCmd.AddCommand(cronListCmd, cronAddCmd, cronTriggerCmd, cronRemoveCmd, cronInspectCmd)
157160
cronAddCmd.Flags().StringVar(&cronAddPath, "path", "/", "The service path to send a request to")
158161
cronAddCmd.Flags().StringVar(&cronAddPayload, "payload", "", "The body payload to send in a request")
159162
}

0 commit comments

Comments
 (0)