diff --git a/cmd/generate.go b/cmd/generate.go index 542a62e..ecad8e3 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -49,12 +49,14 @@ func GenerateCommand() *cobra.Command { var projectName string var githubRepo string + var gitBranch string if err != nil { log.Println(err) } else { projectName = p.Name githubRepo = p.GithubRepo + gitBranch = p.GitBranch } path, err := os.Getwd() @@ -62,8 +64,6 @@ func GenerateCommand() *cobra.Command { log.Println(err) } - var gitBranch string = "main" - generateCmd.Flags().String("project-name", projectName, "Project and binary name, current folder name by default") generateCmd.Flags().String("github-repo", githubRepo, "Github repo in form user/repo") generateCmd.Flags().String("git-branch", gitBranch, "Git branch to refer in the readme") diff --git a/internal/project/project.go b/internal/project/project.go index 4582fba..0e1c874 100644 --- a/internal/project/project.go +++ b/internal/project/project.go @@ -2,6 +2,7 @@ package project import ( "bytes" + "fmt" "os" "os/exec" "path/filepath" @@ -13,6 +14,7 @@ import ( type Project struct { Name string GithubRepo string + GitBranch string } func ResolveProjectFromCwd() (*Project, error) { @@ -26,12 +28,19 @@ func ResolveProjectFromCwd() (*Project, error) { currentRepo, err := resolveGitRepo() if err != nil { - return nil, errors.Wrapf(err, "Failed to resolve github repo") + fmt.Fprintln(os.Stderr, "Failed to resolve github repo, it will have to be specified manually") + } + + currentBranch, err := resolveGitBranch() + + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to resolve current git branch, it will have to be specified manually") } return &Project{ Name: cwdFolder, GithubRepo: currentRepo, + GitBranch: currentBranch, }, nil } @@ -54,3 +63,21 @@ func resolveGitRepo() (string, error) { return repo, nil } + +func resolveGitBranch() (string, error) { + cmd := exec.Command("git", "branch", "--no-color", "--show-current") + + var out bytes.Buffer + cmd.Stdout = &out + + err := cmd.Run() + + if err != nil { + return "", err + } + + branch := out.String() + branch = strings.TrimSpace(branch) + + return branch, nil +}