func actionList(ctx *cli.Context) { if ctx.BoolT("help") == true { cli.ShowAppHelp(ctx) os.Exit(1) } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) opt := &godo.ListOptions{ Page: ctx.Int("page"), PerPage: ctx.Int("page-size"), } actionList, _, err := client.Actions.List(opt) if err != nil { fmt.Printf("Unable to list Actions: %s\n", err) os.Exit(1) } cliOut := NewCLIOutput() defer cliOut.Flush() cliOut.Header("ID", "Region", "ResourceType", "ResourceID", "Type", "StartedAt", "CompletedAt", "Status") for _, action := range actionList { cliOut.Writeln("%d\t%s\t%s\t%d\t%s\t%s\t%s\t%s\n", action.ID, action.RegionSlug, action.ResourceType, action.ResourceID, action.Type, action.StartedAt, action.CompletedAt, action.Status) } }
func dropletActionUpgrade(ctx *cli.Context) { if ctx.Int("id") == 0 && len(ctx.Args()) != 1 { log.Fatal("Error: Must provide ID or name for Droplet to resize.") } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) id := ctx.Int("id") if id == 0 { droplet, err := FindDropletByName(client, ctx.Args()[0]) if err != nil { log.Fatal(err) } else { id = droplet.ID } } droplet, _, err := client.Droplets.Get(id) if err != nil { log.Fatal("Unable to find Droplet: %s.", err) } action, _, err := client.DropletActions.Upgrade(droplet.ID) if err != nil { log.Fatal(err) } WriteOutput(action) }
func dropletDestroy(ctx *cli.Context) { if ctx.Int("id") == 0 && len(ctx.Args()) != 1 { log.Fatal("Error: Must provide ID or name for Droplet to destroy.") } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) id := ctx.Int("id") if id == 0 { droplet, err := FindDropletByName(client, ctx.Args()[0]) if err != nil { log.Fatal(err) } else { id = droplet.ID } } droplet, _, err := client.Droplets.Get(id) if err != nil { log.Fatalf("Unable to find Droplet: %s.", err) } _, err = client.Droplets.Delete(id) if err != nil { log.Fatalf("Unable to destroy Droplet: %s.", err) } log.Fatalf("Droplet %s destroyed.", droplet.Name) }
func domainList(ctx *cli.Context) { if ctx.BoolT("help") == true { cli.ShowAppHelp(ctx) os.Exit(1) } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) opt := &godo.ListOptions{ Page: ctx.Int("page"), PerPage: ctx.Int("page-size"), } domainList, _, err := client.Domains.List(opt) if err != nil { log.Fatalf("Unable to list Domains: %s.", err) } cliOut := NewCLIOutput() defer cliOut.Flush() cliOut.Header("Name", "TTL") for _, domain := range domainList { cliOut.Writeln("%s\t%d\n", domain.Name, domain.TTL) } }
func domainRecordList(ctx *cli.Context) { if len(ctx.Args()) != 1 { fmt.Printf("Error: Must provide a domain name for which to list records.\n") os.Exit(64) } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) domainName := ctx.Args().First() opt := &godo.ListOptions{ Page: ctx.Int("page"), PerPage: ctx.Int("page-size"), } domainDecords, _, err := client.Domains.Records(domainName, opt) if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } WriteOutput(domainDecords) }
func sshDestroy(ctx *cli.Context) { if ctx.Int("id") == 0 && ctx.String("fingerprint") == "" && len(ctx.Args()) < 1 { fmt.Printf("Error: Must provide ID, fingerprint or name for SSH Key to destroy.\n") os.Exit(1) } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) id := ctx.Int("id") fingerprint := ctx.String("fingerprint") var key godo.Key if id == 0 && fingerprint == "" { key, err := FindKeyByName(client, ctx.Args().First()) if err != nil { fmt.Printf("%s\n", err) os.Exit(64) } else { id = key.ID } } else if id != 0 { key, _, err := client.Keys.GetByID(id) if err != nil { fmt.Printf("Unable to find SSH Key: %s\n", err) os.Exit(1) } else { id = key.ID } } else { key, _, err := client.Keys.GetByFingerprint(fingerprint) if err != nil { fmt.Printf("Unable to find SSH Key: %s\n", err) os.Exit(1) } else { id = key.ID } } _, err := client.Keys.DeleteByID(id) if err != nil { fmt.Printf("Unable to destroy SSH Key: %s\n", err) os.Exit(1) } fmt.Printf("Key %s destroyed.\n", key.Name) }
func dropletActionResize(ctx *cli.Context) { if ctx.Int("id") == 0 && len(ctx.Args()) != 1 { fmt.Printf("Error: Must provide ID or name for Droplet to destroy.\n") os.Exit(1) } size := ctx.String("size") disk := ctx.Bool("disk") tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) id := ctx.Int("id") if id == 0 { droplet, err := FindDropletByName(client, ctx.Args()[0]) if err != nil { fmt.Printf("%s\n", err) os.Exit(64) } else { id = droplet.ID } } droplet, _, err := client.Droplets.Get(id) if err != nil { fmt.Printf("Unable to find Droplet: %s\n", err) os.Exit(1) } action, _, err := client.DropletActions.Resize(droplet.ID, size, disk) if err != nil { fmt.Println(err) os.Exit(1) } WriteOutput(action) }
func domainRecordCreate(ctx *cli.Context) { if len(ctx.Args()) != 1 { cli.ShowAppHelp(ctx) fmt.Printf("Must specify a domain name to add a record to.\n") os.Exit(1) } domainName := ctx.Args().First() tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) createRequest := &godo.DomainRecordEditRequest{ Type: strings.ToUpper(ctx.String("type")), Name: ctx.String("name"), Data: ctx.String("data"), } if createRequest.Type == "MX" || createRequest.Type == "SRV" { createRequest.Priority = ctx.Int("priority") } if createRequest.Type == "SRV" { createRequest.Port = ctx.Int("port") createRequest.Weight = ctx.Int("weight") } domainRecord, _, err := client.Domains.CreateRecord(domainName, createRequest) if err != nil { fmt.Printf("%s\n", err) os.Exit(1) } WriteOutput(domainRecord) }
func dropletActionKernels(ctx *cli.Context) { if ctx.Int("id") == 0 && len(ctx.Args()) != 1 { log.Fatal("Error: Must provide ID or name for Droplet to list available kernels.") } tokenSource := &TokenSource{ AccessToken: APIKey, } oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource) client := godo.NewClient(oauthClient) id := ctx.Int("id") if id == 0 { droplet, err := FindDropletByName(client, ctx.Args()[0]) if err != nil { log.Fatal(err) } else { id = droplet.ID } } droplet, _, err := client.Droplets.Get(id) if err != nil { log.Fatal("Unable to find Droplet: %s.", err) } opt := &godo.ListOptions{} kernelList := []godo.Kernel{} for { // TODO make all optional kernelsPage, resp, err := client.Droplets.Kernels(droplet.ID, opt) if err != nil { log.Fatalf("Unable to list Droplet kernels: %s.", err) } // append the current page's droplets to our list for _, k := range kernelsPage { kernelList = append(kernelList, k) } // if we are at the last page, break out the for loop if resp.Links == nil || resp.Links.IsLastPage() { break } page, err := resp.Links.CurrentPage() if err != nil { log.Fatalf("Unable to get pagination: %s.", err) } // set the page we want for the next request opt.Page = page + 1 } cliOut := NewCLIOutput() defer cliOut.Flush() cliOut.Header("ID", "Name", "Version") for _, kernel := range kernelList { cliOut.Writeln("%d\t%s\t%s\n", kernel.ID, kernel.Name, kernel.Version) } }