Skip to content

Commit 1d7a490

Browse files
v3.0 (feature): add simple mock flow for cmf
1 parent 4e9d983 commit 1d7a490

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

cmd/cmd.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/walmartdigital/commit-message-formatter/cmf"
9+
git "github.com/walmartdigital/commit-message-formatter/git"
10+
)
11+
12+
var cmfInstance cmf.CMF
13+
14+
// Root Root cli command
15+
var root = &cobra.Command{
16+
Use: "cmf",
17+
Short: "Commit Message Formatter",
18+
Long: "Generate custom commit message for your repo and standarize your commits log",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
cmfInstance.CommitChanges()
21+
// go git.CheckTree()
22+
// message := template.Run()
23+
// git.Commit(message)
24+
},
25+
}
26+
27+
var version = &cobra.Command{
28+
Use: "version",
29+
Short: "Version cmf",
30+
Long: "Display version of commit message formatter",
31+
Run: func(cmd *cobra.Command, args []string) {
32+
// fmt.Println("CMF - Commit Message Formatter v2.0")
33+
cmfInstance.GetVersion()
34+
},
35+
}
36+
37+
var amend = &cobra.Command{
38+
Use: "amend",
39+
Short: "Amend commit message",
40+
Long: "Amend last commit message",
41+
Run: func(cmd *cobra.Command, args []string) {
42+
cmfInstance.CommitAmend()
43+
// message := template.Run()
44+
// git.Amend(message)
45+
},
46+
}
47+
48+
var boilerplateCMD = &cobra.Command{
49+
Use: "init",
50+
Short: "Create configuration file",
51+
Long: "Create .cmf.yaml configuration file",
52+
Run: func(cmd *cobra.Command, args []string) {
53+
cmfInstance.InitializeProject()
54+
},
55+
}
56+
57+
func Execute() {
58+
if err := root.Execute(); err != nil {
59+
fmt.Fprintln(os.Stderr, err)
60+
os.Exit(1)
61+
}
62+
}
63+
64+
func init() {
65+
cmfInstance = cmf.NewCMF(git.NewGitWrapper())
66+
root.AddCommand(version)
67+
root.AddCommand(boilerplateCMD)
68+
root.AddCommand(amend)
69+
}

cmf/cmf.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmf
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var version = "3.0"
8+
9+
type Repository interface {
10+
CheckWorkspaceChanges()
11+
Commit(message string)
12+
Amend(message string)
13+
}
14+
type cmf struct {
15+
repository Repository
16+
}
17+
18+
type CMF interface {
19+
GetVersion()
20+
CommitChanges()
21+
CommitAmend()
22+
InitializeProject()
23+
}
24+
25+
func NewCMF(repository Repository) CMF {
26+
return &cmf{
27+
repository: repository,
28+
}
29+
}
30+
31+
// GetVersion return current cmf version
32+
func (cmfInstance *cmf) GetVersion() {
33+
fmt.Println("Git - Commit Message Formatter v", version)
34+
}
35+
36+
// CommitChanges perform a commit changes over current repository
37+
func (cmfInstance *cmf) CommitChanges() {
38+
cmfInstance.repository.CheckWorkspaceChanges()
39+
// message := template.Run()
40+
cmfInstance.repository.Commit("message")
41+
}
42+
43+
// CommitAmend perform a commit amend over current repository
44+
func (cmfInstance *cmf) CommitAmend() {
45+
fmt.Println("amend!!")
46+
}
47+
48+
// InitializeProject initialize current directory with a inner cmf template
49+
func (cmfInstance *cmf) InitializeProject() {
50+
fmt.Println("initialize!!")
51+
}

git/git.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package git
2+
3+
import "fmt"
4+
5+
type git struct{}
6+
7+
type Git interface {
8+
CheckWorkspaceChanges()
9+
Commit(message string)
10+
Amend(message string)
11+
}
12+
13+
func NewGitWrapper() Git {
14+
return &git{}
15+
}
16+
17+
func (gitInstance *git) CheckWorkspaceChanges() {
18+
fmt.Println("check wrokspace")
19+
}
20+
21+
func (gitInstance *git) Commit(message string) {
22+
fmt.Println(message)
23+
}
24+
25+
func (gitInstance *git) Amend(message string) {
26+
fmt.Println("amend")
27+
}
28+
29+
// func checkErr(err error) {
30+
// if err != nil {
31+
// fmt.Println(color.Red(err))
32+
// os.Exit(1)
33+
// }
34+
// }
35+
36+
// // CheckTree perform a git tree check
37+
// func CheckTree() {
38+
// cmdGit := exec.Command("git", "diff", "--cached", "--exit-code")
39+
// _, err := cmdGit.Output()
40+
// if err == nil {
41+
// checkErr(errors.New("No changes added to commit"))
42+
// }
43+
// }
44+
45+
// func commit(cmdGit *exec.Cmd, message ...interface{}) {
46+
// fmt.Println("")
47+
// fmt.Println(message...)
48+
// // fmt.Println(color.Gray("-------------------------------"))
49+
// fmt.Println("")
50+
// cmdGit.Stdout = os.Stdout
51+
// cmdGit.Stderr = os.Stderr
52+
// err := cmdGit.Run()
53+
// fmt.Println("")
54+
// // fmt.Println(color.Gray("-------------------------------"))
55+
// if err == nil {
56+
// fmt.Println(color.Green("Done \U0001F604"))
57+
// } else {
58+
// fmt.Println(color.Red("Something went grong \U0001F92F"))
59+
// }
60+
61+
// return
62+
// }
63+
64+
// // Commit execute commit
65+
// func Commit(message string) (err error) {
66+
// ctx := context.Background()
67+
// cmdGit := exec.CommandContext(ctx, "git", "commit", "-m", message)
68+
// commit(cmdGit, "Committing: ", color.Blue(message))
69+
70+
// return
71+
// }
72+
73+
// // Amend execute commit amend
74+
// func Amend(message string) (err error) {
75+
// ctx := context.Background()
76+
// cmdGit := exec.CommandContext(ctx, "git", "commit", "--amend", "-m", message)
77+
// commit(cmdGit, "Amending: ", color.Blue(message))
78+
79+
// return
80+
// }
81+
82+
// // BranchName return current branch name
83+
// func BranchName() string {
84+
// cmdGit := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
85+
// branchName, err := cmdGit.Output()
86+
// if err != nil {
87+
// return ""
88+
// }
89+
90+
// return strings.TrimSuffix(string(branchName), "\n")
91+
// }

0 commit comments

Comments
 (0)