diff --git a/.github/workflows/generate-linter.yml b/.github/workflows/generate-linter.yml index a16decc1..bfb8795b 100644 --- a/.github/workflows/generate-linter.yml +++ b/.github/workflows/generate-linter.yml @@ -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 diff --git a/README.md b/README.md index ea4133e7..b274d7b1 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ gives the option to integrate with one of the more popular Go frameworks (and th Install -```sh +```bash go install github.com/melkeydev/go-blueprint@latest ``` @@ -46,14 +46,14 @@ 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. @@ -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 ```
@@ -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 ```
diff --git a/cmd/create.go b/cmd/create.go index 9254d90c..689805cd 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -43,6 +43,7 @@ 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") @@ -50,6 +51,7 @@ func init() { 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 { @@ -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 @@ -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{}, @@ -89,6 +93,7 @@ var createCmd = &cobra.Command{ Advanced: &multiSelect.Selection{ Choices: make(map[string]bool), }, + Git: &multiInput.Selection{}, } project := &program.Project{ @@ -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) @@ -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) diff --git a/cmd/flags/git.go b/cmd/flags/git.go new file mode 100644 index 00000000..a04f4fed --- /dev/null +++ b/cmd/flags/git.go @@ -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, ", ")) +} diff --git a/cmd/program/program.go b/cmd/program/program.go index e8d02738..8acb3d7f 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -36,6 +36,7 @@ type Project struct { DockerMap map[flags.Database]Docker AdvancedOptions map[string]bool AdvancedTemplates AdvancedTemplates + GitOptions flags.Git } type AdvancedTemplates struct { @@ -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 { @@ -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 { @@ -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 } diff --git a/cmd/steps/steps.go b/cmd/steps/steps.go index c212ddad..cf18e0f4 100644 --- a/cmd/steps/steps.go +++ b/cmd/steps/steps.go @@ -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", + }, + }, + }, }, } diff --git a/docs/docs/creating-project/project-init.md b/docs/docs/creating-project/project-init.md index b25f322e..ece3e2e0 100644 --- a/docs/docs/creating-project/project-init.md +++ b/docs/docs/creating-project/project-init.md @@ -15,7 +15,7 @@ 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: @@ -23,6 +23,7 @@ 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. @@ -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. @@ -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 ``` diff --git a/docs/docs/public/blueprint_1.png b/docs/docs/public/blueprint_1.png index e7391c70..e386eebf 100644 Binary files a/docs/docs/public/blueprint_1.png and b/docs/docs/public/blueprint_1.png differ diff --git a/public/blueprint_1.png b/public/blueprint_1.png index e7391c70..e386eebf 100644 Binary files a/public/blueprint_1.png and b/public/blueprint_1.png differ