Skip to content

Commit

Permalink
🚸 Save prompt for config
Browse files Browse the repository at this point in the history
To avoid scary messages from Git if the user use gut and git,
gut prompt for an username and an email on commit and set it to the local config.
  • Loading branch information
julien040 committed Jan 10, 2023
1 parent 7c3476e commit 7b882e6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/controller/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type emoji struct {
Description string
}

// Some emojis are missing.
// Should be fixed later
var gitEmoji = []emoji{
{"🎉", ":tada:", "Initial commit"},
{"✨", ":sparkles:", "Introduce new features"},
Expand Down Expand Up @@ -90,13 +92,22 @@ var gitEmoji = []emoji{
}

func Save(cmd *cobra.Command, args []string) {
// Get where the command has been called
wd, err := os.Getwd()
if err != nil {
exitOnError("Sorry, I can't get the current working directory", err)
}

// Check if the current directory is a git repository
checkIfGitRepoInitialized(wd)

// Check if the user config is set
verifUserConfig(wd)

// Get the flag from the cmd
title := cmd.Flag("title").Value.String()
message := cmd.Flag("message").Value.String()

var answers struct {
Type int
Titre string
Expand Down Expand Up @@ -133,7 +144,12 @@ func Save(cmd *cobra.Command, args []string) {
if err != nil {
exitOnError("Sorry, I can't get your answers", err)
}

// Append the title to the body because git only accept a message.
// However, it's common that the first line is the title and the rest the body
commitMessage := computeCommitMessage(answers)

// Commit the changes
Result, err := executor.Commit(wd, commitMessage)
if err != nil {
exitOnError("Error while committing", err)
Expand Down Expand Up @@ -170,3 +186,49 @@ func computeCommitMessage(answers struct {
message += gitEmoji[answers.Type].Emoji + " " + answers.Titre + "\n" + answers.Description
return message
}

// Check if the username and email for commits are set.
//
// If not, prompt the user to set it
func verifUserConfig(path string) {
// Get User config for the path
username, email, err := executor.GetUserConfig(path)
if err != nil {
exitOnError("Sorry, I can't get your user config", err)
}
if username != "" && email != "" {
return
}
print.Message("Hi there, I'm missing some information about you. Let's fix that!", print.Info)
var answers struct {
Username string
Email string
}
var qs []*survey.Question
if username == "" {
qs = append(qs, &survey.Question{
Name: "Username",
Prompt: &survey.Input{Message: "Username", Help: "How do you want to be called in commits messages?"},

Validate: survey.Required,
})
}
if email == "" {
qs = append(qs, &survey.Question{
Name: "Email",
Prompt: &survey.Input{Message: "Email", Help: "What is your email?(Be careful, this is public on GitHub)"},
Validate: survey.Required,
})
}
answers.Username = username
answers.Email = email
err = survey.Ask(qs, &answers)
if err != nil {
exitOnError("Sorry, I can't get your answers", err)
}
err = executor.SetUserConfig(path, answers.Username, answers.Email)
if err != nil {
exitOnError("Sorry, I can't set your user config", err)
}

}
12 changes: 12 additions & 0 deletions src/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ func OpenRepo(path string) (*git.Repository, error) {
return git.PlainOpen(path)
}

func GetUserConfig(path string) (string, string, error) {
repo, err := OpenRepo(path)
if err != nil {
return "", "", err
}
config, err := repo.Config()
if err != nil {
return "", "", err
}
return config.User.Name, config.User.Email, nil
}

func SetUserConfig(path string, name string, email string) error {
repo, err := OpenRepo(path)
if err != nil {
Expand Down

0 comments on commit 7b882e6

Please sign in to comment.