Skip to content

Commit

Permalink
add deployments repositories create command
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Sep 26, 2023
1 parent ee94dea commit 5084c29
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
38 changes: 30 additions & 8 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ func (p *Plural) cdRepositoriesCommands() []cli.Command {
{
Name: "create",
Action: latestVersion(p.handleCreateCDRepository),
Usage: "create repository",
Flags: []cli.Flag{
cli.StringFlag{Name: "url", Usage: "git repo url"},
cli.StringFlag{Name: "privateKey", 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",
},
}
}

func (p *Plural) cdServiceCommands() []cli.Command {
return []cli.Command{
{
Name: "list",
ArgsUsage: "CLUSTER_ID",
Action: latestVersion(requireArgs(p.handleListClusterServices, []string{"CLUSTER_ID"})),
Usage: "list cluster services",
Name: "list",
Action: latestVersion(p.handleListClusterServices),
Usage: "list cluster services",
},
}
}
Expand All @@ -71,8 +77,19 @@ func (p *Plural) cdClusterCommands() []cli.Command {
}

func (p *Plural) handleCreateCDRepository(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
repo, err := p.ConsoleClient.CreateRepository(c.String("url"), getFlag(c.String("privateKey")),
getFlag(c.String("passphrase")), getFlag(c.String("username")), getFlag(c.String("password")))
if err != nil {
return err
}

return nil
headers := []string{"ID", "URL"}
return utils.PrintTable([]console.GitRepository{*repo}, headers, func(r console.GitRepository) ([]string, error) {
return []string{r.Id, r.URL}, nil
})
}

func (p *Plural) handleListCDRepositories(c *cli.Context) error {
Expand All @@ -93,8 +110,6 @@ func (p *Plural) handleListClusterServices(c *cli.Context) error {
return utils.PrintTable(sd, headers, func(sd console.ServiceDeployment) ([]string, error) {
return []string{sd.Id, sd.Name, sd.Namespace, sd.Git.Ref, sd.Git.Folder}, nil
})

return nil
}

func (p *Plural) handleListClusters(c *cli.Context) error {
Expand All @@ -114,3 +129,10 @@ func (p *Plural) handleListClusters(c *cli.Context) error {

return nil
}

func getFlag(s string) *string {
if s == "" {
return nil
}
return &s
}
1 change: 1 addition & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type consoleClient struct {
type ConsoleClient interface {
ListClusters() ([]Cluster, error)
ListClusterServices() ([]ServiceDeployment, error)
CreateRepository(url string, privateKey, passphrase, username, password *string) (*GitRepository, error)
}

func NewConsoleClient(token, url string) (ConsoleClient, error) {
Expand Down
22 changes: 22 additions & 0 deletions pkg/console/repositories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package console

import (
gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural/pkg/api"
)

func (c *consoleClient) CreateRepository(url string, privateKey, passphrase, username, password *string) (*GitRepository, error) {
attrs := gqlclient.GitAttributes{
URL: url,
PrivateKey: privateKey,
Passphrase: passphrase,
Username: username,
Password: password,
}
result, err := c.pluralClient.CreateGitRepository(c.ctx, attrs)
if err != nil {
return nil, api.GetErrorResponse(err, "CreateGitRepository")
}

return convertGitRepository(result.CreateGitRepository), nil
}

0 comments on commit 5084c29

Please sign in to comment.