Skip to content

Commit

Permalink
Refactor to use API helpers moved into api-client-go
Browse files Browse the repository at this point in the history
  • Loading branch information
dcondomitti committed Feb 15, 2019
1 parent 8acc5c9 commit d423f75
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 175 deletions.
15 changes: 0 additions & 15 deletions go.mod

This file was deleted.

33 changes: 0 additions & 33 deletions pkg/api_client/main.go

This file was deleted.

94 changes: 0 additions & 94 deletions pkg/change_events/main.go

This file was deleted.

31 changes: 21 additions & 10 deletions pkg/cli/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package cli
import (
"fmt"
"strings"
"time"

changeevents "github.com/firehydrant/fhcli/pkg/change_events"
"github.com/firehydrant/api-client-go/client/changes"
"github.com/firehydrant/api-client-go/fhclient"
"github.com/firehydrant/api-client-go/models"
"github.com/go-openapi/strfmt"
"github.com/urfave/cli"
)

Expand All @@ -14,20 +18,27 @@ func eventCmd(c *cli.Context) error {
return err
}

ce := changeevents.NewChangeEvent()
params := changes.NewPostV1ChangesEventsParams()
summary := strings.Join(c.Args(), " ")

ce.Summary = strings.Join(c.Args(), " ")
ce.Environment = c.String("environment")
ce.Service = c.String("service")
ce.RawIdentities = c.String("identities")
ce.RawLabels = c.String("labels")
identities := fhclient.APIKVtoChangeIdentities(fhclient.MapToAPIKV(fhclient.ParseKV(c.String("identities"))))

params.V1ChangesEvents = &models.PostV1ChangesEvents{
Environments: fhclient.ParamToList(c.String("environment")),
Services: fhclient.ParamToList(c.String("service")),
StartsAt: strfmt.DateTime(time.Now()),
EndsAt: strfmt.DateTime(time.Now()),
Summary: &summary,
ChangeIdentities: identities,
Labels: fhclient.ParseKV(c.String("labels")),
}

resp, err := client.Client.Changes.PostV1ChangesEvents(params, client.Auth)

id, err := ce.Submit(client)
if err != nil {
return err
}

fmt.Println(fmt.Sprintf("Created event %s", id))

fmt.Println(fmt.Sprintf("Created change event %s", resp.Payload.ID))
return nil
}
49 changes: 32 additions & 17 deletions pkg/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"strings"
"time"

changeevents "github.com/firehydrant/fhcli/pkg/change_events"
"github.com/firehydrant/api-client-go/client/changes"
"github.com/firehydrant/api-client-go/fhclient"
"github.com/firehydrant/api-client-go/models"
"github.com/go-openapi/strfmt"
"github.com/urfave/cli"
)

Expand All @@ -22,41 +25,53 @@ func executeCmd(c *cli.Context) error {
return errors.New("No command passed")
}

ce := changeevents.NewChangeEvent()
params := changes.NewPostV1ChangesEventsParams()
command, flags := c.Args()[0], c.Args()[1:]

ce.Summary = strings.Join(c.Args(), " ")
ce.Environment = c.String("environment")
ce.Service = c.String("service")
ce.RawIdentities = c.String("identities")
ce.RawLabels = c.String("labels")
identities := fhclient.ParseKV(c.String("identities"))

command, flags := c.Args()[0], c.Args()[1:]
params.V1ChangesEvents = &models.PostV1ChangesEvents{
Environments: fhclient.ParamToList(c.String("environment")),
Services: fhclient.ParamToList(c.String("service")),
StartsAt: strfmt.DateTime(time.Now()),
Summary: &command,
Labels: fhclient.ParseKV(c.String("labels")),
}

fmt.Println(fmt.Sprintf("Executing command %s with args %s", command, strings.Join(flags, " ")))
cmdExec := exec.Command(command, flags...)

// We don't actually want to capture this; pass it through to the calling shell
cmdExec.Stdout = os.Stdout
cmdExec.Stderr = os.Stderr
ce.StartsAt = time.Now()
err = cmdExec.Run()
duration := time.Since(ce.StartsAt) / time.Millisecond
ce.EndsAt = time.Now()
start := time.Now()

cErr := cmdExec.Run()

duration := time.Since(start) / time.Millisecond
params.V1ChangesEvents.EndsAt = strfmt.DateTime(time.Now())
params.V1ChangesEvents.StartsAt = strfmt.DateTime(start)

// getting the exit code is a pita so i'm not doing it yet
if err != nil {
ce.Identities["error"] = err.Error()
fmt.Println(fmt.Sprintf("Executed command, duration %dms, error: %s", duration, err))
if cErr != nil {
identities["error"] = err.Error()
fmt.Println(fmt.Sprintf("Executed command, duration %dms, error: %s", duration, cErr))
} else {
fmt.Println(fmt.Sprintf("Executed command, duration %dms", duration))
}

id, err := ce.Submit(client)
params.V1ChangesEvents.ChangeIdentities = fhclient.APIKVtoChangeIdentities(fhclient.MapToAPIKV(identities))
resp, err := client.Client.Changes.PostV1ChangesEvents(params, client.Auth)

if err != nil {
return err
}

fmt.Println(fmt.Sprintf("Created event %s", id))
fmt.Println(fmt.Sprintf("Created change event %s", resp.Payload.ID))

if cErr != nil {
return cErr
}

return nil
}
12 changes: 6 additions & 6 deletions pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

apiclient "github.com/firehydrant/fhcli/pkg/api_client"
"github.com/firehydrant/api-client-go/fhclient"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -76,20 +76,20 @@ func NewApp(commit string, version string) *cli.App {
return app
}

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

if len(config.ApiHost) == 0 {
return apiclient.ApiClient{}, errors.New("Invalid or no API host provided")
return fhclient.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 fhclient.ApiClient{}, errors.New("Invalid or no API key provided")
}

return apiclient.NewApiClient(config), nil
return fhclient.NewApiClient(config), nil
}

0 comments on commit d423f75

Please sign in to comment.