main.go1.6 KB · go
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
fmt.Println("Usage: just-go <project-name>")
os.Exit(1)
}
projectName := args[0]
if strings.Contains(projectName, "/") || strings.Contains(projectName, "\\") {
die(fmt.Errorf("keep it flat. no nested paths allowed"))
}
if err := os.Mkdir(projectName, 0755); err != nil {
die(err)
}
createFile(projectName, "go.mod", fmt.Sprintf("module %s\n\ngo 1.23\n", projectName))
createFile(projectName, "main.go", `package main
import "fmt"
func main() {
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
fmt.Println("Hello, world")
return nil
}
`)
createFile(projectName, "main_test.go", `package main
import "testing"
func TestSanity(t *testing.T) {
// Standard library testing only.
// If you need an assertion library, you are writing too much code.
if 1+1 != 2 {
t.Fatal("math is broken")
}
}
`)
createFile(projectName, "README.md", fmt.Sprintf("# %s\n\nGenerated by just-go. Keep it flat.\n", projectName))
createFile(projectName, "Makefile", `.PHONY: run build test clean
run:
go run main.go
build:
go build -o bin/app main.go
test:
go test -v ./...
clean:
rm -rf bin
`)
createFile(projectName, ".gitignore", "bin/\n*.exe\n*.test\n")
fmt.Printf("Created %s. No subdirectories. Pure Go.\n", projectName)
}
func createFile(dir, name, content string) {
path := filepath.Join(dir, name)
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
die(err)
}
}
func die(err error) {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}