diff --git a/docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_package_remove.md b/docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_package_remove.md index 91e66a8d42..eb4a7e48ab 100644 --- a/docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_package_remove.md +++ b/docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_package_remove.md @@ -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 diff --git a/src/cmd/package.go b/src/cmd/package.go index af808d8ba1..5f742e5646 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -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" ) @@ -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) } }, diff --git a/src/test/e2e/24_package_variables_test.go b/src/test/e2e/24_package_variables_test.go index 26dbad7465..78b7d8721d 100644 --- a/src/test/e2e/24_package_variables_test.go +++ b/src/test/e2e/24_package_variables_test.go @@ -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) }