Skip to content

Commit

Permalink
添加二级命令commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blue-duty committed Feb 20, 2023
1 parent 49b186e commit f336ebd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 67 deletions.
124 changes: 58 additions & 66 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd

import (
"bufio"
"fmt"
"github.com/AlecAivazis/survey/v2"
tm "github.com/buger/goterm"
"github.com/go-git/go-git/v5"
Expand All @@ -16,82 +15,74 @@ import (
"time"
)

var rootCmd = &cobra.Command{
Use: "got",
Short: "got is a simple git tool",
Long: `got is a simple git tool,
which can help you to commit and push your code easily.`,
}

var cmd = &cobra.Command{
Use: "ggit",
Short: "A simple git tool",
Use: "commit",
Short: "commit your code",
DisableAutoGenTag: true,
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
// 创建选择问题
question := &survey.Select{
Message: "Select Your Action:",
Options: []string{"Commit", "Commit And Push", "Push"},
}

// 提示用户选择选项
var answer string
if err := survey.AskOne(question, &answer); err != nil {
fmt.Println("Failed to prompt user:", err)
os.Exit(1)
_, err := tm.Println("The following is the file status list:")
cobra.CheckErr(err)
commit(fileStatusList)
tm.Flush()
var fileIndex string
prompt := &survey.Input{
Message: "Please input the serial number of the file you want to commit:",
}

// 根据用户选择的选项提示用户输入
switch answer {
case "Commit":
_, err := tm.Println("The following is the file status list:")
err = survey.AskOne(prompt, &fileIndex)
cobra.CheckErr(err)
// split by ,
for _, index := range strings.Split(fileIndex, ",") {
i, err := strconv.Atoi(strings.TrimSpace(index))
cobra.CheckErr(err)
commit(fileStatusList)
tm.Flush()
var fileIndex string
prompt := &survey.Input{
Message: "Please input the serial number of the file you want to commit:",
}
err = survey.AskOne(prompt, &fileIndex)
_, err = tm.Println(fileStatusList[i-1].file)
cobra.CheckErr(err)
// split by ,
for _, index := range strings.Split(fileIndex, ",") {
i, err := strconv.Atoi(strings.TrimSpace(index))
cobra.CheckErr(err)
_, err = tm.Println(fileStatusList[i-1].file)
cobra.CheckErr(err)
tm.Flush()
tm.Flush()

// add file to staging area
_, err = workTree.Add(fileStatusList[i-1].file)
}
// commit input email, name and message
//var email, name, message string
//prompt = &survey.Input{
// Message: "Please input your email:",
//}
//err = survey.AskOne(prompt, &email)
//cobra.CheckErr(err)
//prompt = &survey.Input{
// Message: "Please input your name:",
//}
//err = survey.AskOne(prompt, &name)
//cobra.CheckErr(err)

var message string
prompt = &survey.Input{
Message: "Please input your commit message:",
}
err = survey.AskOne(prompt, &message)
cobra.CheckErr(err)
_, err = workTree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: globalName,
Email: globalMail,
When: time.Now(),
},
})
cobra.CheckErr(err)
// add file to staging area
_, err = workTree.Add(fileStatusList[i-1].file)
}
// commit input email, name and message
//var email, name, message string
//prompt = &survey.Input{
// Message: "Please input your email:",
//}
//err = survey.AskOne(prompt, &email)
//cobra.CheckErr(err)
//prompt = &survey.Input{
// Message: "Please input your name:",
//}
//err = survey.AskOne(prompt, &name)
//cobra.CheckErr(err)

var message string
prompt = &survey.Input{
Message: "Please input your commit message:",
}
err = survey.AskOne(prompt, &message)
cobra.CheckErr(err)
_, err = workTree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: globalName,
Email: globalMail,
When: time.Now(),
},
})
cobra.CheckErr(err)
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := cmd.Execute()
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -144,13 +135,14 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ggit.yaml)")
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.got.yaml)")

fileStatusList = gitStatus()
globalName, globalMail = getGitConfig()
// Cobra also supports local flags, which will only run
// when this action is called directly.
cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
//cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.AddCommand(cmd)
}

// git the status of the current repository
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {

cmd.Execute()

//path := "/home/duty/go/src/ggit"
//path := "/home/duty/go/src/got"
//
//r, err := git.PlainOpen(path)
//if err != nil {
Expand Down

0 comments on commit f336ebd

Please sign in to comment.