Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Initialize Git repository #21

Merged
merged 15 commits into from
Nov 11, 2023
Merged
32 changes: 30 additions & 2 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"os"
"strings"

tea "github.com/charmbracelet/bubbletea"
tpl "github.com/melkeydev/go-blueprint/cmd/template"
"github.com/melkeydev/go-blueprint/cmd/utils"
Expand Down Expand Up @@ -209,6 +208,35 @@ func (p *Project) CreateMainFile() error {
return err
}

// Initialize git repo
err = utils.ExecuteCmd("git", []string{"init"}, projectPath)
if err != nil {
log.Printf("Error initializing git repo: %v", err)
cobra.CheckErr(err)
return err
}
// rename master to main
err = utils.ExecuteCmd("git", []string{"branch","-f", "-m","main"}, projectPath)
if err != nil {
log.Printf("Error renaming master branch to main: %v", err)
cobra.CheckErr(err)
return err
}
// Create gitignore
gitignoreFile, err := os.Create(fmt.Sprintf("%s/.gitignore", projectPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer gitignoreFile.Close()

// inject gitignore template
gitignoreTemplate := template.Must(template.New(".gitignore").Parse(string(tpl.GitIgnoreTemplate())))
err = gitignoreTemplate.Execute(gitignoreFile, p)
if err != nil {
return err
}

// Create .air.toml file
airTomlFile, err := os.Create(fmt.Sprintf("%s/.air.toml", projectPath))
if err != nil {
Expand All @@ -229,8 +257,8 @@ func (p *Project) CreateMainFile() error {
if err != nil {
log.Printf("Could not gofmt in new project %v\n", err)
cobra.CheckErr(err)
return err
}

return nil
}

Expand Down
39 changes: 36 additions & 3 deletions cmd/template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// help with the templating of created files.
package template

// MakeHTTPRoutes returns a byte slice that represents
// MakeHTTPRoutes returns a byte slice that represents
// the default cmd/api/main.go file template.
func MainTemplate() []byte {
return []byte(`package main
Expand All @@ -23,7 +23,7 @@ func main() {
`)
}

// MakeHTTPRoutes returns a byte slice that represents
// MakeHTTPRoutes returns a byte slice that represents
// the default Makefile.
func MakeTemplate() []byte {
return []byte(
Expand Down Expand Up @@ -61,6 +61,38 @@ watch:
}


func GitIgnoreTemplate() []byte {
return []byte(
`
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with "go test -c"
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
tmp/

# IDE specific files
.vscode
.idea

# build output
air
`)
SudoSurya marked this conversation as resolved.
Show resolved Hide resolved
}

func AirTomlTemplate() []byte {
return []byte(
`
Expand Down Expand Up @@ -114,7 +146,7 @@ tmp_dir = "tmp"
}


// ReadmeTemplate returns a byte slice that represents
// ReadmeTemplate returns a byte slice that represents
// the default README.md file template.
func ReadmeTemplate() []byte {
return []byte(
Expand Down Expand Up @@ -154,4 +186,5 @@ clean up binary from the last build
make clean
` + "```" + `
`)

}