Skip to content

Commit

Permalink
make: add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 28, 2024
1 parent a30c5b7 commit 62b281e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ lint: bin/golangci-lint

fmt:
go fmt ./...

init:
go run etc/init.go
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<org>/<project>` (`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 (`<org>/<project>`) 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:

Expand Down
107 changes: 107 additions & 0 deletions etc/init.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 62b281e

Please sign in to comment.