Skip to content

Commit

Permalink
Merge pull request #14 from rog-golang-buddies/whutchinson98/feat/sta…
Browse files Browse the repository at this point in the history
…rter-commands

feat: Set up basic commands that are called from cmd/main.go
  • Loading branch information
whutchinson98 authored Jul 15, 2022
2 parents 5614ad4 + 4d1703c commit 9982e06
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/rog-golang-buddies/go-automatic-apps/internal/parser"
cmd "github.com/rog-golang-buddies/go-automatic-apps/pkg/commands"
)

func main() {
Expand All @@ -14,6 +15,19 @@ func main() {
panic(err)
}

fmt.Printf("CLI Commands: %v\n", commands)
fmt.Printf("CLI Flags: %v\n", flags)
baseCommand := commands[0]
subCommands := commands[1:]

switch baseCommand {
case "help":
err = cmd.HelpCommand.Run(&subCommands, &flags)
case "version":
err = cmd.VersionCommand.Run(&subCommands, &flags)
default:
panic(fmt.Errorf("no command matches %v", baseCommand))
}

if err != nil {
panic(fmt.Errorf("error executing command %v", err))
}
}
4 changes: 4 additions & 0 deletions internal/parser/parse_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@ func ParseCliArguments(arguments []string) ([]string, map[string]interface{}, er
flags[flag] = true
}

if len(commands) == 0 {
return nil, nil, fmt.Errorf("no commands were provided")
}

return commands, flags, nil
}
12 changes: 12 additions & 0 deletions internal/parser/parse_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,17 @@ func TestParseCliArguments(t *testing.T) {
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "empty flag was passed in", err.Error())
}
})
t.Run("Errors if no commands were passed in", func(t *testing.T) {
_, _, err := ParseCliArguments([]string{"--foo", "bar"})

if err == nil {
t.Fatalf("Error was expected but not received")
}

if err.Error() != "no commands were provided" {
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "no commands were provided", err.Error())
}

})

}
7 changes: 7 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package commands

type BaseCommand struct {
Name string
Description string
Run func(commands *[]string, flags *map[string]interface{}) error
}
20 changes: 20 additions & 0 deletions pkg/commands/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package commands

import "fmt"

var HelpCommand = BaseCommand{
Name: "help",
Description: "Provides list of all commands",
Run: helpCommand,
}

func helpCommand(commands *[]string, flags *map[string]interface{}) error {
// We have to "remake" the help command otherwise we get a runtime cycle error
commandsList := []BaseCommand{{Name: "help", Description: "Provides list of all commands"}, VersionCommand}

for _, command := range commandsList {
fmt.Printf("[%v]: %v\n", command.Name, command.Description)
}

return nil
}
19 changes: 19 additions & 0 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package commands

import (
"fmt"
"runtime"
)

var VersionCommand = BaseCommand{
Name: "version",
Description: "Provides useful information about go environment",
Run: versionCommand,
}

func versionCommand(commands *[]string, flags *map[string]interface{}) error {
fmt.Printf("Go Version: %v\n", runtime.Version())
fmt.Printf("GAA: %v\n", runtime.GOARCH)
fmt.Printf("GOOS: %v\n", runtime.GOOS)
return nil
}

0 comments on commit 9982e06

Please sign in to comment.