-
Notifications
You must be signed in to change notification settings - Fork 6
/
push.go
128 lines (103 loc) · 2.42 KB
/
push.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
123
124
125
126
127
128
/*
Copyright 2021 Adevinta
*/
package cmd
import (
"fmt"
"path/filepath"
"github.com/spf13/cobra"
"github.com/adevinta/vulcan-api/cmd/vulcan-cli/cli"
)
var PushTeam = &cobra.Command{
Use: `push <teams_dir> <team_name>`,
Short: `Uploads the info of a team with the one that is defined in files, overwriting the remote info`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return runPushTeam(args, apiClient)
},
}
func init() {
PushTeam.Flags().BoolVarP(&force, "force", "f", false, "Push changes to remote, otherwise performs a dry run only")
rootCmd.AddCommand(PushTeam)
}
func runPushTeam(args []string, apiClient *cli.CLI) error {
path := args[0]
name := args[1]
localTeams, err := readLocalTeams(path, name)
if err != nil {
return err
}
remoteTeams, err := readRemoteTeams(name, apiClient)
if err != nil {
return err
}
journal, err := cli.NewJournal(localTeams, remoteTeams, apiClient)
if err != nil {
return err
}
err = journal.BuildModifications()
if err != nil {
return err
}
fmt.Println(journal)
if force {
fmt.Println("[*] Applying")
if err := journal.Apply(); err != nil {
return err
}
fmt.Println("[*] Done. Updating local files")
return updateLocalTeams(path, localTeams)
}
fmt.Println("[*] Use force option to execute it")
return nil
}
func readLocalTeams(path, name string) ([]*cli.Team, error) {
var teams []*cli.Team
if name == allTeamsToken {
t, err := cli.ReadLocalTeams(path)
if err != nil {
return nil, err
}
teams = append(teams, t...)
} else {
t, err := cli.ReadLocalTeam(filepath.Join(path, name))
if err != nil {
return nil, err
}
teams = append(teams, t)
}
return teams, nil
}
func updateLocalTeams(path string, teams []*cli.Team) error {
for _, t := range teams {
assets, err := apiClient.Assets(t.ID)
if err != nil {
return err
}
t.Assets = assets
groups, err := apiClient.Groups(t.ID)
if err != nil {
return err
}
t.Groups = groups
orphans, err := apiClient.OrphanAssets(assets, t.Groups)
if err != nil {
return err
}
t.OrphanAssets = orphans
foreigns, err := apiClient.ForeignAssets(assets, t.Groups)
if err != nil {
return err
}
t.ForeignAssets = foreigns
members, err := apiClient.Members(t.ID)
if err != nil {
return err
}
t.Members = members
if err := t.WriteLocal(filepath.Join(path, t.Name)); err != nil {
return err
}
}
return nil
}