Skip to content

Commit

Permalink
Add init command for writing config
Browse files Browse the repository at this point in the history
  • Loading branch information
dcondomitti committed Feb 22, 2019
1 parent 1337334 commit 491a24a
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 45 deletions.
68 changes: 68 additions & 0 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cli

import (
"fmt"
"io/ioutil"

"gopkg.in/urfave/cli.v1"
"gopkg.in/yaml.v2"
)

type configFile struct {
APIKey string `yaml:"apiKey"`
APIHost string `yaml:"apiHost"`
Debug bool `yaml:"debug"`
Identities string `yaml:"identities"`
Labels string `yaml:"labels"`
Environment string `yaml:"environment"`
Service string `yaml:"service"`
}

func initCmd(c *cli.Context) error {

out := c.GlobalString("config")
if out == "" {
out = "/tmp/firehydrant.cfg"
}

config := configFile{}
if len(c.GlobalString("apiKey")) > 0 {
config.APIKey = c.GlobalString("apiKey")
}

if len(c.GlobalString("apiHost")) > 0 {
config.APIHost = c.GlobalString("apiHost")
}

config.Debug = c.GlobalBool("debug")

if len(c.String("identities")) > 0 {
config.Identities = c.String("identities")
}

if len(c.String("labels")) > 0 {
config.Labels = c.String("labels")
}

if len(c.String("environment")) > 0 {
config.Environment = c.String("environment")
}

if len(c.String("service")) > 0 {
config.Service = c.String("service")
}

d, err := yaml.Marshal(config)

if err != nil {
return err
}

err = ioutil.WriteFile(out, d, 0644)
if err != nil {
return err
}

fmt.Println(fmt.Sprintf("Wrote config file %s", out))
return nil
}
122 changes: 77 additions & 45 deletions pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cli
import (
"errors"
"fmt"
"os"
"path"

"github.com/davecgh/go-spew/spew"
"github.com/firehydrant/api-client-go/fhclient"
"gopkg.in/urfave/cli.v1"
"gopkg.in/urfave/cli.v1/altsrc"
Expand Down Expand Up @@ -33,66 +34,60 @@ var sharedFlags = []cli.Flag{
}),
}

var flags = []cli.Flag{
altsrc.NewStringFlag(cli.StringFlag{
Name: "apiKey",
Usage: "firehydrant.io API Key",
EnvVar: "FH_API_KEY",
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "apiHost",
Usage: "firehydrant.io API hostname",
Value: "api.firehydrant.io",
EnvVar: "FH_API_HOST",
}),
altsrc.NewBoolFlag(cli.BoolFlag{
Name: "debug",
Usage: "Enable debug output for API calls",
}),
altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "ignoreErrors",
Usage: "exit 0 on errors from FH API (default)",
}),
cli.StringFlag{
Name: "config",
Usage: "config file",
EnvVar: "FH_CONFIG_FILE",
},
}

func NewApp(commit string, version string) *cli.App {
app := cli.NewApp()

flags := []cli.Flag{
altsrc.NewStringFlag(cli.StringFlag{
Name: "apiKey",
Usage: "firehydrant.io API Key",
EnvVar: "FH_API_KEY",
}),
altsrc.NewStringFlag(cli.StringFlag{
Name: "apiHost",
Usage: "firehydrant.io API hostname",
Value: "api.firehydrant.io",
EnvVar: "FH_API_HOST",
}),
altsrc.NewBoolFlag(cli.BoolFlag{
Name: "debug",
Usage: "Enable debug output for API calls",
}),
altsrc.NewBoolTFlag(cli.BoolTFlag{
Name: "ignoreErrors",
Usage: "exit 0 on errors from FH API (default)",
}),
cli.StringFlag{
Name: "config",
Usage: "config file",
EnvVar: "FH_CONFIG_FILE",
},
}

app.Commands = []cli.Command{
{
Name: "init",
Usage: "write a config file to be used in future invocations of the FireHydrant CLI",
Action: initCmd,
Flags: sharedFlags,
},
{
Name: "event",
Usage: "submit an event to the FireHydrant API",
Action: eventCmd,
Flags: sharedFlags,
Before: func(c *cli.Context) error {
s, err := altsrc.NewYamlSourceFromFile(c.GlobalString("config"))
if err != nil {
return err
}
return altsrc.ApplyInputSourceValues(c, s, sharedFlags)
},
Before: parseConfigFile,
},
{
Name: "execute",
Usage: "execute a command and submit metrics to the FireHydrant API",
Action: executeCmd,
Flags: sharedFlags,
Before: func(c *cli.Context) error {
s, err := altsrc.NewYamlSourceFromFile(c.GlobalString("config"))
if err != nil {
return err
}
return altsrc.ApplyInputSourceValues(c, s, sharedFlags)
},
Before: parseConfigFile,
},
}

app.Before = altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config"))
app.Before = parseConfigFile
app.Flags = flags
app.Version = fmt.Sprintf("%s (%s)", version, commit)

Expand All @@ -105,10 +100,9 @@ func NewApiClient(c *cli.Context) (fhclient.ApiClient, error) {
ApiKey: c.GlobalString("apiKey"),
Debug: c.GlobalBool("debug"),
}
spew.Dump(config)

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

if len(config.ApiKey) == 0 {
Expand All @@ -117,3 +111,41 @@ func NewApiClient(c *cli.Context) (fhclient.ApiClient, error) {

return fhclient.NewApiClient(config), nil
}

func parseConfigFile(c *cli.Context) error {
// Ignore the empty or missing configuration file if we're creating one
if c.Args()[0] == "init" {
return nil
}

paths := []string{
c.GlobalString("config"),
path.Join("/etc", "firehydrant.cfg"),
path.Join(os.Getenv("HOME"), "firehydrant.cfg"),
path.Join("/tmp", "firehydrant.cfg"),
}

p := ""
for _, pa := range paths {
if _, err := os.Stat(pa); err == nil {
p = pa
break
}
}

if p != "" {
s, err := altsrc.NewYamlSourceFromFile(p)
if err != nil {
return err
}

err = altsrc.ApplyInputSourceValues(c, s, flags)
if err != nil {
return nil
}

return altsrc.ApplyInputSourceValues(c, s, sharedFlags)
}

return nil
}

0 comments on commit 491a24a

Please sign in to comment.