Added CLI commands
This commit is contained in:
parent
19d23eeec4
commit
c3925e86b3
5
cli/Build.go
Normal file
5
cli/Build.go
Normal file
@ -0,0 +1,5 @@
|
||||
package cli
|
||||
|
||||
func Build(args []string) int {
|
||||
return 0
|
||||
}
|
13
cli/Help.go
Normal file
13
cli/Help.go
Normal 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
18
cli/Main.go
Normal 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
41
cli/System.go
Normal 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
27
cli_test.go
Normal 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
2
go.mod
@ -1,3 +1,5 @@
|
||||
module git.akyoto.dev/cli/q
|
||||
|
||||
go 1.21
|
||||
|
||||
require git.akyoto.dev/go/assert v0.1.3
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal 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
14
log/log.go
Normal 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)
|
||||
)
|
8
main.go
8
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:]))
|
||||
}
|
||||
|
15
main_test.go
Normal file
15
main_test.go
Normal 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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user