/
cmd_mapper.go
122 lines (104 loc) · 3.37 KB
/
cmd_mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package addcmdadapter
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
"github.com/hekmekk/git-team/src/command/assignments/add"
addeventadapter "github.com/hekmekk/git-team/src/command/assignments/add/cliadapter/event"
"github.com/hekmekk/git-team/src/core/validation"
commandadapter "github.com/hekmekk/git-team/src/shared/cli/commandadapter"
"github.com/hekmekk/git-team/src/shared/cli/effects"
gitconfiglegacy "github.com/hekmekk/git-team/src/shared/gitconfig/impl/legacy"
gitconfigscope "github.com/hekmekk/git-team/src/shared/gitconfig/scope"
)
// Command the add command
func Command() *cli.Command {
return &cli.Command{
Name: "add",
Usage: "Add a new or override an existing alias to co-author assignment",
ArgsUsage: "<alias> <coauthor>",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "force-override", Value: false, Aliases: []string{"f"}, Usage: "Override an existing assignment"},
&cli.BoolFlag{Name: "keep-existing", Value: false, Aliases: []string{"k"}, Usage: "Keep existing assignment"},
},
Action: func(c *cli.Context) error {
forceOverride := c.Bool("force-override")
keepExisting := c.Bool("keep-existing")
if c.NArg() == 0 {
return handleInputFromStdin(forceOverride, keepExisting).Run()
}
if c.NArg() != 2 {
return effects.NewExitErrMsg(errors.New("exactly 2 arguments expected")).Run()
}
args := c.Args()
alias := args.First()
coauthor := args.Get(1)
return commandadapter.Run(policy(&alias, &coauthor, &forceOverride, &keepExisting), addeventadapter.MapEventToEffect)
},
}
}
func handleInputFromStdin(forceOverride bool, keepExisting bool) effects.Effect {
lines, err := readLinesFromStdin()
if err != nil {
return effects.NewExitErrMsg(err)
}
errOccured := false
for _, line := range lines {
argsFromStdin := strings.SplitN(strings.TrimSpace(line), " ", 2)
var effect effects.Effect
if len(argsFromStdin) != 2 {
effect = effects.NewExitErrMsg(errors.New("exactly 2 arguments expected"))
} else {
alias := argsFromStdin[0]
coauthor := argsFromStdin[1]
effect = commandadapter.ApplyPolicy(policy(&alias, &coauthor, &forceOverride, &keepExisting), addeventadapter.MapEventToEffect)
}
err := effect.Run()
if err != nil {
errOccured = true
}
}
if errOccured {
return effects.NewExitErr()
}
return effects.NewExitOk()
}
func readLinesFromStdin() ([]string, error) {
s := bufio.NewScanner(os.Stdin)
lines := []string{}
for s.Scan() {
line := s.Text()
lines = append(lines, line)
}
if s.Err() != nil {
return []string{}, s.Err()
}
return lines, nil
}
func policy(alias *string, coauthor *string, forceOverride *bool, keepExisting *bool) add.Policy {
return add.Policy{
Req: add.AssignmentRequest{
Alias: alias,
Coauthor: coauthor,
ForceOverride: forceOverride,
KeepExisting: keepExisting,
},
Deps: add.Dependencies{
SanityCheckCoauthor: validation.SanityCheckCoauthor,
GitResolveAlias: commandadapter.ResolveAlias,
GitAddAlias: func(alias string, coauthor string) error {
return gitconfiglegacy.ReplaceAll(gitconfigscope.Global, fmt.Sprintf("team.alias.%s", alias), coauthor)
},
GetAnswerFromUser: func(question string) (string, error) {
_, err := os.Stdout.WriteString(question)
if err != nil {
return "", err
}
return bufio.NewReader(os.Stdin).ReadString('\n')
},
},
}
}