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
39 changes: 35 additions & 4 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package program

import (
"fmt"
"html/template"
"log"
"os"

"html/template"
"log"
"os"
tea "github.com/charmbracelet/bubbletea"
tpl "github.com/melkeydev/go-blueprint/cmd/template"
"github.com/melkeydev/go-blueprint/cmd/utils"
Expand Down Expand Up @@ -192,6 +191,38 @@ 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
}
// Switch to main branch
err = utils.ExecuteCmd("git", []string{"branch", "-M", "main"}, projectPath)
if err != nil {
log.Printf("Error switching to main branch: %v", err)
cobra.CheckErr(err)
return err
}
SudoSurya marked this conversation as resolved.
Show resolved Hide resolved

// 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
}

err = utils.GoFmt(projectPath)
if err != nil {
log.Printf("Could not gofmt in new project %v\n", err)
Expand Down
35 changes: 35 additions & 0 deletions cmd/template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ clean:
`)
}


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

vendor/
tmp/

# IDE specific files
.vscode
.idea

# build output
air
`)
SudoSurya marked this conversation as resolved.
Show resolved Hide resolved
}
func ReadmeTemplate() []byte {
return []byte(
`
Expand Down Expand Up @@ -87,4 +121,5 @@ clean up binary from the last build
make clean
` + "```" + `
`)

}