Skip to content

Commit

Permalink
feat(snapshot): Added the snapshot cmd
Browse files Browse the repository at this point in the history
- 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
alejandrojnm committed May 6, 2020
1 parent 6c6d035 commit 775885d
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ Golang has no shortage of external libraries for various parts of this, but the
-~~Networks~~
- Snapshots
- Volumes
- Templates

- Kubernetes Clusters
- Kubernetes Applications



- Templates

- Blueprints
- `curl | bash` installation mechanism
- Homebrew
5 changes: 0 additions & 5 deletions cmd/network.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package cmd

// network list -- list all networks [ls, all]
// network create LABEL -- create a new private network called LABEL [new]
// network update OLD_NAME NEW_NAME -- create a new private network called LABEL [new]
// network remove ID -- remove the network ID [delete, destroy, rm]

import (
"github.com/spf13/cobra"
)
Expand Down
23 changes: 20 additions & 3 deletions cmd/snapshot.go
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")
}
59 changes: 59 additions & 0 deletions cmd/snapshot_create.go
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))
}
},
}
84 changes: 84 additions & 0 deletions cmd/snapshot_list.go
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()
}
},
}
43 changes: 43 additions & 0 deletions cmd/snapshot_remove.go
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))
}
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ajg/form v1.5.1 // indirect
github.com/briandowns/spinner v1.9.0
github.com/civo/civogo v0.2.6
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d // indirect
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down

0 comments on commit 775885d

Please sign in to comment.