Skip to content

Commit

Permalink
fix crash when environemnt variable MACKEREL_APIKEY is used
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 15, 2018
1 parent 7673888 commit 0e6e24a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cmd/mkrg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func run(args []string) error {
func setupClientHostID(ctx *cli.Context) (*mackerel.Client, string, error) {
var conf *config.Config
var err error
apiKey := os.Getenv("MACKEREL_APIKEY")
var apiKey, apiBase string

apiKey = os.Getenv("MACKEREL_APIKEY")
if apiKey == "" {
confFile := config.DefaultConfig.Conffile
conf, err = config.LoadConfig(confFile)
Expand All @@ -76,22 +78,24 @@ func setupClientHostID(ctx *cli.Context) (*mackerel.Client, string, error) {
if apiKey == "" {
return nil, "", errors.New("MACKEREL_APIKEY not set")
}
apiBase = conf.Apibase
}
apiBase := conf.Apibase
if apiBase == "" {
apiBase = config.DefaultConfig.Apibase
}
client, err := mackerel.NewClientWithOptions(apiKey, apiBase, false)
if err != nil {
return nil, "", err
}

hostID := ctx.GlobalString("host")
if hostID == "" {
hostID, err = loadHostID(conf.Root)
if err != nil {
return nil, "", errors.New("specify host id")
}
}

return client, hostID, nil
}

Expand Down

0 comments on commit 0e6e24a

Please sign in to comment.