Skip to content

Commit

Permalink
DRY up identity/label parsing and fail on missing API key
Browse files Browse the repository at this point in the history
  • Loading branch information
dcondomitti committed Feb 14, 2019
1 parent 502bd91 commit 8bf9f7f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
19 changes: 5 additions & 14 deletions pkg/change_events/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,19 @@ func (ce *ChangeEvent) identitiesToAPI() []*models.PostV1ChangesEventsChangeIden
return ciItems
}

func (ce *ChangeEvent) parseIdentities() {
for _, identity := range strings.Split(ce.RawIdentities, ",") {
func (ce *ChangeEvent) parseKV(raw string, target map[string]string) {
for _, identity := range strings.Split(raw, ",") {
iParts := strings.Split(identity, "=")
if len(iParts) > 1 {
ce.Identities[iParts[0]] = iParts[1]
}
}
}

func (ce *ChangeEvent) parseLabels() {
for _, label := range strings.Split(ce.RawLabels, ",") {
lParts := strings.Split(label, "=")
if len(lParts) > 1 {
ce.Labels[lParts[0]] = lParts[1]
target[iParts[0]] = iParts[1]
}
}
}

func (ce *ChangeEvent) Submit(client apiclient.ApiClient) (string, error) {
c := changes.NewPostV1ChangesEventsParams()
ce.parseIdentities()
ce.parseLabels()
ce.parseKV(ce.RawIdentities, ce.Identities)
ce.parseKV(ce.RawLabels, ce.Labels)

envList := []string{}
if len(ce.Environment) > 0 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/cli/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
)

func eventCmd(c *cli.Context) error {
client := NewApiClient(c)
client, err := NewApiClient(c)
if err != nil {
return err
}

ce := changeevents.NewChangeEvent()

Expand Down
7 changes: 5 additions & 2 deletions pkg/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func executeCmd(c *cli.Context) error {
client := NewApiClient(c)
client, err := NewApiClient(c)
if err != nil {
return err
}

if len(c.Args()) < 1 {
return errors.New("No command passed")
Expand All @@ -36,7 +39,7 @@ func executeCmd(c *cli.Context) error {
cmdExec.Stdout = os.Stdout
cmdExec.Stderr = os.Stderr
ce.StartsAt = time.Now()
err := cmdExec.Run()
err = cmdExec.Run()
duration := time.Since(ce.StartsAt) / time.Millisecond
ce.EndsAt = time.Now()

Expand Down
16 changes: 13 additions & 3 deletions pkg/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"errors"

apiclient "github.com/firehydrant/fhcli/pkg/api_client"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -43,7 +45,7 @@ func NewApp() *cli.App {
Value: "api.firehydrant.io",
EnvVar: "FH_API_HOST",
},
cli.BoolTFlag{
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debug output for API calls",
},
Expand Down Expand Up @@ -71,12 +73,20 @@ func NewApp() *cli.App {
return app
}

func NewApiClient(c *cli.Context) apiclient.ApiClient {
func NewApiClient(c *cli.Context) (apiclient.ApiClient, error) {
config := apiclient.Config{
ApiHost: c.GlobalString("api-host"),
ApiKey: c.GlobalString("api-key"),
Debug: c.GlobalBool("debug"),
}

return apiclient.NewApiClient(config)
if len(config.ApiHost) == 0 {
return apiclient.ApiClient{}, errors.New("Invalid or no API host provided")
}

if len(config.ApiKey) == 0 {
return apiclient.ApiClient{}, errors.New("Invalid or no API key provided")
}

return apiclient.NewApiClient(config), nil
}

0 comments on commit 8bf9f7f

Please sign in to comment.