Skip to content

Commit 2992746

Browse files
author
R0n0066
committed
feature (cmd): Add commands
1 parent 8ae3320 commit 2992746

File tree

3 files changed

+198
-2
lines changed

3 files changed

+198
-2
lines changed

cmd/jira.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright © 2018 Rodrigo Navarro <rodrigonavarro23@gmail.com>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package cmd
22+
23+
import (
24+
"fmt"
25+
26+
"github.com/spf13/cobra"
27+
"github.com/spf13/viper"
28+
)
29+
30+
// versionCmd represents the version command
31+
var versionCmd = &cobra.Command{
32+
Use: "jira",
33+
Short: "Display current version",
34+
Long: `version Display the current version of cfm`,
35+
PreRun: jiraPreRun,
36+
Run: func(cmd *cobra.Command, args []string) {
37+
p := parseTemplate(viper.GetString("template"))
38+
fmt.Println(p)
39+
},
40+
}
41+
42+
func jiraPreRun(cmd *cobra.Command, args []string) {
43+
viper.AddConfigPath("./")
44+
viper.SetConfigType("yaml")
45+
viper.SetConfigName("jira")
46+
err := viper.ReadInConfig()
47+
checkErr(err)
48+
49+
promptList()
50+
}
51+
52+
func init() {
53+
rootCmd.AddCommand(versionCmd)
54+
}

cmd/utils.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright © 2018 Rodrigo Navarro <rodrigonavarro23@gmail.com>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package cmd
22+
23+
import (
24+
"errors"
25+
"fmt"
26+
"os"
27+
"regexp"
28+
"strings"
29+
"time"
30+
31+
"github.com/fatih/color"
32+
"github.com/manifoldco/promptui"
33+
"github.com/spf13/viper"
34+
gitconfig "github.com/tcnksm/go-gitconfig"
35+
// "github.com/tcnksm/go-gitconfig"
36+
"gopkg.in/src-d/go-git.v4"
37+
"gopkg.in/src-d/go-git.v4/plumbing/object"
38+
)
39+
40+
func checkErr(err error) {
41+
if err != nil {
42+
color.Set(color.FgMagenta)
43+
defer color.Unset()
44+
fmt.Print(err)
45+
os.Exit(1)
46+
}
47+
}
48+
49+
func parseTemplate(template string) string {
50+
for _, v := range variables {
51+
template = strings.Replace(template, "{{"+v.Key+"}}", v.Value, -1)
52+
}
53+
54+
return template
55+
}
56+
57+
func promptList() {
58+
settings := viper.AllSettings()
59+
prompts := settings["prompt"].([]interface{})
60+
for _, v := range prompts {
61+
result := ""
62+
pr := v.(map[interface{}]interface{})
63+
if options := pr["OPTIONS"]; options == nil {
64+
validate := func(input string) error {
65+
if input == "" {
66+
return errors.New("Empty value")
67+
}
68+
return nil
69+
}
70+
p := promptui.Prompt{
71+
Label: pr["LABEL"],
72+
// Templates: templates,
73+
Validate: validate,
74+
}
75+
r, err := p.Run()
76+
checkErr(err)
77+
result = r
78+
} else {
79+
80+
templates := &promptui.SelectTemplates{
81+
Label: "{{ . | bold }}",
82+
Active: "\U0001F449 {{ .Value | cyan }} {{ .Desc | faint }}",
83+
Inactive: " {{ .Value }} {{ .Desc | faint }}",
84+
Selected: "\U0001F44D {{ .Value | bold }}",
85+
}
86+
optList := pr["OPTIONS"].([]interface{})
87+
var opts []*option
88+
for _, o := range optList {
89+
op := o.(map[interface{}]interface{})
90+
opts = append(opts, &option{Value: op["VALUE"].(string), Desc: op["DESC"].(string)})
91+
}
92+
p := promptui.Select{
93+
Label: pr["LABEL"],
94+
Items: opts,
95+
Templates: templates,
96+
}
97+
i, _, err := p.Run()
98+
checkErr(err)
99+
result = opts[i].Value
100+
}
101+
102+
variables = append(variables, keyValue{
103+
Key: pr["KEY"].(string),
104+
Value: result,
105+
})
106+
}
107+
}
108+
109+
func commit(message string) (err error) {
110+
directory, err := os.Getwd()
111+
checkErr(err)
112+
113+
// Opens an already existent repository.
114+
r, err := git.PlainOpen(directory)
115+
checkErr(err)
116+
117+
w, err := r.Worktree()
118+
checkErr(err)
119+
120+
s, err := w.Status()
121+
checkErr(err)
122+
123+
m, _ := regexp.MatchString("A .*", s.String())
124+
125+
if m {
126+
username, err := gitconfig.Username()
127+
email, err := gitconfig.Email()
128+
129+
_, err = w.Commit(message, &git.CommitOptions{
130+
Author: &object.Signature{
131+
Name: username,
132+
Email: email,
133+
When: time.Now(),
134+
},
135+
})
136+
checkErr(err)
137+
} else {
138+
checkErr(errors.New("no changes added to commit"))
139+
}
140+
141+
return
142+
}

cmd/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
// versionCmd represents the version command
30-
var versionCmd = &cobra.Command{
30+
var jiraCmd = &cobra.Command{
3131
Use: "version",
3232
Short: "Display current version",
3333
Long: `version Display the current version of cfm`,
@@ -38,5 +38,5 @@ var versionCmd = &cobra.Command{
3838
}
3939

4040
func init() {
41-
rootCmd.AddCommand(versionCmd)
41+
rootCmd.AddCommand(jiraCmd)
4242
}

0 commit comments

Comments
 (0)