Skip to content

Commit

Permalink
unpack tarball and remove by name (#814)
Browse files Browse the repository at this point in the history
## Description

Adds functionality to provide a tarball path in `zarf package remove
<name | path>`. Unpacks given tarball temporarily to grab `zarf.yaml >
.Metadata.Name` as the name to use in `packager.Remove(<name>)`.

## Related Issue

Fixes #742 

## Type of change

- [x] New feature (non-breaking change which adds functionality)
  • Loading branch information
Noxsios authored Oct 1, 2022
1 parent 09d41ca commit abb391b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Use to remove a Zarf package that has been deployed already

```
zarf package remove {PACKAGE_NAME} [flags]
zarf package remove {PACKAGE_NAME|PACKAGE_FILE} [flags]
```

### Options
Expand Down
35 changes: 32 additions & 3 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/defenseunicorns/zarf/src/internal/k8s"
"github.com/defenseunicorns/zarf/src/internal/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager"
"github.com/defenseunicorns/zarf/src/internal/utils"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -114,13 +118,38 @@ var packageListCmd = &cobra.Command{
}

var packageRemoveCmd = &cobra.Command{
Use: "remove {PACKAGE_NAME}",
Use: "remove {PACKAGE_NAME|PACKAGE_FILE}",
Aliases: []string{"u"},
Args: cobra.ExactArgs(1),
Short: "Use to remove a Zarf package that has been deployed already",
Run: func(cmd *cobra.Command, args []string) {
err := packager.Remove(args[0])
if err != nil {
pkgName := args[0]
isTarball := regexp.MustCompile(`.*zarf-package-.*\.tar\.zst$`).MatchString
if isTarball(pkgName) {
if utils.InvalidPath(pkgName) {
message.Fatalf(nil, "Invalid tarball path provided")
}

tempPath, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
message.Fatalf(err, "Unable to create tmpdir: %s", config.CommonOptions.TempDirectory)
}
defer os.RemoveAll(tempPath)

if err := archiver.Unarchive(pkgName, tempPath); err != nil {
message.Fatalf(err, "Unable to extract the package contents")
}
configPath := filepath.Join(tempPath, "zarf.yaml")

var pkgConfig types.ZarfPackage

if err := utils.ReadYaml(configPath, &pkgConfig); err != nil {
message.Fatalf(err, "Unable to read zarf.yaml")
}

pkgName = pkgConfig.Metadata.Name
}
if err := packager.Remove(pkgName); err != nil {
message.Fatalf(err, "Unable to remove the package with an error of: %#v", err)
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/24_package_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func TestPackageVariables(t *testing.T) {
// zebra should remain unset as it is not a component variable
assert.Contains(t, string(kubectlOut), "zebra=###ZARF_VAR_ZEBRA###")

stdOut, stdErr, err = e2e.execZarfCommand("package", "remove", "package-variables", "--confirm")
stdOut, stdErr, err = e2e.execZarfCommand("package", "remove", path, "--confirm")
require.NoError(t, err, stdOut, stdErr)
}

0 comments on commit abb391b

Please sign in to comment.