Skip to content

Commit 0026980

Browse files
Rodrigo NavarroRodrigo Navarro
authored andcommitted
feat(cmf): add initializeProject logic, to create a cmf file on the working directory
1 parent 486b489 commit 0026980

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

cmf/cmf.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package cmf
22

33
import (
4+
"bufio"
45
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"strings"
10+
11+
color "github.com/logrusorgru/aurora/v3"
512
)
613

714
const version = "3.0"
@@ -38,6 +45,27 @@ type CMF interface {
3845
InitializeProject()
3946
}
4047

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+
4169
func NewCMF(repository Repository, templateManager TemplateManager, fsManager FS) CMF {
4270
return &cmf{
4371
repository: repository,
@@ -85,5 +113,16 @@ func (cmfInstance *cmf) CommitAmend() {
85113

86114
// InitializeProject initialize current directory with a inner cmf template
87115
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+
}
89128
}

0 commit comments

Comments
 (0)