Skip to content

Commit

Permalink
#315 fix: cursor error when go-blueprint create fails (#318)
Browse files Browse the repository at this point in the history
* #315 fix: cursor error
  • Loading branch information
vinitparekh17 authored Nov 3, 2024
1 parent ecb9f4a commit 09d2485
Showing 1 changed file with 20 additions and 46 deletions.
66 changes: 20 additions & 46 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/melkeydev/go-blueprint/cmd/template/docker"
"github.com/melkeydev/go-blueprint/cmd/template/framework"
"github.com/melkeydev/go-blueprint/cmd/utils"
"github.com/spf13/cobra"
)

// A Project contains the data for the project folder
Expand Down Expand Up @@ -244,7 +243,7 @@ func (p *Project) CreateMainFile() error {
// Check if user.email is set.
emailSet, err := utils.CheckGitConfig("user.email")
if err != nil {
cobra.CheckErr(err)
return err
}

if !emailSet && p.GitOptions.String() != flags.Skip {
Expand Down Expand Up @@ -275,15 +274,15 @@ func (p *Project) CreateMainFile() error {
err = utils.InitGoMod(p.ProjectName, projectPath)
if err != nil {
log.Printf("Could not initialize go.mod in new project %v\n", err)
cobra.CheckErr(err)
return err
}

// Install the correct package for the selected framework
if p.ProjectType != flags.StandardLibrary {
err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType].packageName)
if err != nil {
log.Printf("Could not install go dependency for the chosen framework %v\n", err)
cobra.CheckErr(err)
return err
}
}

Expand All @@ -293,28 +292,25 @@ func (p *Project) CreateMainFile() error {
err = utils.GoGetPackage(projectPath, p.DBDriverMap[p.DBDriver].packageName)
if err != nil {
log.Printf("Could not install go dependency for chosen driver %v\n", err)
cobra.CheckErr(err)
return err
}

err = p.CreatePath(internalDatabasePath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", internalDatabasePath)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalDatabasePath, projectPath, "database.go", "database")
if err != nil {
log.Printf("Error injecting database.go file: %v", err)
cobra.CheckErr(err)
return err
}

if p.DBDriver != "sqlite" {
err = p.CreateFileWithInjection(internalDatabasePath, projectPath, "database_test.go", "integration-tests")
if err != nil {
log.Printf("Error injecting database_test.go file: %v", err)
cobra.CheckErr(err)
return err
}
}
Expand All @@ -329,7 +325,6 @@ func (p *Project) CreateMainFile() error {
err = p.CreateFileWithInjection(root, projectPath, "docker-compose.yml", "db-docker")
if err != nil {
log.Printf("Error injecting docker-compose.yml file: %v", err)
cobra.CheckErr(err)
return err
}
} else {
Expand All @@ -341,25 +336,23 @@ func (p *Project) CreateMainFile() error {
err = utils.GoGetPackage(projectPath, godotenvPackage)
if err != nil {
log.Printf("Could not install go dependency %v\n", err)
cobra.CheckErr(err)

return err
}

err = p.CreatePath(cmdApiPath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", projectPath)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(cmdApiPath, projectPath, "main.go", "main")
if err != nil {
cobra.CheckErr(err)
return err
}

makeFile, err := os.Create(filepath.Join(projectPath, "Makefile"))
if err != nil {
cobra.CheckErr(err)
return err
}

Expand All @@ -374,7 +367,6 @@ func (p *Project) CreateMainFile() error {

readmeFile, err := os.Create(filepath.Join(projectPath, "README.md"))
if err != nil {
cobra.CheckErr(err)
return err
}
defer readmeFile.Close()
Expand All @@ -389,7 +381,6 @@ func (p *Project) CreateMainFile() error {
err = p.CreatePath(internalServerPath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", internalServerPath)
cobra.CheckErr(err)
return err
}

Expand All @@ -399,7 +390,7 @@ func (p *Project) CreateMainFile() error {

tailwindConfigFile, err := os.Create(fmt.Sprintf("%s/tailwind.config.js", projectPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer tailwindConfigFile.Close()

Expand All @@ -411,12 +402,12 @@ func (p *Project) CreateMainFile() error {

err = os.MkdirAll(fmt.Sprintf("%s/%s/assets/css", projectPath, cmdWebPath), 0o755)
if err != nil {
cobra.CheckErr(err)
return err
}

inputCssFile, err := os.Create(fmt.Sprintf("%s/%s/assets/css/input.css", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer inputCssFile.Close()

Expand All @@ -428,7 +419,7 @@ func (p *Project) CreateMainFile() error {

outputCssFile, err := os.Create(fmt.Sprintf("%s/%s/assets/css/output.css", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer outputCssFile.Close()

Expand All @@ -443,12 +434,11 @@ func (p *Project) CreateMainFile() error {
// create folders and hello world file
err = p.CreatePath(cmdWebPath, projectPath)
if err != nil {
cobra.CheckErr(err)
return err
}
helloTemplFile, err := os.Create(fmt.Sprintf("%s/%s/hello.templ", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer helloTemplFile.Close()

Expand All @@ -461,7 +451,7 @@ func (p *Project) CreateMainFile() error {

baseTemplFile, err := os.Create(fmt.Sprintf("%s/%s/base.templ", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer baseTemplFile.Close()

Expand All @@ -473,12 +463,12 @@ func (p *Project) CreateMainFile() error {

err = os.MkdirAll(fmt.Sprintf("%s/%s/assets/js", projectPath, cmdWebPath), 0o755)
if err != nil {
cobra.CheckErr(err)
return err
}

htmxMinJsFile, err := os.Create(fmt.Sprintf("%s/%s/assets/js/htmx.min.js", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer htmxMinJsFile.Close()

Expand All @@ -490,7 +480,7 @@ func (p *Project) CreateMainFile() error {

efsFile, err := os.Create(fmt.Sprintf("%s/%s/efs.go", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer efsFile.Close()

Expand All @@ -502,12 +492,12 @@ func (p *Project) CreateMainFile() error {
err = utils.GoGetPackage(projectPath, templPackage)
if err != nil {
log.Printf("Could not install go dependency %v\n", err)
cobra.CheckErr(err)
return err
}

helloGoFile, err := os.Create(fmt.Sprintf("%s/%s/hello.go", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
return err
}
defer efsFile.Close()

Expand All @@ -520,7 +510,7 @@ func (p *Project) CreateMainFile() error {
err = utils.GoGetPackage(projectPath, []string{"github.com/gofiber/fiber/v2/middleware/adaptor"})
if err != nil {
log.Printf("Could not install go dependency %v\n", err)
cobra.CheckErr(err)
return err
}
} else {
helloGoTemplate := template.Must(template.New("efs").Parse((string(advanced.HelloGoTemplate()))))
Expand All @@ -538,28 +528,24 @@ func (p *Project) CreateMainFile() error {
err = p.CreatePath(gitHubActionPath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", gitHubActionPath)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(gitHubActionPath, projectPath, "release.yml", "releaser")
if err != nil {
log.Printf("Error injecting release.yml file: %v", err)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(gitHubActionPath, projectPath, "go-test.yml", "go-test")
if err != nil {
log.Printf("Error injecting go-test.yml file: %v", err)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(root, projectPath, ".goreleaser.yml", "releaser-config")
if err != nil {
log.Printf("Error injecting .goreleaser.yml file: %v", err)
cobra.CheckErr(err)
return err
}
}
Expand All @@ -575,7 +561,6 @@ func (p *Project) CreateMainFile() error {
if p.AdvancedOptions[string(flags.Docker)] {
Dockerfile, err := os.Create(filepath.Join(projectPath, "Dockerfile"))
if err != nil {
cobra.CheckErr(err)
return err
}
defer Dockerfile.Close()
Expand All @@ -591,7 +576,6 @@ func (p *Project) CreateMainFile() error {

Dockercompose, err := os.Create(filepath.Join(projectPath, "docker-compose.yml"))
if err != nil {
cobra.CheckErr(err)
return err
}
defer Dockercompose.Close()
Expand All @@ -608,34 +592,29 @@ func (p *Project) CreateMainFile() error {
err = p.CreateFileWithInjection(internalServerPath, projectPath, "routes.go", "routes")
if err != nil {
log.Printf("Error injecting routes.go file: %v", err)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "routes_test.go", "tests")
if err != nil {
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "server.go", "server")
if err != nil {
log.Printf("Error injecting server.go file: %v", err)
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(root, projectPath, ".env", "env")
if err != nil {
log.Printf("Error injecting .env file: %v", err)
cobra.CheckErr(err)
return err
}

// Create gitignore
gitignoreFile, err := os.Create(filepath.Join(projectPath, ".gitignore"))
if err != nil {
cobra.CheckErr(err)
return err
}
defer gitignoreFile.Close()
Expand All @@ -650,7 +629,6 @@ func (p *Project) CreateMainFile() error {
// Create .air.toml file
airTomlFile, err := os.Create(filepath.Join(projectPath, ".air.toml"))
if err != nil {
cobra.CheckErr(err)
return err
}

Expand All @@ -666,19 +644,18 @@ func (p *Project) CreateMainFile() error {
err = utils.GoTidy(projectPath)
if err != nil {
log.Printf("Could not go tidy in new project %v\n", err)
cobra.CheckErr(err)
return err
}

err = utils.GoFmt(projectPath)
if err != nil {
log.Printf("Could not gofmt in new project %v\n", err)
cobra.CheckErr(err)
return err
}

nameSet, err := utils.CheckGitConfig("user.name")
if err != nil {
cobra.CheckErr(err)
return err
}

if p.GitOptions != flags.Skip {
Expand All @@ -691,15 +668,13 @@ func (p *Project) CreateMainFile() error {
err = utils.ExecuteCmd("git", []string{"init"}, projectPath)
if err != nil {
log.Printf("Error initializing git repo: %v", err)
cobra.CheckErr(err)
return err
}

// Git add files
err = utils.ExecuteCmd("git", []string{"add", "."}, projectPath)
if err != nil {
log.Printf("Error adding files to git repo: %v", err)
cobra.CheckErr(err)
return err
}

Expand All @@ -708,7 +683,6 @@ func (p *Project) CreateMainFile() error {
err = utils.ExecuteCmd("git", []string{"commit", "-m", "Initial commit"}, projectPath)
if err != nil {
log.Printf("Error committing files to git repo: %v", err)
cobra.CheckErr(err)
return err
}
}
Expand Down

0 comments on commit 09d2485

Please sign in to comment.