github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/cd_repositories.go (about) 1 package plural 2 3 import ( 4 "fmt" 5 6 gqlclient "github.com/pluralsh/console-client-go" 7 "github.com/pluralsh/plural-cli/pkg/utils" 8 "github.com/samber/lo" 9 "github.com/urfave/cli" 10 ) 11 12 func (p *Plural) cdRepositories() cli.Command { 13 return cli.Command{ 14 Name: "repositories", 15 Subcommands: p.cdRepositoriesCommands(), 16 Usage: "manage CD repositories", 17 } 18 } 19 20 func (p *Plural) cdRepositoriesCommands() []cli.Command { 21 return []cli.Command{ 22 { 23 Name: "list", 24 Action: latestVersion(p.handleListCDRepositories), 25 Usage: "list repositories", 26 }, 27 { 28 Name: "create", 29 Action: latestVersion(p.handleCreateCDRepository), 30 Flags: []cli.Flag{ 31 cli.StringFlag{Name: "url", Usage: "git repo url", Required: true}, 32 cli.StringFlag{Name: "private-key", Usage: "git repo private key"}, 33 cli.StringFlag{Name: "passphrase", Usage: "git repo passphrase"}, 34 cli.StringFlag{Name: "username", Usage: "git repo username"}, 35 cli.StringFlag{Name: "password", Usage: "git repo password"}, 36 }, 37 Usage: "create repository", 38 }, 39 { 40 Name: "update", 41 ArgsUsage: "REPO_ID", 42 Action: latestVersion(requireArgs(p.handleUpdateCDRepository, []string{"REPO_ID"})), 43 Flags: []cli.Flag{ 44 cli.StringFlag{Name: "url", Usage: "git repo url", Required: true}, 45 cli.StringFlag{Name: "private-key", Usage: "git repo private key"}, 46 cli.StringFlag{Name: "passphrase", Usage: "git repo passphrase"}, 47 cli.StringFlag{Name: "username", Usage: "git repo username"}, 48 cli.StringFlag{Name: "password", Usage: "git repo password"}, 49 }, 50 Usage: "update repository", 51 }, 52 } 53 } 54 55 func (p *Plural) handleListCDRepositories(_ *cli.Context) error { 56 if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { 57 return err 58 } 59 repos, err := p.ConsoleClient.ListRepositories() 60 if err != nil { 61 return err 62 } 63 if repos == nil { 64 return fmt.Errorf("returned objects list [ListRepositories] is nil") 65 } 66 headers := []string{"ID", "URL", "Status", "Error"} 67 return utils.PrintTable(repos.GitRepositories.Edges, headers, func(r *gqlclient.GitRepositoryEdgeFragment) ([]string, error) { 68 health := "UNKNOWN" 69 if r.Node.Health != nil { 70 health = string(*r.Node.Health) 71 } 72 return []string{r.Node.ID, r.Node.URL, health, lo.FromPtr(r.Node.Error)}, nil 73 }) 74 75 } 76 77 func (p *Plural) handleCreateCDRepository(c *cli.Context) error { 78 if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { 79 return err 80 } 81 url := c.String("url") 82 repo, err := p.ConsoleClient.CreateRepository(url, getFlag(c.String("privateKey")), 83 getFlag(c.String("passphrase")), getFlag(c.String("username")), getFlag(c.String("password"))) 84 if err != nil { 85 return err 86 } 87 88 headers := []string{"ID", "URL"} 89 return utils.PrintTable([]gqlclient.GitRepositoryFragment{*repo.CreateGitRepository}, headers, func(r gqlclient.GitRepositoryFragment) ([]string, error) { 90 return []string{r.ID, r.URL}, nil 91 }) 92 } 93 94 func (p *Plural) handleUpdateCDRepository(c *cli.Context) error { 95 if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { 96 return err 97 } 98 repoId := c.Args().Get(0) 99 100 attr := gqlclient.GitAttributes{ 101 URL: c.String("url"), 102 } 103 104 if c.String("private-key") != "" { 105 attr.PrivateKey = lo.ToPtr(c.String("private-key")) 106 } 107 108 if c.String("passphrase") != "" { 109 attr.Passphrase = lo.ToPtr(c.String("passphrase")) 110 } 111 112 if c.String("password") != "" { 113 attr.Password = lo.ToPtr(c.String("password")) 114 } 115 116 if c.String("username") != "" { 117 attr.Username = lo.ToPtr(c.String("username")) 118 } 119 120 repo, err := p.ConsoleClient.UpdateRepository(repoId, attr) 121 if err != nil { 122 return err 123 } 124 125 headers := []string{"ID", "URL"} 126 return utils.PrintTable([]gqlclient.GitRepositoryFragment{*repo.UpdateGitRepository}, headers, func(r gqlclient.GitRepositoryFragment) ([]string, error) { 127 return []string{r.ID, r.URL}, nil 128 }) 129 } 130 131 func getFlag(s string) *string { 132 if s == "" { 133 return nil 134 } 135 return &s 136 }