|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/pluralsh/plural/pkg/api" |
| 7 | + "github.com/pluralsh/plural/pkg/manifest" |
| 8 | + "github.com/pluralsh/plural/pkg/utils" |
| 9 | + "github.com/urfave/cli" |
| 10 | +) |
| 11 | + |
| 12 | +func (p *Plural) clusterCommands() []cli.Command { |
| 13 | + return []cli.Command{ |
| 14 | + { |
| 15 | + Name: "list", |
| 16 | + Usage: "lists clusters accessible to your user", |
| 17 | + Action: latestVersion(p.listClusters), |
| 18 | + }, |
| 19 | + { |
| 20 | + Name: "view", |
| 21 | + Usage: "shows info for a cluster", |
| 22 | + Flags: []cli.Flag{ |
| 23 | + cli.StringFlag{ |
| 24 | + Name: "id", |
| 25 | + Usage: "the id of the source cluster", |
| 26 | + }, |
| 27 | + }, |
| 28 | + Action: latestVersion(p.showCluster), |
| 29 | + }, |
| 30 | + { |
| 31 | + Name: "depend", |
| 32 | + Usage: "have a cluster wait for promotion on another cluster", |
| 33 | + Flags: []cli.Flag{ |
| 34 | + cli.StringFlag{ |
| 35 | + Name: "source-id", |
| 36 | + Usage: "the id of the source cluster", |
| 37 | + }, |
| 38 | + cli.StringFlag{ |
| 39 | + Name: "dest-id", |
| 40 | + Usage: "the id of the cluster waiting for promotion", |
| 41 | + }, |
| 42 | + }, |
| 43 | + Action: latestVersion(p.dependCluster), |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "promote", |
| 47 | + Usage: "promote pending upgrades to your cluster", |
| 48 | + Action: latestVersion(p.promoteCluster), |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (p *Plural) listClusters(c *cli.Context) error { |
| 54 | + p.InitPluralClient() |
| 55 | + clusters, err := p.Client.Clusters() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + headers := []string{"ID", "Name", "Provider", "Git Url", "Owner"} |
| 61 | + return utils.PrintTable(clusters, headers, func(c *api.Cluster) ([]string, error) { |
| 62 | + return []string{c.Id, c.Name, c.Provider, c.GitUrl, c.Owner.Email}, nil |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func (p *Plural) showCluster(c *cli.Context) error { |
| 67 | + p.InitPluralClient() |
| 68 | + id := c.String("id") |
| 69 | + if id == "" { |
| 70 | + clusters, err := p.Client.Clusters() |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + project, err := manifest.FetchProject() |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + for _, cluster := range clusters { |
| 80 | + if cluster.Name == project.Cluster && cluster.Owner.Email == project.Owner.Email { |
| 81 | + id = cluster.Id |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + cluster, err := p.Client.Cluster(id) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + fmt.Printf("Cluster %s:\n\n", cluster.Id) |
| 92 | + |
| 93 | + utils.PrintAttributes(map[string]string{ |
| 94 | + "Id": cluster.Id, |
| 95 | + "Name": cluster.Name, |
| 96 | + "Provider": cluster.Provider, |
| 97 | + "Git Url": cluster.GitUrl, |
| 98 | + "Owner": cluster.Owner.Email, |
| 99 | + }) |
| 100 | + |
| 101 | + fmt.Println("") |
| 102 | + if len(cluster.UpgradeInfo) > 0 { |
| 103 | + fmt.Printf("Pending Upgrades:\n\n") |
| 104 | + headers := []string{"Repository", "Count"} |
| 105 | + return utils.PrintTable(cluster.UpgradeInfo, headers, func(c *api.UpgradeInfo) ([]string, error) { |
| 106 | + return []string{c.Installation.Repository.Name, fmt.Sprintf("%d", c.Count)}, nil |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Println("No pending upgrades") |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (p *Plural) dependCluster(c *cli.Context) error { |
| 115 | + p.InitPluralClient() |
| 116 | + source, dest := c.String("source-id"), c.String("dest-id") |
| 117 | + if err := p.Client.CreateDependency(source, dest); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + utils.Highlight("Cluster %s will now delegate upgrades to %s", dest, source) |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (p *Plural) promoteCluster(c *cli.Context) error { |
| 126 | + p.InitPluralClient() |
| 127 | + if err := p.Client.PromoteCluster(); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + utils.Success("Upgrades promoted!") |
| 132 | + return nil |
| 133 | +} |
0 commit comments