Exemple #1
0
func main() {
	var app *cli.App
	app = cli.NewApp()
	app.Name = "awsutils"
	app.Usage = "automation of Amazon Web Services configurations through their API"
	app.EnableBashCompletion = true
	app.Commands = []cli.Command{
		{
			Name:  "iam",
			Usage: "use the AWS iam API",
			Subcommands: []cli.Command{
				{
					Name:  "certs",
					Usage: "summarise certificate configurations available in your AWS account",
					Action: func(c *cli.Context) {
						out, err := exec.Command("aws", "iam", "list-server-certificates").Output()
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						var r ListServerCertificatesResponse
						err = json.Unmarshal(out, &r)
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
						fmt.Fprint(w, "ID\tName\tStatus\n")
						for _, cm := range r.ServerCertificateMetadataList {
							fmt.Fprintf(w, "%s\t%s\t%s\n", cm.ServerCertificateID, cm.ServerCertificateName, ResolveStatus(cm.Expiration))
						}
						w.Flush()
						// fmt.Printf("%+v\n", r)
					},
				},
			},
		},
		{
			Name:    "cloudfront",
			Aliases: []string{"cf"},
			Usage:   "use the AWS cloudfront API",
			Subcommands: []cli.Command{
				{
					Name:  "export-configs",
					Usage: "export the cloudfront distribution configurations",
					Action: func(c *cli.Context) {
						out, err := exec.Command("aws", "cloudfront", "list-distributions").Output()
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						var r CloudFrontListDistributionsResponse
						err = json.Unmarshal(out, &r)
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
						fmt.Fprint(w, "DomainName\n")
						for _, d := range r.Distributionlist.Items {
							fmt.Fprintf(w, "%+v\n", d.Origins.Items[0]["DomainName"])
							json, _ := json.MarshalIndent(d, "", "  ")

							fn := fmt.Sprintf("%s.json", d.Origins.Items[0]["Id"])
							f, err := os.Create(fn)
							if err != nil {
								cwd, _ := os.Getwd()
								fmt.Printf("Unable to create file '%s' in '%s'", fn, cwd)
								continue
							}
							f.Write(json)
							f.Close()
						}
						w.Flush()
					},
				},
				{
					Name:  "dists",
					Usage: "summarize the cloudfront distribution configurations",
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:  "order-by",
							Value: "alias",
							Usage: "sort results on alias|origin|status",
						},
						cli.BoolFlag{
							Name:  "csv",
							Usage: "output as csv",
						},
					},
					Action: func(c *cli.Context) {
						out, err := exec.Command("aws", "cloudfront", "list-distributions").Output()
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						var r CloudFrontListDistributionsResponse
						err = json.Unmarshal(out, &r)
						if err != nil {
							fmt.Println(err.Error())
							return
						}

						var list []DistSummary
						for _, d := range r.Distributionlist.Items {
							id := d.ID
							distDomain := d.DomainName
							status := d.Status
							origin := ""
							alias := ""

							if len(d.Origins.Items) > 0 {
								for _, o := range d.Origins.Items {
									if op, ok := o["OriginPath"].(string); ok && op == "" {
										origin = o["DomainName"].(string)
									}
								}
							}
							if len(d.Aliases.Items) > 0 {
								alias = strings.Join(d.Aliases.Items, ",")
							}

							list = append(list, DistSummary{ID: id, Domain: distDomain, Alias: alias, Origin: origin, Status: status})
						}

						switch c.String("order-by") {
						case "origin":
							sort.Sort(ByOrigin(list))
						case "status":
							sort.Sort(ByStatus(list))
						case "alias":
							sort.Sort(ByAlias(list))
						default:
							fmt.Println("Unrecognised value for", c.String("order-by"), ". Sorting by alias instead.")
							sort.Sort(ByAlias(list))
						}

						if c.Bool("csv") {
							fmt.Println("ID;Domain;Alias;Origin;Status")
							for _, s := range list {
								fmt.Printf(`"%s";"%s";"%s";"%s";"%s"`, s.ID, s.Domain, s.Alias, s.Origin, s.Status)
								fmt.Println()
							}
						} else {
							w := tabwriter.NewWriter(os.Stdout, 0, 4, 3, ' ', 0)
							fmt.Fprint(w, "ID\tDomain\tAlias\tOrigin\tStatus\n")
							for _, s := range list {
								fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", s.ID, s.Domain, s.Alias, s.Origin, s.Status)
							}
							w.Flush()
						}
					},
				},
			},
		},
	}

	app.Run(os.Args)
}