Skip to content

Commit

Permalink
Improve config propagation and consistency across commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
lnsp committed Jul 14, 2024
1 parent af4c3da commit c4133df
Show file tree
Hide file tree
Showing 13 changed files with 482 additions and 374 deletions.
29 changes: 22 additions & 7 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,22 @@ func (client *Client) DeleteDomain(project, domain string) error {
return nil
}

func (client *Client) LinkDomain(project, domain, service string, allowInsecureTraffic bool) error {
type LinkDomainArgs struct {
Project string
Domain string
Service string
AllowInsecureTraffic bool
}

func (client *Client) LinkDomain(args LinkDomainArgs) error {
var (
path = fmt.Sprintf("/projects/%s/domains/%s/link", project, domain)
path = fmt.Sprintf("/projects/%s/domains/%s/link", args.Project, args.Domain)
payload, _ = json.Marshal(struct {
Service string `json:"service"`
AllowInsecureTraffic bool `json:"allowInsecureTraffic"`
}{
Service: service,
AllowInsecureTraffic: allowInsecureTraffic,
Service: args.Service,
AllowInsecureTraffic: args.AllowInsecureTraffic,
})
)
if err := client.request(http.MethodPost, path, nil, bytes.NewReader(payload)); err != nil {
Expand All @@ -374,12 +381,20 @@ func (client *Client) LinkDomain(project, domain, service string, allowInsecureT
return nil
}

func (client *Client) UnlinkDomain(project, domain, service string) error {
type UnlinkDomainArgs struct {
Project string
Domain string
Service string
}

func (client *Client) UnlinkDomain(args UnlinkDomainArgs) error {
var (
path = fmt.Sprintf("/projects/%s/domains/%s/link", project, domain)
path = fmt.Sprintf("/projects/%s/domains/%s/link", args.Project, args.Domain)
payload, _ = json.Marshal(struct {
Service string `json:"service"`
}{service})
}{
Service: args.Service,
})
)
if err := client.request(http.MethodDelete, path, nil, bytes.NewReader(payload)); err != nil {
return err
Expand Down
76 changes: 40 additions & 36 deletions cmd/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import (
"strings"
"text/tabwriter"

"github.com/valar/cli/config"

"github.com/valar/cli/api"

"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/juju/ansiterm"
"github.com/spf13/cobra"
"github.com/valar/cli/api"
"github.com/valar/cli/config"
)

var buildService string

var buildCmd = &cobra.Command{
Use: "builds [prefix]",
Use: "build [--service service]",
Short: "Manage the builds of a service",
Aliases: []string{"builds", "b"},
}

var buildListCmd = &cobra.Command{
Use: "list [prefix]",
Short: "List builds of the service",
Args: cobra.MaximumNArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
Expand All @@ -42,8 +48,8 @@ var buildAbortCmd = &cobra.Command{
Short: "Abort a scheduled or running build",
Args: cobra.MaximumNArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
Expand All @@ -53,19 +59,19 @@ var buildAbortCmd = &cobra.Command{
if len(args) < 1 {
args = append(args, "")
}
return client.AbortBuild(cfg.Project, cfg.Service, args[0])
return client.AbortBuild(cfg.Project(), cfg.Service(), args[0])
}),
}

var logsFollow = false

var buildLogsCmd = &cobra.Command{
Use: "logs [task]",
Use: "logs [buildid]",
Short: "Show the build logs of the given task",
Args: cobra.MaximumNArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
Expand All @@ -77,7 +83,7 @@ var buildLogsCmd = &cobra.Command{
if len(args) > 0 {
prefix = args[0]
}
builds, err := client.ListBuilds(cfg.Project, cfg.Service, prefix)
builds, err := client.ListBuilds(cfg.Project(), cfg.Service(), prefix)
if err != nil {
return err
}
Expand All @@ -88,19 +94,19 @@ var buildLogsCmd = &cobra.Command{
sort.Slice(builds, func(i, j int) bool { return builds[i].CreatedAt.After(builds[j].CreatedAt) })
latestBuildID := builds[0].ID
if logsFollow {
return client.StreamBuildLogs(cfg.Project, cfg.Service, latestBuildID, os.Stdout)
return client.StreamBuildLogs(cfg.Project(), cfg.Service(), latestBuildID, os.Stdout)
}
return client.ShowBuildLogs(cfg.Project, cfg.Service, latestBuildID, os.Stdout)
return client.ShowBuildLogs(cfg.Project(), cfg.Service(), latestBuildID, os.Stdout)
}),
}

var inspectCmd = &cobra.Command{
var buildInspectCmd = &cobra.Command{
Use: "inspect [prefix]",
Short: "Inspect the first matched task with the given ID prefix",
Args: cobra.ExactArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
Expand All @@ -111,13 +117,13 @@ var inspectCmd = &cobra.Command{
}),
}

var statusCmd = &cobra.Command{
var buildStatusCmd = &cobra.Command{
Use: "status [buildid]",
Short: "Show the status of the given build",
Args: cobra.ExactArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
Expand All @@ -129,8 +135,8 @@ var statusCmd = &cobra.Command{
}),
}

func listBuilds(client *api.Client, cfg *config.ServiceConfig, id string) error {
builds, err := client.ListBuilds(cfg.Project, cfg.Service, id)
func listBuilds(client *api.Client, cfg config.ServiceConfig, id string) error {
builds, err := client.ListBuilds(cfg.Project(), cfg.Service(), id)
if err != nil {
return err
}
Expand All @@ -151,8 +157,8 @@ func listBuilds(client *api.Client, cfg *config.ServiceConfig, id string) error
return nil
}

func showBuildStatusAndExit(client *api.Client, cfg *config.ServiceConfig, id string) error {
build, err := client.InspectBuild(cfg.Project, cfg.Service, id)
func showBuildStatusAndExit(client *api.Client, cfg config.ServiceConfig, id string) error {
build, err := client.InspectBuild(cfg.Project(), cfg.Service(), id)
if err != nil {
return err
}
Expand All @@ -161,8 +167,8 @@ func showBuildStatusAndExit(client *api.Client, cfg *config.ServiceConfig, id st
return nil
}

func inspectBuild(client *api.Client, cfg *config.ServiceConfig, id string) error {
build, err := client.InspectBuild(cfg.Project, cfg.Service, id)
func inspectBuild(client *api.Client, cfg config.ServiceConfig, id string) error {
build, err := client.InspectBuild(cfg.Project(), cfg.Service(), id)
if err != nil {
return err
}
Expand All @@ -180,13 +186,13 @@ func inspectBuild(client *api.Client, cfg *config.ServiceConfig, id string) erro
return nil
}

func deployBuild(client *api.Client, cfg *config.ServiceConfig, id string) error {
func deployBuild(client *api.Client, cfg config.ServiceConfig, id string) error {
var deployReq api.DeployRequest
deployReq.Build = id
for _, kv := range cfg.Deployment.Environment {
for _, kv := range cfg.Deployment().Environment {
deployReq.Environment = append(deployReq.Environment, api.KVPair(kv))
}
deployment, err := client.SubmitDeploy(cfg.Project, cfg.Service, &deployReq)
deployment, err := client.SubmitDeploy(cfg.Project(), cfg.Service(), &deployReq)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,10 +225,8 @@ func colorize(status string) string {
}

func initBuildsCmd() {
buildLogsCmd.PersistentFlags().BoolVarP(&logsFollow, "follow", "f", false, "follow the logs")
buildCmd.AddCommand(inspectCmd)
buildCmd.AddCommand(buildLogsCmd)
buildCmd.AddCommand(buildAbortCmd)
buildCmd.AddCommand(statusCmd)
buildCmd.PersistentFlags().StringVarP(&buildService, "service", "s", "", "The service to inspect for builds")
buildLogsCmd.PersistentFlags().BoolVarP(&logsFollow, "follow", "f", false, "Follow the logs")
buildCmd.AddCommand(buildListCmd, buildInspectCmd, buildLogsCmd, buildAbortCmd, buildStatusCmd)
rootCmd.AddCommand(buildCmd)
}
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/juju/ansiterm"
"github.com/spf13/cobra"
"github.com/valar/cli/config"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ var configEndpointSetCmd = &cobra.Command{
ep.URL = configEndpointSetUrl
}
if globalConfiguration.Endpoints == nil {
globalConfiguration.Endpoints = make(map[string]valarEndpoint)
globalConfiguration.Endpoints = map[string]config.APIEndpoint{}
}
globalConfiguration.Endpoints[args[0]] = ep
if err := globalConfiguration.Write(); err != nil {
Expand Down
43 changes: 23 additions & 20 deletions cmd/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"strings"
"text/tabwriter"

"github.com/valar/cli/config"

"github.com/valar/cli/api"

"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/valar/cli/api"
"github.com/valar/cli/config"
)

var (
Expand All @@ -20,16 +18,21 @@ var (
cronCmd = &cobra.Command{
Use: "cron",
Short: "Manage scheduled invocations of a service",
}

cronListCmd = &cobra.Command{
Use: "list",
Short: "List all cron schedules for a service",
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
if err != nil {
return err
}
schedules, err := client.ListSchedules(cfg.Project, cfg.Service)
schedules, err := client.ListSchedules(cfg.Project(), cfg.Service())
if err != nil {
return err
}
Expand All @@ -50,15 +53,15 @@ var (
Short: "Add or edit a service invocation schedule",
Args: cobra.ExactArgs(2),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
if err != nil {
return err
}
if err := client.AddSchedule(cfg.Project, cfg.Service, api.Schedule{
if err := client.AddSchedule(cfg.Project(), cfg.Service(), api.Schedule{
Name: args[0],
Timespec: args[1],
Payload: cronAddPayload,
Expand All @@ -75,15 +78,15 @@ var (
Short: "Manually triggers a scheduled invocation",
Args: cobra.ExactArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
if err != nil {
return err
}
if err := client.TriggerSchedule(cfg.Project, cfg.Service, args[0]); err != nil {
if err := client.TriggerSchedule(cfg.Project(), cfg.Service(), args[0]); err != nil {
return err
}
return nil
Expand All @@ -94,15 +97,15 @@ var (
Short: "Remove a service invocation schedule",
Args: cobra.ExactArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
if err != nil {
return err
}
if err := client.RemoveSchedule(cfg.Project, cfg.Service, args[0]); err != nil {
if err := client.RemoveSchedule(cfg.Project(), cfg.Service(), args[0]); err != nil {
return err
}
return nil
Expand All @@ -113,15 +116,15 @@ var (
Short: "Inspect the invocation history of a service schedule",
Args: cobra.ExactArgs(1),
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
cfg := &config.ServiceConfig{}
if err := cfg.ReadFromFile(functionConfiguration); err != nil {
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &cronService, globalConfiguration)
if err != nil {
return err
}
client, err := globalConfiguration.APIClient()
if err != nil {
return err
}
invocations, err := client.InspectSchedule(cfg.Project, cfg.Service, args[0])
invocations, err := client.InspectSchedule(cfg.Project(), cfg.Service(), args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +156,7 @@ var (
func initCronCmd() {
rootCmd.AddCommand(cronCmd)
cronCmd.PersistentFlags().StringVarP(&cronService, "service", "s", "", "The service to manage cron schedules for")
cronCmd.AddCommand(cronAddCmd, cronTriggerCmd, cronRemoveCmd, cronInspectCmd)
cronCmd.AddCommand(cronListCmd, cronAddCmd, cronTriggerCmd, cronRemoveCmd, cronInspectCmd)
cronAddCmd.Flags().StringVar(&cronAddPath, "path", "/", "The service path to send a request to")
cronAddCmd.Flags().StringVar(&cronAddPayload, "payload", "", "The body payload to send in a request")
}
Loading

0 comments on commit c4133df

Please sign in to comment.