diff --git a/cli/Build.go b/cli/Build.go new file mode 100644 index 0000000..5c7ea6e --- /dev/null +++ b/cli/Build.go @@ -0,0 +1,5 @@ +package cli + +func Build(args []string) int { + return 0 +} diff --git a/cli/Help.go b/cli/Help.go new file mode 100644 index 0000000..464cb35 --- /dev/null +++ b/cli/Help.go @@ -0,0 +1,13 @@ +package cli + +import ( + "git.akyoto.dev/cli/q/log" +) + +func Help(args []string) int { + log.Error.Println("Usage: q [command] [options]") + log.Error.Println("") + log.Error.Println(" build [directory]") + log.Error.Println(" system") + return 1 +} diff --git a/cli/Main.go b/cli/Main.go new file mode 100644 index 0000000..d6d43c6 --- /dev/null +++ b/cli/Main.go @@ -0,0 +1,18 @@ +package cli + +func Main(args []string) int { + if len(args) == 0 { + return Help(nil) + } + + switch args[0] { + case "build": + return Build(args[1:]) + + case "system": + return System(args[1:]) + + default: + return Help(args[1:]) + } +} diff --git a/cli/System.go b/cli/System.go new file mode 100644 index 0000000..524e0fb --- /dev/null +++ b/cli/System.go @@ -0,0 +1,41 @@ +package cli + +import ( + "os" + "runtime" + + "git.akyoto.dev/cli/q/log" +) + +func System(args []string) int { + line := "%-19s%s\n" + + // Platform + log.Info.Printf(line, "Platform:", runtime.GOOS) + + // Architecture + log.Info.Printf(line, "Architecture:", runtime.GOARCH) + + // Go + log.Info.Printf(line, "Go:", runtime.Version()) + + // Directory + directory, err := os.Getwd() + + if err == nil { + log.Info.Printf(line, "Directory:", directory) + } else { + log.Info.Printf(line, "Directory:", err.Error()) + } + + // Compiler + executable, err := os.Executable() + + if err == nil { + log.Info.Printf(line, "Compiler:", executable) + } else { + log.Info.Printf(line, "Compiler:", err.Error()) + } + + return 0 +} diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 0000000..b99942f --- /dev/null +++ b/cli_test.go @@ -0,0 +1,27 @@ +package main_test + +import ( + "testing" + + "git.akyoto.dev/cli/q/cli" + "git.akyoto.dev/go/assert" +) + +func TestCLI(t *testing.T) { + type cliTest struct { + arguments []string + expectedExitCode int + } + + tests := []cliTest{ + {[]string{}, 1}, + {[]string{"invalid"}, 1}, + {[]string{"system"}, 0}, + } + + for _, test := range tests { + exitCode := cli.Main(test.arguments) + t.Log(test.arguments) + assert.Equal(t, exitCode, test.expectedExitCode) + } +} diff --git a/go.mod b/go.mod index 2c2b156..e78e48b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.akyoto.dev/cli/q go 1.21 + +require git.akyoto.dev/go/assert v0.1.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9fc2547 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8= +git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM= diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..395f6ab --- /dev/null +++ b/log/log.go @@ -0,0 +1,14 @@ +package log + +import ( + "log" + "os" +) + +var ( + // Info is used for general info messages. + Info = log.New(os.Stdout, "", 0) + + // Error is used for error messages. + Error = log.New(os.Stderr, "", 0) +) diff --git a/main.go b/main.go index d2013bb..2a518ef 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,11 @@ package main -import "os" +import ( + "os" + + "git.akyoto.dev/cli/q/cli" +) func main() { - os.Exit(0) + os.Exit(cli.Main(os.Args[1:])) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..9bf72ae --- /dev/null +++ b/main_test.go @@ -0,0 +1,15 @@ +package main_test + +import ( + "io" + "os" + "testing" + + "git.akyoto.dev/cli/q/log" +) + +func TestMain(m *testing.M) { + log.Info.SetOutput(io.Discard) + log.Error.SetOutput(io.Discard) + os.Exit(m.Run()) +}