-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.go
68 lines (60 loc) · 1.81 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
package main
import (
"github.com/mateothegreat/go-multilog/multilog"
"github.com/polyrepopro/api/config"
"github.com/polyrepopro/api/workspaces"
"github.com/spf13/cobra"
)
func init() {
switchCommand.Flags().StringP("branch", "b", "", "The branch to switch to.")
switchCommand.MarkFlagRequired("branch")
root.AddCommand(switchCommand)
}
var switchCommand = &cobra.Command{
Use: "switch",
Short: "switch the branch for each repository in the workspace",
Long: "switch the branch for each repository in the workspace",
Run: func(cmd *cobra.Command, args []string) {
workspaceName, err := cmd.Flags().GetString("workspace")
if err != nil {
multilog.Fatal("workspace.switch", "failed to get workspace", map[string]interface{}{
"error": err,
})
}
branch, err := cmd.Flags().GetString("branch")
if err != nil {
multilog.Fatal("workspace.switch", "failed to get branch", map[string]interface{}{
"error": err,
})
}
cfg, err := config.GetRelativeConfig()
if err != nil {
multilog.Fatal("workspace.switch", "failed to get config", map[string]interface{}{
"error": err,
})
}
workspace, err := cfg.GetWorkspace(workspaceName)
if err != nil {
multilog.Fatal("workspace.switch", "failed to get workspace from config", map[string]interface{}{
"workspace": workspaceName,
"error": err,
})
}
errs := workspaces.Switch(workspaces.SwitchArgs{
Workspace: workspace,
Branch: branch,
})
if len(errs) > 0 {
multilog.Fatal("workspace.switch", "switch failed", map[string]interface{}{
"workspace": workspace.Name,
"path": workspace.Path,
"branch": branch,
"errors": errs,
})
}
multilog.Info("workspace.switch", "switched", map[string]interface{}{
"branch": branch,
"repositories": len(*workspace.Repositories),
})
},
}