Skip to content

Commit

Permalink
Changed from flags to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleimp committed Jul 29, 2020
1 parent fc580ba commit c5bfa5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 63 deletions.
41 changes: 41 additions & 0 deletions cmd/cli/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"errors"

"github.com/apex/log"
"github.com/urfave/cli/v2"
"github.com/wesleimp/unleash-checkr/internal/check"
"github.com/wesleimp/unleash-checkr/pkg/config"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

func runCheck(c *cli.Context) error {
expires := c.Int("expires")

url, err := checkArgs(c.Args())
if err != nil {
return err
}

ctx := context.New(&config.Config{
URL: url,
Expires: expires,
})

err = check.Start(ctx)
if err != nil {
log.WithError(err).Error("Error checking flags")
return err
}

return nil
}

func checkArgs(args cli.Args) (string, error) {
if args.Len() == 0 {
return "", errors.New("check command requires exactly 1 argument. See --help")
}

return args.Get(0), nil
}
72 changes: 15 additions & 57 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package cli

import (
"fmt"

"github.com/apex/log"
clih "github.com/apex/log/handlers/cli"
"github.com/urfave/cli/v2"

"github.com/wesleimp/unleash-checkr/internal/check"
"github.com/wesleimp/unleash-checkr/pkg/config"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

// Run starts the app
Expand All @@ -18,58 +10,24 @@ func Run(version string, args []string) error {
Name: "unleash-checkr",
Usage: "Checks if any flags have expired and notifies somewhere",
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "unleash url",
Required: true,
},
&cli.IntFlag{
Name: "expires",
Aliases: []string{"e"},
Usage: "expires after days",
Value: 7,
},
&cli.StringFlag{
Name: "slack-channel",
Usage: "slack notification channel",
},
&cli.StringFlag{
Name: "slack-token",
Usage: "slack token",
EnvVars: []string{"SLACK_TOKEN"},
Commands: []*cli.Command{
{
Name: "check",
Aliases: []string{"c"},
Usage: "checks the flags and print on the console",
ArgsUsage: "URL",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "expires",
Aliases: []string{"e"},
Usage: "expires after days",
Value: 7,
},
},
Action: runCheck,
},
},
Action: run,
}

return app.Run(args)
}

func run(c *cli.Context) error {
url := c.String("url")
expires := c.Int("expires")
slackChannel := c.String("slack-channel")
slackToken := c.String("slack-token")

log.SetHandler(clih.Default)

fmt.Println()
defer fmt.Println()

ctx := context.New(&config.Config{
URL: url,
Expires: expires,
SlackChannel: slackChannel,
SlackToken: slackToken,
})

err := check.Start(ctx)
if err != nil {
log.WithError(err).Error("Error checking flags")
return err
}

return nil
}
7 changes: 3 additions & 4 deletions internal/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ func Start(ctx *context.Context) error {
fmt.Println()

for _, f := range ff {
name := color.New(color.Bold).Sprintf("Name:")
descripion := color.New(color.Bold).Sprint("Description:")
createdAt := color.New(color.Bold).Sprint("Created at:")

log.Info(fmt.Sprintf("%s %s", name, f.Name))
log.Info(fmt.Sprintf("%s %s", descripion, f.Description))
log.Info(color.New(color.Bold, color.Green).Sprintf(f.Name))
log.Info(f.Description)
log.Info(fmt.Sprintf("%s %v", createdAt, f.CreatedAt))
log.Info(fmt.Sprintf("%s/#/features/strategies/%s", ctx.Config.URL, f.Name))
fmt.Println()
}

Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"log"
"fmt"
"os"

"github.com/apex/log"
clih "github.com/apex/log/handlers/cli"
"github.com/wesleimp/unleash-checkr/cmd/cli"
)

Expand All @@ -12,8 +14,13 @@ var (
)

func main() {
log.SetHandler(clih.Default)

fmt.Println()
defer fmt.Println()

err := cli.Run(version, os.Args)
if err != nil {
log.Fatal(err)
log.Fatal(err.Error())
}
}

0 comments on commit c5bfa5a

Please sign in to comment.