Skip to content

Commit

Permalink
Merge pull request #87 from stgraber/main
Browse files Browse the repository at this point in the history
Fix snapshot delete syntax
  • Loading branch information
freeekanayaka committed Sep 20, 2023
2 parents 415bcaa + fccbda5 commit 4ee8580
Show file tree
Hide file tree
Showing 47 changed files with 3,327 additions and 3,121 deletions.
36 changes: 13 additions & 23 deletions cmd/incus/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ type cmdSnapshotDelete struct {

func (c *cmdSnapshotDelete) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("delete", i18n.G("[<remote>:]<instance>/<snapshot name> [[<remote>:]<instance>/<snapshot name>...]"))
cmd.Use = usage("delete", i18n.G("[<remote>:]<instance> <snapshot name>"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Delete instance snapshots")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
Expand All @@ -178,7 +178,7 @@ func (c *cmdSnapshotDelete) Command() *cobra.Command {

func (c *cmdSnapshotDelete) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, -1)
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
Expand All @@ -189,33 +189,25 @@ func (c *cmdSnapshotDelete) Run(cmd *cobra.Command, args []string) error {
return err
}

// Check that everything exists.
err = instancesExist(resources)
if err != nil {
return err
}

// Process with deletion.
for _, resource := range resources {
if c.flagInteractive {
err := c.promptDelete(resource.name)
if err != nil {
return err
}
}

err := c.doDelete(resource.server, resource.name)
if c.flagInteractive {
err := c.promptDelete(resources[0].name, args[1])
if err != nil {
return err
}
}

err = c.doDelete(resources[0].server, resources[0].name, args[1])
if err != nil {
return err
}

return nil
}

func (c *cmdSnapshotDelete) promptDelete(name string) error {
func (c *cmdSnapshotDelete) promptDelete(instName string, name string) error {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(i18n.G("Remove %s (yes/no): "), name)
fmt.Printf(i18n.G("Remove snapshot %s from %s (yes/no): "), name, instName)
input, _ := reader.ReadString('\n')
input = strings.TrimSuffix(input, "\n")

Expand All @@ -226,14 +218,12 @@ func (c *cmdSnapshotDelete) promptDelete(name string) error {
return nil
}

func (c *cmdSnapshotDelete) doDelete(d incus.InstanceServer, name string) error {
func (c *cmdSnapshotDelete) doDelete(d incus.InstanceServer, instName string, name string) error {
var op incus.Operation
var err error

// Snapshot delete
fields := strings.SplitN(name, shared.SnapshotDelimiter, 2)

op, err = d.DeleteInstanceSnapshot(fields[0], fields[1])
op, err = d.DeleteInstanceSnapshot(instName, name)
if err != nil {
return err
}
Expand Down
16 changes: 5 additions & 11 deletions cmd/incus/storage_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,7 @@ type cmdStorageVolumeSnapshotDelete struct {

func (c *cmdStorageVolumeSnapshotDelete) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("delete", i18n.G("[<remote>:]<pool> <volume>/<snapshot>"))
cmd.Use = usage("delete", i18n.G("[<remote>:]<pool> <volume> <snapshot>"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Delete storage volume snapshots")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
Expand All @@ -2116,7 +2116,7 @@ func (c *cmdStorageVolumeSnapshotDelete) Command() *cobra.Command {

func (c *cmdStorageVolumeSnapshotDelete) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
exit, err := c.global.CheckArgs(cmd, args, 3, 3)
if exit {
return err
}
Expand All @@ -2137,19 +2137,13 @@ func (c *cmdStorageVolumeSnapshotDelete) Run(cmd *cobra.Command, args []string)
// Parse the input
volName, volType := c.storageVolume.parseVolume("custom", args[1])

// If a target was specified, create the volume on the given member.
// If a target was specified, delete the volume on the given member.
if c.storage.flagTarget != "" {
client = client.UseTarget(c.storage.flagTarget)
}

if !strings.Contains(volName, shared.SnapshotDelimiter) {
return fmt.Errorf("Volume is not a snapshot")
}

fields := strings.SplitN(volName, "/", 2)

// Delete the snapshot
op, err := client.DeleteStoragePoolVolumeSnapshot(resource.name, volType, fields[0], fields[1])
op, err := client.DeleteStoragePoolVolumeSnapshot(resource.name, volType, volName, args[2])
if err != nil {
return err
}
Expand All @@ -2160,7 +2154,7 @@ func (c *cmdStorageVolumeSnapshotDelete) Run(cmd *cobra.Command, args []string)
}

if !c.global.flagQuiet {
fmt.Printf(i18n.G("Storage volume %s deleted")+"\n", args[1])
fmt.Printf(i18n.G("Storage volume snapshot %s deleted from %s")+"\n", args[2], args[1])
}

return nil
Expand Down

0 comments on commit 4ee8580

Please sign in to comment.