Skip to content

Commit

Permalink
fix 🐛: fixed the init flag
Browse files Browse the repository at this point in the history
  • Loading branch information
muandane committed Nov 27, 2023
1 parent 745d881 commit 70a1d2d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
39 changes: 39 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/fatih/color"
"github.com/muandane/goji/pkg/config"
"github.com/spf13/cobra"
)

var (
globalFlag bool
repoFlag bool
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize your goji config",
Long: `Initialize your goji config in your repository or globally in your home directory`,
Run: func(cmd *cobra.Command, args []string) {
color.Set(color.FgYellow)
err := config.InitRepoConfig(globalFlag, repoFlag)
if err != nil {
fmt.Printf("Failed to initialize repository configuration: %v\n", err)
return
}
color.Unset()
},
}

func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().BoolVar(&globalFlag, "global", false, "save the init file to your home directory")
initCmd.Flags().BoolVar(&repoFlag, "repo", false, "save the init file in the repository")
}
13 changes: 0 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

var (
version string
initFlag bool
versionFlag bool
typeFlag string
scopeFlag string
Expand All @@ -34,17 +33,6 @@ var rootCmd = &cobra.Command{
color.Unset()
return
}
if initFlag {
color.Set(color.FgYellow)
err := config.InitRepoConfig()
if err != nil {
fmt.Printf("Failed to initialize repository configuration: %v\n", err)
return
}
color.Unset()
return
}

color.Set(color.FgGreen)
fmt.Printf("Goji v%s is a cli tool to generate conventional commits with emojis.\n", version)
color.Unset()
Expand Down Expand Up @@ -116,7 +104,6 @@ var rootCmd = &cobra.Command{
}

func init() {
rootCmd.Flags().BoolVarP(&initFlag, "init", "i", false, "Generate a configuration file")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information")
rootCmd.Flags().StringVarP(&typeFlag, "type", "t", "", "Specify the type from the config file")
rootCmd.Flags().StringVarP(&scopeFlag, "scope", "s", "", "Specify a custom scope")
Expand Down
31 changes: 20 additions & 11 deletions pkg/config/configInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,16 @@ func GetGitRootDir() (string, error) {
return gitDir, nil
}

func SaveGitmojisToFile(config initConfig, filename string) error {
gitDir, err := GetGitRootDir()

if err != nil {
return err
}

configFile := filepath.Join(gitDir, filename)
func SaveGitmojisToFile(config initConfig, filename string, dir string) error {
configFile := filepath.Join(dir, filename)
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}

return os.WriteFile(configFile, data, 0644)
}

func InitRepoConfig() error {
func InitRepoConfig(global bool, repo bool) error {
gitmojis := AddCustomCommitTypes([]Gitmoji{})
config := initConfig{
Types: gitmojis,
Expand All @@ -64,7 +57,23 @@ func InitRepoConfig() error {
SubjectMaxLength: 50,
}

err := SaveGitmojisToFile(config, ".goji.json")
var dir string
var err error
if global {
dir, err = os.UserHomeDir()
if err != nil {
return err
}
} else if repo {
dir, err = GetGitRootDir()
if err != nil {
return err
}
} else {
return fmt.Errorf("no flag set for location to save configuration file")
}

err = SaveGitmojisToFile(config, ".goji.json", dir)

if err != nil {
return fmt.Errorf("error saving gitmojis to file: %v", err)
Expand Down

0 comments on commit 70a1d2d

Please sign in to comment.