-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathswitch.go
86 lines (70 loc) · 1.51 KB
/
switch.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
package cmd
import (
"os"
"strings"
"github.com/ldez/prm/v3/choose"
"github.com/ldez/prm/v3/config"
"github.com/ldez/prm/v3/types"
)
// Switch from a list.
func Switch(options *types.ListOptions) error {
// get configuration
configs, err := config.ReadFile()
if err != nil {
return err
}
if options.All {
err := changeProject(configs)
if err != nil {
return err
}
} else {
err := changePR(configs)
if err != nil {
return err
}
}
return nil
}
func changeProject(configs []config.Configuration) error {
var projectsConf []config.Configuration
for _, value := range configs {
if len(value.PullRequests) > 0 {
projectsConf = append(projectsConf, value)
}
}
conf, err := choose.Project(projectsConf)
if err != nil || conf == nil {
return err
}
number, err := choose.PullRequest(conf.PullRequests, false)
if err != nil || number <= choose.ExitValue {
return err
}
currentDir, err := os.Getwd()
if err != nil {
return err
}
if !strings.HasPrefix(currentDir, conf.Directory) {
err := os.Chdir(conf.Directory)
if err != nil {
return err
}
}
return Checkout(&types.CheckoutOptions{Number: number})
}
func changePR(configs []config.Configuration) error {
repoDir, err := os.Getwd()
if err != nil {
return err
}
conf, err := config.Find(configs, repoDir)
if err != nil {
return err
}
number, err := choose.PullRequest(conf.PullRequests, false)
if err != nil || number == choose.ExitValue {
return err
}
return Checkout(&types.CheckoutOptions{Number: number})
}