Skip to content

Commit

Permalink
Add git options (commit, stage, and no git repository) (#270)
Browse files Browse the repository at this point in the history
* Feat: create git step functionality
  • Loading branch information
tomasohCHOM authored Jul 10, 2024
1 parent ade48aa commit 323bc07
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
git config --global user.email 'testemail@users.noreply.github.com'
- name: build templates
run: script -q /dev/null -c "go run main.go create -n ${{ matrix.framework }} -f ${{ matrix.framework}} -d ${{matrix.driver}} --advanced true --feature ${{ matrix.advanced }}" /dev/null
run: script -q /dev/null -c "go run main.go create -n ${{ matrix.framework }} -f ${{ matrix.framework}} -d ${{ matrix.driver }} --advanced true --feature ${{ matrix.advanced }} -g commit" /dev/null

- if: ${{ matrix.advanced == 'htmx' }}
name: Install Templ & gen templates
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ gives the option to integrate with one of the more popular Go frameworks (and th
Install
</h2>

```sh
```bash
go install github.com/melkeydev/go-blueprint@latest
```

This installs a go binary that will automatically bind to your $GOPATH

Then in a new terminal run:

```
```bash
go-blueprint create
```

You can also use the provided flags to set up a project without interacting with the UI.

```
go-blueprint create --name my-project --framework gin --driver postgres
```bash
go-blueprint create --name my-project --framework gin --driver postgres --git commit
```

See `go-blueprint create -h` for all the options and shorthands.
Expand Down Expand Up @@ -138,7 +138,7 @@ Blueprint UI is a web application that allows you to create commands for the CLI
Here's an example of setting up a project with a specific database driver:

```bash
go-blueprint create --name my-project --framework gin --driver postgres
go-blueprint create --name my-project --framework gin --driver postgres --git commit
```

<p align="center">
Expand Down Expand Up @@ -175,7 +175,7 @@ go-blueprint create --advanced --feature tailwind

Or all features at once:
```bash
go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind
go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind --git commit
```

<p align="center">
Expand Down
22 changes: 22 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func init() {
var flagFramework flags.Framework
var flagDBDriver flags.Database
var advancedFeatures flags.AdvancedFeatures
var flagGit flags.Git
rootCmd.AddCommand(createCmd)

createCmd.Flags().StringP("name", "n", "", "Name of project to create")
createCmd.Flags().VarP(&flagFramework, "framework", "f", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(flags.AllowedProjectTypes, ", ")))
createCmd.Flags().VarP(&flagDBDriver, "driver", "d", fmt.Sprintf("Database drivers to use. Allowed values: %s", strings.Join(flags.AllowedDBDrivers, ", ")))
createCmd.Flags().BoolP("advanced", "a", false, "Get prompts for advanced features")
createCmd.Flags().Var(&advancedFeatures, "feature", fmt.Sprintf("Advanced feature to use. Allowed values: %s", strings.Join(flags.AllowedAdvancedFeatures, ", ")))
createCmd.Flags().VarP(&flagGit, "git", "g", fmt.Sprintf("Git to use. Allowed values: %s", strings.Join(flags.AllowedGitsOptions, ", ")))
}

type Options struct {
Expand All @@ -58,6 +60,7 @@ type Options struct {
DBDriver *multiInput.Selection
Advanced *multiSelect.Selection
Workflow *multiInput.Selection
Git *multiInput.Selection
}

// createCmd defines the "create" command for the CLI
Expand All @@ -81,6 +84,7 @@ var createCmd = &cobra.Command{
// If this flag is filled, it is always valid
flagFramework := flags.Framework(cmd.Flag("framework").Value.String())
flagDBDriver := flags.Database(cmd.Flag("driver").Value.String())
flagGit := flags.Git(cmd.Flag("git").Value.String())

options := Options{
ProjectName: &textinput.Output{},
Expand All @@ -89,6 +93,7 @@ var createCmd = &cobra.Command{
Advanced: &multiSelect.Selection{
Choices: make(map[string]bool),
},
Git: &multiInput.Selection{},
}

project := &program.Project{
Expand All @@ -98,6 +103,7 @@ var createCmd = &cobra.Command{
FrameworkMap: make(map[flags.Framework]program.Framework),
DBDriverMap: make(map[flags.Database]program.Driver),
AdvancedOptions: make(map[string]bool),
GitOptions: flagGit,
}

steps := steps.InitSteps(flagFramework, flagDBDriver)
Expand Down Expand Up @@ -202,6 +208,22 @@ var createCmd = &cobra.Command{

}

if project.GitOptions == "" {
isInteractive = true
step := steps.Steps["git"]
tprogram = tea.NewProgram(multiInput.InitialModelMulti(step.Options, options.Git, step.Headers, project))
if _, err := tprogram.Run(); err != nil {
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
project.ExitCLI(tprogram)

project.GitOptions = flags.Git(strings.ToLower(options.Git.Choice))
err := cmd.Flag("git").Value.Set(project.GitOptions.String())
if err != nil {
log.Fatal("failed to set the git flag value", err)
}
}

currentWorkingDir, err := os.Getwd()
if err != nil {
log.Printf("could not get current working directory: %v", err)
Expand Down
35 changes: 35 additions & 0 deletions cmd/flags/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package flags

import (
"fmt"
"strings"
)

type Git string

const (
Commit = "commit"
Stage = "stage"
Skip = "skip"
)

var AllowedGitsOptions = []string{string(Commit), string(Stage), string(Skip)}

func (f Git) String() string {
return string(f)
}

func (f *Git) Type() string {
return "Git"
}

func (f *Git) Set(value string) error {
for _, gitOption := range AllowedGitsOptions {
if gitOption == value {
*f = Git(value)
return nil
}
}

return fmt.Errorf("Git to use. Allowed values: %s", strings.Join(AllowedGitsOptions, ", "))
}
62 changes: 35 additions & 27 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Project struct {
DockerMap map[flags.Database]Docker
AdvancedOptions map[string]bool
AdvancedTemplates AdvancedTemplates
GitOptions flags.Git
}

type AdvancedTemplates struct {
Expand Down Expand Up @@ -222,16 +223,6 @@ func (p *Project) CreateMainFile() error {
}
}

nameSet, err := utils.CheckGitConfig("user.name")
if err != nil {
cobra.CheckErr(err)
}
if !nameSet {
fmt.Println("user.name is not set in git config.")
fmt.Println("Please set up git config before trying again.")
panic("\nGIT CONFIG ISSUE: user.name is not set in git config.\n")
}

// Check if user.email is set.
emailSet, err := utils.CheckGitConfig("user.email")
if err != nil {
Expand Down Expand Up @@ -583,13 +574,6 @@ 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
}
// Create gitignore
gitignoreFile, err := os.Create(filepath.Join(projectPath, ".gitignore"))
if err != nil {
Expand Down Expand Up @@ -633,19 +617,43 @@ func (p *Project) CreateMainFile() error {
cobra.CheckErr(err)
return err
}
// Git add files
err = utils.ExecuteCmd("git", []string{"add", "."}, projectPath)

nameSet, err := utils.CheckGitConfig("user.name")
if err != nil {
log.Printf("Error adding files to git repo: %v", err)
cobra.CheckErr(err)
return err
}
// Git commit files
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

if p.GitOptions != flags.Skip {
if !nameSet {
fmt.Println("user.name is not set in git config.")
fmt.Println("Please set up git config before trying again.")
panic("\nGIT CONFIG ISSUE: user.name is not set in git config.\n")
}
// 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
}

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

if p.GitOptions == flags.Commit {
// Git commit files
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
}
}
}
return nil
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ func InitSteps(projectType flags.Framework, databaseType flags.Database) *Steps
},
},
},
"git": {
StepName: "Git Repository",
Headers: "Which git option would you like to select for your project?",
Options: []Item{
{
Title: "Commit",
Desc: "Initialize a new git repository and commit all the changes",
},
{
Title: "Stage",
Desc: "Initialize a new git repository but only stage the changes",
},
{
Title: "Skip",
Desc: "Proceed without initializing a git repository",
},
},
},
},
}

Expand Down
7 changes: 4 additions & 3 deletions docs/docs/creating-project/project-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ This command will interactively guide you through the project setup process, all
For a non-interactive setup, you can use flags to provide the necessary information during project creation. Here's an example:

```
go-blueprint create --name my-project --framework gin --driver postgres
go-blueprint create --name my-project --framework gin --driver postgres --git commit
```

In this example:

- `--name`: Specifies the name of the project (replace "my-project" with your desired project name).
- `--framework`: Specifies the Go framework to be used (e.g., "gin").
- `--driver`: Specifies the database driver to be integrated (e.g., "postgres").
- `--git`: Specifies the git configuration option of the project (e.g., "commit").

Customize the flags according to your project requirements.

Expand All @@ -36,7 +37,7 @@ go-blueprint create --advanced

To recreate the project using the same configuration semi-interactively, use the following command:
```bash
go-blueprint create --name my-project --framework chi --driver mysql --advanced
go-blueprint create --name my-project --framework chi --driver mysql --git commit --advanced
```
This approach opens interactive mode only for advanced features, which allow you to choose the one or combination of available features.

Expand Down Expand Up @@ -67,5 +68,5 @@ go-blueprint create --advanced --feature tailwind

Or all features at once:
```bash
go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind
go-blueprint create --name my-project --framework chi --driver mysql --git commit --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind
```
Binary file modified docs/docs/public/blueprint_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/blueprint_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 323bc07

Please sign in to comment.