diff --git a/Makefile b/Makefile index 25b95a0..6f654fc 100644 --- a/Makefile +++ b/Makefile @@ -47,3 +47,6 @@ lint: bin/golangci-lint fmt: go fmt ./... + +init: + go run etc/init.go diff --git a/README.md b/README.md index 1ed036d..54e9113 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,17 @@ Clone this repository: git clone --depth 1 https://github.com/anycable/anycable-go-scaffold my-cable-project ``` -Rename the project from `anycable/mycable` to `/` (`go.mod`, `*.go` files, etc.). +Run the following command to set up the project: -Make sure everything is ready by running `make lint` and `make test`. +```sh +make init +``` + +It would rename the project from `anycable/mycable` to whatever name you want (`/`) updating the `go.mod`, `*.go` files, etc. ## Development -**NOTE:** Make sure Go 1.21+ installed. +**NOTE:** Make sure Go 1.23+ installed. The following commands are available: diff --git a/etc/init.go b/etc/init.go new file mode 100644 index 0000000..e759a86 --- /dev/null +++ b/etc/init.go @@ -0,0 +1,107 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" +) + +func main() { + // Check if PROJECT parameter is provided + project := os.Getenv("PROJECT") + if project == "" { + fmt.Print("Enter project name (e.g. my-org/my-project): ") + var input string + _, err := fmt.Scanln(&input) + + if err != nil { + fmt.Printf("Error reading input: %v\n", err) + os.Exit(1) + } + + if input == "" { + fmt.Println("Error: Project name is required") + os.Exit(1) + } + project = input + } + + // Get binary name from project + binName := filepath.Base(project) + + // Replace in all .go files + err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && strings.HasSuffix(path, ".go") { + replaceInFile(path, "github.com/anycable/mycable", "github.com/"+project) + } + return nil + }) + if err != nil { + fmt.Printf("Error scanning .go files: %v\n", err) + os.Exit(1) + } + + // Replace in go.mod + replaceInFile("go.mod", "github.com/anycable/mycable", "github.com/"+project) + + // Replace in go.sum + replaceInFile("go.sum", "github.com/anycable/mycable", "github.com/"+project) + + // Replace in Makefile + replaceInFile("Makefile", "PROJECT=anycable/mycable", "PROJECT="+project) + + // Rename cmd directory + err = os.Rename("cmd/mycable", "cmd/"+binName) + if err != nil { + fmt.Printf("Error renaming cmd directory: %v\n", err) + os.Exit(1) + } + + // Run go mod tidy + if err := execCommand("go", "mod", "tidy"); err != nil { + fmt.Printf("Error running go mod tidy: %v\n", err) + os.Exit(1) + } + + // Run make lint + if err := execCommand("make", "lint"); err != nil { + fmt.Printf("Error running make lint: %v\n", err) + os.Exit(1) + } + + // Run make test + if err := execCommand("make", "test"); err != nil { + fmt.Printf("Error running make test: %v\n", err) + os.Exit(1) + } + + fmt.Println("Project renamed successfully!") +} + +func replaceInFile(filename, old, new string) { + content, err := os.ReadFile(filename) + if err != nil { + fmt.Printf("Error reading file %s: %v\n", filename, err) + os.Exit(1) + } + + newContent := strings.ReplaceAll(string(content), old, new) + + err = os.WriteFile(filename, []byte(newContent), 0600) + if err != nil { + fmt.Printf("Error writing file %s: %v\n", filename, err) + os.Exit(1) + } +} + +func execCommand(name string, args ...string) error { + cmd := exec.Command(name, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +}