/
cd_repositories.go
136 lines (119 loc) · 3.84 KB
/
cd_repositories.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
129
130
131
132
133
134
135
136
package plural
import (
"fmt"
gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/samber/lo"
"github.com/urfave/cli"
)
func (p *Plural) cdRepositories() cli.Command {
return cli.Command{
Name: "repositories",
Subcommands: p.cdRepositoriesCommands(),
Usage: "manage CD repositories",
}
}
func (p *Plural) cdRepositoriesCommands() []cli.Command {
return []cli.Command{
{
Name: "list",
Action: latestVersion(p.handleListCDRepositories),
Usage: "list repositories",
},
{
Name: "create",
Action: latestVersion(p.handleCreateCDRepository),
Flags: []cli.Flag{
cli.StringFlag{Name: "url", Usage: "git repo url", Required: true},
cli.StringFlag{Name: "private-key", Usage: "git repo private key"},
cli.StringFlag{Name: "passphrase", Usage: "git repo passphrase"},
cli.StringFlag{Name: "username", Usage: "git repo username"},
cli.StringFlag{Name: "password", Usage: "git repo password"},
},
Usage: "create repository",
},
{
Name: "update",
ArgsUsage: "REPO_ID",
Action: latestVersion(requireArgs(p.handleUpdateCDRepository, []string{"REPO_ID"})),
Flags: []cli.Flag{
cli.StringFlag{Name: "url", Usage: "git repo url", Required: true},
cli.StringFlag{Name: "private-key", Usage: "git repo private key"},
cli.StringFlag{Name: "passphrase", Usage: "git repo passphrase"},
cli.StringFlag{Name: "username", Usage: "git repo username"},
cli.StringFlag{Name: "password", Usage: "git repo password"},
},
Usage: "update repository",
},
}
}
func (p *Plural) handleListCDRepositories(_ *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
repos, err := p.ConsoleClient.ListRepositories()
if err != nil {
return err
}
if repos == nil {
return fmt.Errorf("returned objects list [ListRepositories] is nil")
}
headers := []string{"ID", "URL", "Status", "Error"}
return utils.PrintTable(repos.GitRepositories.Edges, headers, func(r *gqlclient.GitRepositoryEdgeFragment) ([]string, error) {
health := "UNKNOWN"
if r.Node.Health != nil {
health = string(*r.Node.Health)
}
return []string{r.Node.ID, r.Node.URL, health, lo.FromPtr(r.Node.Error)}, nil
})
}
func (p *Plural) handleCreateCDRepository(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
url := c.String("url")
repo, err := p.ConsoleClient.CreateRepository(url, getFlag(c.String("privateKey")),
getFlag(c.String("passphrase")), getFlag(c.String("username")), getFlag(c.String("password")))
if err != nil {
return err
}
headers := []string{"ID", "URL"}
return utils.PrintTable([]gqlclient.GitRepositoryFragment{*repo.CreateGitRepository}, headers, func(r gqlclient.GitRepositoryFragment) ([]string, error) {
return []string{r.ID, r.URL}, nil
})
}
func (p *Plural) handleUpdateCDRepository(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
repoId := c.Args().Get(0)
attr := gqlclient.GitAttributes{
URL: c.String("url"),
}
if c.String("private-key") != "" {
attr.PrivateKey = lo.ToPtr(c.String("private-key"))
}
if c.String("passphrase") != "" {
attr.Passphrase = lo.ToPtr(c.String("passphrase"))
}
if c.String("password") != "" {
attr.Password = lo.ToPtr(c.String("password"))
}
if c.String("username") != "" {
attr.Username = lo.ToPtr(c.String("username"))
}
repo, err := p.ConsoleClient.UpdateRepository(repoId, attr)
if err != nil {
return err
}
headers := []string{"ID", "URL"}
return utils.PrintTable([]gqlclient.GitRepositoryFragment{*repo.UpdateGitRepository}, headers, func(r gqlclient.GitRepositoryFragment) ([]string, error) {
return []string{r.ID, r.URL}, nil
})
}
func getFlag(s string) *string {
if s == "" {
return nil
}
return &s
}