/
list-clusters.go
executable file
·69 lines (56 loc) · 1.93 KB
/
list-clusters.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
package cmd
import (
"fmt"
"os"
"text/tabwriter"
ead "github.com/silinternational/ecs-ami-deploy/v2"
"github.com/spf13/cobra"
)
// latestAMICmd represents the ec2 latest-ami command
var listClustersCmd = &cobra.Command{
Use: "list-clusters",
Short: "List all ECS Clusters and AMI status",
Long: "Command returns a list of ECS clusters along with AMI and is latest status",
Run: func(cmd *cobra.Command, args []string) {
listClusters()
},
}
func init() {
rootCmd.AddCommand(listClustersCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// ecsListInstanceIPsCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// ecsListInstanceIPsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// ecsReplaceInstancesCmd.Flags().StringVarP(&cluster, "cluster", "c", "", "ECS cluster name")
// latestAMICmd.Flags().StringVarP(&AMIFilter, "filter", "f", ead.DefaultAMIFilter, "AMI name filter")
}
func listClusters() {
initAwsCfg()
upgrader, err := ead.NewUpgrader(AwsCfg, &ead.Config{AMIFilter: AMIFilter})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
latestAMI, err := upgrader.LatestAMI()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
list, err := upgrader.ListClusters()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("\nLatest AMI: %s released %s\n\n", *latestAMI.Name, *latestAMI.CreationDate)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
_, _ = fmt.Fprintln(w, "Cluster \t Current AMI \t Released \t Is Latest AMI?")
for _, c := range list {
isLatest := *latestAMI.ImageId == *c.Image.ImageId
_, _ = fmt.Fprintf(w, "%s \t %s \t %s \t %t\n", *c.Cluster.ClusterName, *c.Image.Name, *c.Image.CreationDate, isLatest)
}
_ = w.Flush()
fmt.Println("")
}