Added CLI commands

This commit is contained in:
Eduard Urbach 2023-10-17 14:01:01 +02:00
parent 19d23eeec4
commit c3925e86b3
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 143 additions and 2 deletions

5
cli/Build.go Normal file
View File

@ -0,0 +1,5 @@
package cli
func Build(args []string) int {
return 0
}

13
cli/Help.go Normal file
View File

@ -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
}

18
cli/Main.go Normal file
View File

@ -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:])
}
}

41
cli/System.go Normal file
View File

@ -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
}

27
cli_test.go Normal file
View File

@ -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)
}
}

2
go.mod
View File

@ -1,3 +1,5 @@
module git.akyoto.dev/cli/q module git.akyoto.dev/cli/q
go 1.21 go 1.21
require git.akyoto.dev/go/assert v0.1.3

2
go.sum Normal file
View File

@ -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=

14
log/log.go Normal file
View File

@ -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)
)

View File

@ -1,7 +1,11 @@
package main package main
import "os" import (
"os"
"git.akyoto.dev/cli/q/cli"
)
func main() { func main() {
os.Exit(0) os.Exit(cli.Main(os.Args[1:]))
} }

15
main_test.go Normal file
View File

@ -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())
}