Skip to content

Commit

Permalink
Refactor to use urfav/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dcondomitti committed Feb 13, 2019
1 parent 0ef018a commit ca9a1bb
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 230 deletions.
46 changes: 0 additions & 46 deletions cmd/event.go

This file was deleted.

75 changes: 0 additions & 75 deletions cmd/execute.go

This file was deleted.

18 changes: 18 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"log"
"os"

"github.com/firehydrant/fhcli/pkg/cli"
)

func main() {
app := cli.NewApp()

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}

}
74 changes: 0 additions & 74 deletions cmd/root.go

This file was deleted.

23 changes: 0 additions & 23 deletions cmd/version.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/urfave/cli v1.20.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
9 changes: 0 additions & 9 deletions main.go

This file was deleted.

33 changes: 33 additions & 0 deletions pkg/api_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package api_client

import (
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"

fhclient "github.com/firehydrant/api-client-go/client"
transport "github.com/go-openapi/runtime/client"
)

type Config struct {
ApiHost string
ApiKey string
Debug bool
}

type ApiClient struct {
transport *transport.Runtime
Client *fhclient.FireHydrant
Auth runtime.ClientAuthInfoWriter
}

func NewApiClient(c Config) ApiClient {
client := ApiClient{}

client.transport = transport.New(c.ApiHost, "", []string{"http"})
client.transport.Debug = c.Debug

client.Client = fhclient.New(client.transport, strfmt.Default)
client.Auth = transport.BearerToken(c.ApiKey)

return client
}
22 changes: 19 additions & 3 deletions cmd/change_event.go → pkg/change_events/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd
package events

import (
"strings"
"time"

apiclient "github.com/firehydrant/fhcli/pkg/api_client"

"github.com/firehydrant/api-client-go/client/changes"
"github.com/firehydrant/api-client-go/models"
"github.com/go-openapi/strfmt"
Expand All @@ -13,7 +15,9 @@ type ChangeEvent struct {
Environment string
Service string
RawIdentities string
RawLabels string
Identities map[string]string
Labels map[string]string
StartsAt time.Time
EndsAt time.Time
Summary string
Expand All @@ -22,6 +26,7 @@ type ChangeEvent struct {
func NewChangeEvent() ChangeEvent {
return ChangeEvent{
Identities: make(map[string]string),
Labels: make(map[string]string),
StartsAt: time.Now(),
EndsAt: time.Now(),
}
Expand Down Expand Up @@ -49,9 +54,19 @@ func (ce *ChangeEvent) parseIdentities() {
}
}

func (ce *ChangeEvent) Submit() (string, error) {
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]
}
}
}

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

envList := []string{}
if len(ce.Environment) > 0 {
Expand All @@ -70,9 +85,10 @@ func (ce *ChangeEvent) Submit() (string, error) {
EndsAt: strfmt.DateTime(ce.EndsAt),
Summary: &ce.Summary,
ChangeIdentities: ce.identitiesToAPI(),
Labels: ce.Labels,
}

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

if err != nil {
return "", err
Expand Down
30 changes: 30 additions & 0 deletions pkg/cli/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cli

import (
"fmt"
"strings"

changeevents "github.com/firehydrant/fhcli/pkg/change_events"
"github.com/urfave/cli"
)

func eventCmd(c *cli.Context) error {
client := NewApiClient(c)

ce := changeevents.NewChangeEvent()

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")

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

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

return nil
}
Loading

0 comments on commit ca9a1bb

Please sign in to comment.