|
1 | 1 | package cmf
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
4 | 5 | "fmt"
|
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + color "github.com/logrusorgru/aurora/v3" |
5 | 12 | )
|
6 | 13 |
|
7 | 14 | const version = "3.0"
|
@@ -38,6 +45,27 @@ type CMF interface {
|
38 | 45 | InitializeProject()
|
39 | 46 | }
|
40 | 47 |
|
| 48 | +func askForConfirmation(s string) bool { |
| 49 | + reader := bufio.NewReader(os.Stdin) |
| 50 | + |
| 51 | + for { |
| 52 | + fmt.Printf("%s [y/n]: ", s) |
| 53 | + |
| 54 | + response, err := reader.ReadString('\n') |
| 55 | + if err != nil { |
| 56 | + log.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + response = strings.ToLower(strings.TrimSpace(response)) |
| 60 | + |
| 61 | + if response == "y" || response == "yes" { |
| 62 | + return true |
| 63 | + } else if response == "n" || response == "no" { |
| 64 | + return false |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
41 | 69 | func NewCMF(repository Repository, templateManager TemplateManager, fsManager FS) CMF {
|
42 | 70 | return &cmf{
|
43 | 71 | repository: repository,
|
@@ -85,5 +113,16 @@ func (cmfInstance *cmf) CommitAmend() {
|
85 | 113 |
|
86 | 114 | // InitializeProject initialize current directory with a inner cmf template
|
87 | 115 | func (cmfInstance *cmf) InitializeProject() {
|
88 |
| - fmt.Println("initialize!!") |
| 116 | + if askForConfirmation("This action will create a new .cmf.yaml file on your working directory. Do you want to continue?") { |
| 117 | + currentDirectory, _ := cmfInstance.fs.GetCurrentDirectory() |
| 118 | + cmfFilePath := currentDirectory + "/" + defaultCMFFile |
| 119 | + cmfFile, _ := cmfInstance.fs.GetFileFromVirtualFS(defaultYamlFile) |
| 120 | + err := ioutil.WriteFile(cmfFilePath, []byte(cmfFile), 0644) |
| 121 | + if err != nil { |
| 122 | + fmt.Println(color.Red("Cannot create .cmf.yaml file")) |
| 123 | + os.Exit(2) |
| 124 | + } |
| 125 | + |
| 126 | + fmt.Println(color.Green("You can customize your flow, just visit: https://github.com/walmartdigital/commit-message-formatter. Enjoy!")) |
| 127 | + } |
89 | 128 | }
|
0 commit comments