-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snapshot): Added the snapshot cmd
- Added the possibility the you can list, create, and delete a snapshot BREAKING CHANGE: No Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
6c6d035
commit 775885d
Showing
8 changed files
with
211 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
package cmd | ||
|
||
// snapshot list -- list all snapshots [ls, all] | ||
// snapshot create NAME INSTANCE_ID [-c '0 * * * *'] -- create a snapshot called NAME from instance INSTANCE_ID [new] | ||
// snapshot remove ID -- remove the snapshot ID [delete, destroy, rm] | ||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var snapshotCmd = &cobra.Command{ | ||
Use: "snapshot", | ||
Short: "Details of Civo Snapshot", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(snapshotCmd) | ||
snapshotCmd.AddCommand(snapshotListCmd) | ||
snapshotCmd.AddCommand(snapshotCreateCmd) | ||
snapshotCmd.AddCommand(snapshotRemoveCmd) | ||
|
||
/* | ||
Flags for the create cmd | ||
*/ | ||
snapshotCreateCmd.Flags().StringVarP(&cron, "cron", "c", "", "If a valid cron string is passed, the snapshot will be saved as an automated snapshot") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var cron string | ||
|
||
var snapshotCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Aliases: []string{"new", "add"}, | ||
Short: "Create a new snapshot", | ||
Args: cobra.MinimumNArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
instance, err := client.FindInstance(args[1]) | ||
if err != nil { | ||
fmt.Printf("Unable to find the instance: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
snapshotConfig := &civogo.SnapshotConfig{ | ||
InstanceID: instance.ID, | ||
Safe: false, | ||
} | ||
|
||
if cron != "" { | ||
snapshotConfig.Cron = cron | ||
} | ||
|
||
snapshot, err := client.CreateSnapshot(args[0], snapshotConfig) | ||
if err != nil { | ||
fmt.Printf("Unable to create the snapshot: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": snapshot.ID, "Name": snapshot.Name}) | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteSingleObjectJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
fmt.Printf("Created a snapshot called %s with ID %s\n", aurora.Green(snapshot.Name), aurora.Green(snapshot.ID)) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/gorhill/cronexpr" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var snapshotListCmd = &cobra.Command{ | ||
Use: "ls", | ||
Aliases: []string{"list", "all"}, | ||
Short: "List snapshot", | ||
Long: `List all available snapshot. | ||
If you wish to use a custom format, the available fields are: | ||
* ID | ||
* InstanceID | ||
* Hostname | ||
* Template | ||
* Region | ||
* Name | ||
* Safe | ||
* SizeGigabytes | ||
* State | ||
* Cron | ||
* RequestedAt | ||
* CompletedAt | ||
Example: civo snapshot ls -o custom -f "ID: Name (Hostname)"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
return | ||
} | ||
|
||
snapshots, err := client.ListSnapshots() | ||
if err != nil { | ||
fmt.Printf("Unable to list snapshots: %s\n", aurora.Red(err)) | ||
return | ||
} | ||
|
||
ow := utility.NewOutputWriter() | ||
|
||
for _, snapshot := range snapshots { | ||
ow.StartLine() | ||
ow.AppendData("ID", snapshot.ID) | ||
ow.AppendData("Name", snapshot.Name) | ||
ow.AppendDataWithLabel("SizeGigabytes", fmt.Sprintf("%s GB", strconv.Itoa(snapshot.SizeGigabytes)), "Size") | ||
ow.AppendData("Hostname", snapshot.Hostname) | ||
ow.AppendData("State", snapshot.State) | ||
ow.AppendData("Cron", snapshot.Cron) | ||
if snapshot.Cron != "" { | ||
ow.AppendData("Schedule", cronexpr.MustParse(snapshot.Cron).Next(time.Now()).Format(time.RFC1123)) | ||
} else { | ||
ow.AppendData("Schedule", "One-off") | ||
} | ||
ow.AppendData("RequestedAt", snapshot.RequestedAt.Format(time.RFC1123)) | ||
ow.AppendData("CompletedAt", snapshot.CompletedAt.Format(time.RFC1123)) | ||
|
||
if outputFormat == "json" || outputFormat == "custom" { | ||
ow.AppendData("InstanceID", snapshot.InstanceID) | ||
ow.AppendData("Template", snapshot.Template) | ||
ow.AppendData("Region", snapshot.Region) | ||
ow.AppendData("Safe", strconv.Itoa(snapshot.Safe)) | ||
} | ||
|
||
} | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteMultipleObjectsJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
ow.WriteTable() | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/cli/config" | ||
"github.com/civo/cli/utility" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var snapshotRemoveCmd = &cobra.Command{ | ||
Use: "remove", | ||
Aliases: []string{"rm", "delete", "destroy"}, | ||
Short: "Remove a snapshot", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client, err := config.CivoAPIClient() | ||
if err != nil { | ||
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
snapshot, err := client.FindSnapshot(args[0]) | ||
if err != nil { | ||
fmt.Printf("Unable to find snapshot for your search: %s\n", aurora.Red(err)) | ||
os.Exit(1) | ||
} | ||
|
||
_, err = client.DeleteSnapshot(snapshot.Name) | ||
|
||
ow := utility.NewOutputWriterWithMap(map[string]string{"ID": snapshot.ID, "Name": snapshot.Name}) | ||
|
||
switch outputFormat { | ||
case "json": | ||
ow.WriteSingleObjectJSON() | ||
case "custom": | ||
ow.WriteCustomOutput(outputFields) | ||
default: | ||
fmt.Printf("The snapshot called %s with ID %s was delete\n", aurora.Green(snapshot.Name), aurora.Green(snapshot.ID)) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters