Skip to content

Commit

Permalink
migrate to urfave/cli/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwrfuller committed Jan 13, 2025
1 parent 0f4a78e commit aba1cfa
Show file tree
Hide file tree
Showing 94 changed files with 10,620 additions and 4,197 deletions.
47 changes: 25 additions & 22 deletions v2/ahoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sort"
"strings"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)

Expand All @@ -22,7 +22,7 @@ import (
type Config struct {
Usage string
AhoyAPI string
Commands map[string]Command
Commands map[string]*Command
Entrypoint []string
Env string
}
Expand Down Expand Up @@ -136,12 +136,12 @@ func getConfig(file string) (Config, error) {
return config, err
}

func getSubCommands(includes []string) []cli.Command {
subCommands := []cli.Command{}
func getSubCommands(includes []string) []*cli.Command {
subCommands := []*cli.Command{}
if len(includes) == 0 {
return subCommands
}
commands := map[string]cli.Command{}
commands := map[string]*cli.Command{}
for _, include := range includes {
if len(include) == 0 {
continue
Expand Down Expand Up @@ -197,8 +197,8 @@ func getEnvironmentVars(envFile string) []string {
return envVars
}

func getCommands(config Config) []cli.Command {
exportCmds := []cli.Command{}
func getCommands(config Config) []*cli.Command {
exportCmds := []*cli.Command{}
envVars := []string{}

// Get environment variables from the 'global' environment variable file, if it is defined.
Expand Down Expand Up @@ -231,7 +231,7 @@ func getCommands(config Config) []cli.Command {
logger("fatal", "Command ["+name+"] has 'imports' set, but it is empty. Check your yaml file.")
}

newCmd := cli.Command{
newCmd := &cli.Command{
Name: name,
Aliases: cmd.Aliases,
SkipFlagParsing: true,
Expand All @@ -243,7 +243,7 @@ func getCommands(config Config) []cli.Command {
}

if cmd.Cmd != "" {
newCmd.Action = func(c *cli.Context) {
newCmd.Action = func(c *cli.Context) error {
// For some unclear reason, if we don't add an item at the end here,
// the first argument is skipped... actually it's not!
// 'bash -c' says that arguments will be passed starting with $0, which also means that
Expand All @@ -253,7 +253,7 @@ func getCommands(config Config) []cli.Command {
var cmdEntrypoint []string

// c.Args() is not a slice apparently.
for _, arg := range c.Args() {
for _, arg := range c.Args().Slice() {
if arg != "--" {
cmdArgs = append(cmdArgs, arg)
}
Expand Down Expand Up @@ -292,6 +292,7 @@ func getCommands(config Config) []cli.Command {
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
return nil
}
}

Expand All @@ -314,18 +315,18 @@ func getCommands(config Config) []cli.Command {
return exportCmds
}

func addDefaultCommands(commands []cli.Command) []cli.Command {
func addDefaultCommands(commands []*cli.Command) []*cli.Command {

defaultInitCmd := cli.Command{
defaultInitCmd := &cli.Command{
Name: "init",
Usage: "Initialize a new .ahoy.yml config file in the current directory.",
Flags: []cli.Flag{
cli.BoolFlag{
&cli.BoolFlag{
Name: "force",
Usage: "force overwriting the .ahoy.yml file in the current directory.",
},
},
Action: func(c *cli.Context) {
Action: func(c *cli.Context) error {
if fileExists(filepath.Join(".", ".ahoy.yml")) {
if c.Bool("force") {
fmt.Println("Warning: '--force' parameter passed, overwriting .ahoy.yml in current directory.")
Expand All @@ -343,7 +344,7 @@ func addDefaultCommands(commands []cli.Command) []cli.Command {
fmt.Println("Abort: exiting without overwriting.")
os.Exit(0)
}
if len(c.Args()) > 0 {
if c.Args().Len() > 0 {
fmt.Println("Ok, overwriting .ahoy.yml in current directory with specified file.")
} else {
fmt.Println("Ok, overwriting .ahoy.yml in current directory with example file.")
Expand All @@ -354,8 +355,8 @@ func addDefaultCommands(commands []cli.Command) []cli.Command {
// Allows users to define their own files to call to init.
// TODO: Make file downloading OS-independent.
var wgetURL = "https://raw.githubusercontent.com/ahoy-cli/ahoy/master/examples/examples.ahoy.yml"
if len(c.Args()) > 0 {
wgetURL = c.Args()[0]
if c.Args().Len() > 0 {
wgetURL = c.Args().Get(0)
}
grabYaml := "wget " + wgetURL + " -O .ahoy.yml"
cmd := exec.Command("bash", "-c", grabYaml)
Expand All @@ -365,12 +366,13 @@ func addDefaultCommands(commands []cli.Command) []cli.Command {
fmt.Fprintln(os.Stderr)
os.Exit(1)
} else {
if len(c.Args()) > 0 {
if c.Args().Len() > 0 {
fmt.Println("Your specified .ahoy.yml has been downloaded to the current directory.")
} else {
fmt.Println("Example .ahoy.yml downloaded to the current directory. You can customize it to suit your needs!")
}
}
return nil
},
}

Expand Down Expand Up @@ -407,10 +409,10 @@ func BashComplete(c *cli.Context) {
// NoArgsAction is the application wide default action, for when no flags or arguments
// are passed or when a command doesn't exist.
// Looks like -f flag still works through here though.
func NoArgsAction(c *cli.Context) {
func NoArgsAction(c *cli.Context) error {
args := c.Args()
if len(args) > 0 {
msg := "Command not found for '" + strings.Join(args, " ") + "'"
if args.Len() > 0 {
msg := "Command not found for '" + strings.Join(args.Slice(), " ") + "'"
logger("fatal", msg)
}

Expand All @@ -427,6 +429,7 @@ func NoArgsAction(c *cli.Context) {

// Exit gracefully if we get to here.
os.Exit(0)
return nil
}

// BeforeCommand runs before every command so arguments or flags must be passed
Expand All @@ -438,7 +441,7 @@ func BeforeCommand(c *cli.Context) error {
return errors.New("don't continue with commands")
}
if c.Bool("help") {
if len(args) > 0 {
if args.Len() > 0 {
cli.ShowCommandHelp(c, args.First())
} else {
cli.ShowAppHelp(c)
Expand Down
4 changes: 2 additions & 2 deletions v2/ahoy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetCommands(t *testing.T) {
config := Config{
Usage: "Test getSubCommands Usage.",
AhoyAPI: "v2",
Commands: map[string]Command{
Commands: map[string]*Command{
"test-command": {
Description: "Testing example Command.",
Usage: "test-command a",
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestGetConfig(t *testing.T) {
expected := Config{
Usage: "Test example usage.",
AhoyAPI: "v2",
Commands: map[string]Command{
Commands: map[string]*Command{
"test-command": {
Description: "Testing example Command.",
Usage: "test-command",
Expand Down
23 changes: 13 additions & 10 deletions v2/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package main
import (
"flag"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

var globalFlags = []cli.Flag{
cli.BoolFlag{
Name: "verbose, v",
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Output extra details like the commands to be run.",
EnvVar: "AHOY_VERBOSE",
EnvVars: []string{"AHOY_VERBOSE"},
Destination: &verbose,
},
cli.StringFlag{
Name: "file, f",
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "Use a specific ahoy file.",
Destination: &sourcefile,
},
cli.BoolFlag{
Name: "help, h",
&cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "version",
Usage: "print the version",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "generate-bash-completion",
},
}
Expand Down
4 changes: 3 additions & 1 deletion v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ require (
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.27.5 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
6 changes: 6 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -14,6 +16,10 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.9 h1:cv3/KhXGBGjEXLC4bH0sLuJ9BewaAbpk5oyMOveu4pw=
github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
2 changes: 1 addition & 1 deletion v2/testdata/command-aliases.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commands:
hello:
usage: Say hello
cmd: echo "Hello, World!"
aliases: ["hi", "greet", "ahoy"]
aliases: ["hi", "greet"]

ahoy-there:
usage: Say "ahoy there!"
Expand Down
6 changes: 0 additions & 6 deletions v2/tests/command-aliases.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ load 'test_helpers/bats-assert/load'
[[ "$output" =~ "ahoy there!" ]]
[ "$status" -eq 0 ]
}

@test "Multiple conflicting aliases means the last one loaded takes precedence" {
run ./ahoy -f testdata/command-aliases.ahoy.yml ahoy
[[ "$output" =~ "ahoy there!" ]]
[ "$status" -eq 0 ]
}
1 change: 1 addition & 0 deletions v2/tests/sub-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ teardown() {

# Check that sub1-cmd and sub2-cmd are listed under main-cmd
run ./ahoy main-cmd --help
echo $output
[ "$status" -eq 0 ]
[[ "$output" =~ "sub1-cmd" ]]
[[ "$output" =~ "sub2-cmd" ]]
Expand Down
62 changes: 62 additions & 0 deletions v2/vendor/github.com/cpuguy83/go-md2man/v2/md2man/debug.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions v2/vendor/github.com/cpuguy83/go-md2man/v2/md2man/md2man.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aba1cfa

Please sign in to comment.