[agent-automation] scaffold Go CLI project
This commit is contained in:
parent
f719faa28b
commit
938b08c734
5 changed files with 133 additions and 0 deletions
28
.gitignore
vendored
Normal file
28
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Binaries
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
test
|
||||||
|
test.exe
|
||||||
|
test.test
|
||||||
|
|
||||||
|
# Test binary
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage
|
||||||
|
profile.out
|
||||||
|
|
||||||
|
# Go build directory
|
||||||
|
vendor/
|
||||||
|
|
||||||
|
# Binary directory
|
||||||
|
bin/
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
15
cmd/cli/main.go
Normal file
15
cmd/cli/main.go
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/marmaduke/testbed2/pkg/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := cli.Run(os.Args[1:]); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module github.com/marmaduke/testbed2
|
||||||
|
|
||||||
|
go 1.21
|
||||||
26
pkg/cli/cli.go
Normal file
26
pkg/cli/cli.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
// Package cli provides the core CLI functionality.
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Run executes the CLI with the given arguments.
|
||||||
|
func Run(args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
fmt.Println("testbed2 CLI")
|
||||||
|
fmt.Println("Usage: testbed2 <command>")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch args[0] {
|
||||||
|
case "help", "--help", "-h":
|
||||||
|
fmt.Println("Available commands:")
|
||||||
|
fmt.Println(" help Show this help message")
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown command: %s", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
61
pkg/cli/cli_test.go
Normal file
61
pkg/cli/cli_test.go
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRunNoArgs(t *testing.T) {
|
||||||
|
oldStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
|
||||||
|
err := Run([]string{})
|
||||||
|
|
||||||
|
w.Close()
|
||||||
|
os.Stdout = oldStdout
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunHelp(t *testing.T) {
|
||||||
|
oldStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
|
||||||
|
err := Run([]string{"help"})
|
||||||
|
|
||||||
|
w.Close()
|
||||||
|
os.Stdout = oldStdout
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunUnknownCommand(t *testing.T) {
|
||||||
|
err := Run([]string{"unknown"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for unknown command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunOutput(t *testing.T) {
|
||||||
|
oldStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
|
||||||
|
Run([]string{})
|
||||||
|
|
||||||
|
w.Close()
|
||||||
|
os.Stdout = oldStdout
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(r)
|
||||||
|
if buf.Len() == 0 {
|
||||||
|
t.Fatal("expected output")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue