|
| 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 | +} |
0 commit comments