-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
116 lines (100 loc) · 2.8 KB
/
start.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
package cli
import (
"errors"
"fmt"
issuectl "github.com/janekbaraniewski/issuectl/pkg"
"github.com/spf13/cobra"
)
type CLIOverwrites struct {
Repos []string
Profile string
IssueBackend string
RepoBackend string
IssueName string
}
var Flags CLIOverwrites
func MergeConfigWithOverwrites(conf issuectl.IssuectlConfig, overwrites *CLIOverwrites) (issuectl.IssuectlConfig, error) {
conf = conf.GetInMemory()
if overwrites.Profile != "" {
if err := conf.UseProfile(issuectl.ProfileName(overwrites.Profile)); err != nil {
return conf, err
}
}
overwriteProfile := conf.GetProfile(conf.GetCurrentProfile())
if overwriteProfile == nil {
return conf, fmt.Errorf("Failed - profile %v not defined.", overwrites.Profile)
}
if overwrites.IssueBackend != "" {
overwriteProfile.IssueBackend = issuectl.BackendConfigName(overwrites.IssueBackend)
}
if overwrites.Repos != nil {
for _, repoName := range overwrites.Repos {
overwriteProfile.Repositories = append(overwriteProfile.Repositories, issuectl.RepoConfigName(repoName))
}
}
if err := conf.UpdateProfile(overwriteProfile); err != nil {
return nil, err
}
return conf, nil
}
func initStartCommand(rootCmd *cobra.Command) {
startCmd := &cobra.Command{
Use: "start [issueID]",
Short: "Start work on issue",
Long: `Create issue work directory. Clone all repositories from current profile. Create branches.`,
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("you must provide exactly 1 argument - issue id")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
config, err := MergeConfigWithOverwrites(issuectl.LoadConfig(), &Flags)
if err != nil {
return err
}
if err := issuectl.StartWorkingOnIssue(Flags.IssueName, config.GetPersistent(), issuectl.IssueID(args[0])); err != nil {
// TODO: rollback changes made by StartWorkingOnIssue
return err
}
return nil
},
}
startCmd.PersistentFlags().StringSliceVarP(
&Flags.Repos,
"repos",
"r",
[]string{},
"A list of repositories to clone",
)
startCmd.PersistentFlags().StringVarP(
&Flags.Profile,
"profile",
"p",
"",
"Name of profile to use for command",
)
startCmd.PersistentFlags().StringVarP(
&Flags.IssueBackend,
"issue-backend",
"",
"",
"Name of issue backend to use for command",
)
startCmd.PersistentFlags().StringVarP(
&Flags.RepoBackend,
"repo-backend",
"",
"",
"Name of repo backend to use for command",
)
startCmd.PersistentFlags().StringVarP(
&Flags.IssueName,
"name",
"n",
"",
"Custom issue name to use [defaults to IssueID]. IssueID will be added as prefix of custom name.",
)
rootCmd.AddCommand(startCmd)
}