Skip to content

Commit 9982e06

Browse files
Merge pull request #14 from rog-golang-buddies/whutchinson98/feat/starter-commands
feat: Set up basic commands that are called from cmd/main.go
2 parents 5614ad4 + 4d1703c commit 9982e06

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

cmd/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/rog-golang-buddies/go-automatic-apps/internal/parser"
8+
cmd "github.com/rog-golang-buddies/go-automatic-apps/pkg/commands"
89
)
910

1011
func main() {
@@ -14,6 +15,19 @@ func main() {
1415
panic(err)
1516
}
1617

17-
fmt.Printf("CLI Commands: %v\n", commands)
18-
fmt.Printf("CLI Flags: %v\n", flags)
18+
baseCommand := commands[0]
19+
subCommands := commands[1:]
20+
21+
switch baseCommand {
22+
case "help":
23+
err = cmd.HelpCommand.Run(&subCommands, &flags)
24+
case "version":
25+
err = cmd.VersionCommand.Run(&subCommands, &flags)
26+
default:
27+
panic(fmt.Errorf("no command matches %v", baseCommand))
28+
}
29+
30+
if err != nil {
31+
panic(fmt.Errorf("error executing command %v", err))
32+
}
1933
}

internal/parser/parse_cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@ func ParseCliArguments(arguments []string) ([]string, map[string]interface{}, er
7474
flags[flag] = true
7575
}
7676

77+
if len(commands) == 0 {
78+
return nil, nil, fmt.Errorf("no commands were provided")
79+
}
80+
7781
return commands, flags, nil
7882
}

internal/parser/parse_cli_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,17 @@ func TestParseCliArguments(t *testing.T) {
9797
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "empty flag was passed in", err.Error())
9898
}
9999
})
100+
t.Run("Errors if no commands were passed in", func(t *testing.T) {
101+
_, _, err := ParseCliArguments([]string{"--foo", "bar"})
102+
103+
if err == nil {
104+
t.Fatalf("Error was expected but not received")
105+
}
106+
107+
if err.Error() != "no commands were provided" {
108+
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "no commands were provided", err.Error())
109+
}
110+
111+
})
100112

101113
}

pkg/commands/commands.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package commands
2+
3+
type BaseCommand struct {
4+
Name string
5+
Description string
6+
Run func(commands *[]string, flags *map[string]interface{}) error
7+
}

pkg/commands/help.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package commands
2+
3+
import "fmt"
4+
5+
var HelpCommand = BaseCommand{
6+
Name: "help",
7+
Description: "Provides list of all commands",
8+
Run: helpCommand,
9+
}
10+
11+
func helpCommand(commands *[]string, flags *map[string]interface{}) error {
12+
// We have to "remake" the help command otherwise we get a runtime cycle error
13+
commandsList := []BaseCommand{{Name: "help", Description: "Provides list of all commands"}, VersionCommand}
14+
15+
for _, command := range commandsList {
16+
fmt.Printf("[%v]: %v\n", command.Name, command.Description)
17+
}
18+
19+
return nil
20+
}

pkg/commands/version.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var VersionCommand = BaseCommand{
9+
Name: "version",
10+
Description: "Provides useful information about go environment",
11+
Run: versionCommand,
12+
}
13+
14+
func versionCommand(commands *[]string, flags *map[string]interface{}) error {
15+
fmt.Printf("Go Version: %v\n", runtime.Version())
16+
fmt.Printf("GAA: %v\n", runtime.GOARCH)
17+
fmt.Printf("GOOS: %v\n", runtime.GOOS)
18+
return nil
19+
}

0 commit comments

Comments
 (0)