diff --git a/internal/boxcli/clean.go b/internal/boxcli/clean.go new file mode 100644 index 00000000000..1fe530d09dd --- /dev/null +++ b/internal/boxcli/clean.go @@ -0,0 +1,38 @@ +// Copyright 2024 Jetify Inc. and contributors. All rights reserved. +// Use of this source code is governed by the license in the LICENSE file. + +package boxcli + +import ( + "github.com/AlecAivazis/survey/v2" + "github.com/spf13/cobra" + + "go.jetpack.io/devbox/internal/devbox" +) + +type cleanFlags struct { + pathFlag +} + +func cleanCmd() *cobra.Command { + flags := cleanFlags{} + cmd := &cobra.Command{ + Use: "clean", + Short: "Clean up devbox files from the current directory", + RunE: func(cmd *cobra.Command, args []string) error { + prompt := &survey.Confirm{ + Message: "Are you sure you want to clean up devbox files?", + } + confirmed := false + if err := survey.AskOne(prompt, &confirmed); err != nil { + return err + } + if !confirmed { + return nil + } + return devbox.Clean(flags.path, cmd.ErrOrStderr()) + }, + } + flags.register(cmd) + return cmd +} diff --git a/internal/boxcli/multi/sync.go b/internal/boxcli/multi/sync.go index ac43ea8d2b7..f5f68ff3440 100644 --- a/internal/boxcli/multi/sync.go +++ b/internal/boxcli/multi/sync.go @@ -104,7 +104,7 @@ func collectLockfiles() ([]string, error) { return err } - if !dirEntry.IsDir() && filepath.Base(path) == "devbox.lock" { + if !dirEntry.IsDir() && filepath.Base(path) == lock.FileName { lockfiles = append(lockfiles, path) } diff --git a/internal/boxcli/root.go b/internal/boxcli/root.go index e30ba6a363b..fb18f4c6ab4 100644 --- a/internal/boxcli/root.go +++ b/internal/boxcli/root.go @@ -60,6 +60,7 @@ func RootCmd() *cobra.Command { command.AddCommand(authCmd()) } command.AddCommand(cacheCmd()) + command.AddCommand(cleanCmd()) command.AddCommand(createCmd()) command.AddCommand(secretsCmd()) command.AddCommand(generateCmd()) diff --git a/internal/devbox/devbox.go b/internal/devbox/devbox.go index 557f6de7ebe..30649ab4244 100644 --- a/internal/devbox/devbox.go +++ b/internal/devbox/devbox.go @@ -32,6 +32,7 @@ import ( "go.jetpack.io/devbox/internal/devbox/envpath" "go.jetpack.io/devbox/internal/devbox/generate" "go.jetpack.io/devbox/internal/devconfig" + "go.jetpack.io/devbox/internal/devconfig/configfile" "go.jetpack.io/devbox/internal/devpkg" "go.jetpack.io/devbox/internal/devpkg/pkgtype" "go.jetpack.io/devbox/internal/envir" @@ -1167,3 +1168,20 @@ func validateEnvironment(environment string) (string, error) { environment, ) } + +func Clean(path string, w io.Writer) error { + toDelete := []string{ + filepath.Join(path, lock.FileName), + filepath.Join(path, shellgen.DevboxHiddenDirName), + filepath.Join(path, configfile.DefaultName), + } + for _, path := range toDelete { + if fileutil.Exists(path) { + ux.Finfof(w, "Deleting %s\n", path) + } + if err := os.RemoveAll(path); err != nil { + return err + } + } + return nil +} diff --git a/internal/lock/lockfile.go b/internal/lock/lockfile.go index e4a47effa31..b7f90843a55 100644 --- a/internal/lock/lockfile.go +++ b/internal/lock/lockfile.go @@ -21,6 +21,8 @@ import ( "go.jetpack.io/devbox/internal/cuecfg" ) +const FileName = "devbox.lock" + const lockFileVersion = "1" // Lightly inspired by package-lock.json @@ -232,7 +234,7 @@ func (f *File) isDirty() (bool, error) { } func lockFilePath(projectDir string) string { - return filepath.Join(projectDir, "devbox.lock") + return filepath.Join(projectDir, FileName) } func ResolveRunXPackage(ctx context.Context, pkg string) (types.PkgRef, error) { diff --git a/internal/shellgen/generate.go b/internal/shellgen/generate.go index 57d8e37acc9..74df8988285 100644 --- a/internal/shellgen/generate.go +++ b/internal/shellgen/generate.go @@ -20,6 +20,8 @@ import ( "go.jetpack.io/devbox/internal/redact" ) +const DevboxHiddenDirName = ".devbox" + //go:embed tmpl/* var tmplFS embed.FS @@ -43,7 +45,7 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error { } // Gitignore file is added to the .devbox directory - err = writeFromTemplate(filepath.Join(devbox.ProjectDir(), ".devbox"), plan, ".gitignore", ".gitignore") + err = writeFromTemplate(filepath.Join(devbox.ProjectDir(), DevboxHiddenDirName), plan, ".gitignore", ".gitignore") if err != nil { return errors.WithStack(err) }