Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boxcli: add clean command #2341

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions internal/boxcli/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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/devconfig"
"go.jetpack.io/devbox/internal/devconfig/configfile"
"go.jetpack.io/devbox/internal/lock"
"go.jetpack.io/devbox/internal/shellgen"
)

type cleanFlags struct {
pathFlag
hard bool
}

func cleanCmd() *cobra.Command {
flags := &cleanFlags{}
command := &cobra.Command{
Use: "clean",
Short: "Clean up devbox files from the current directory",
Long: "Cleans up an existing devbox directory. " +
"This will delete .devbox and devbox.lock. ",
RunE: func(cmd *cobra.Command, args []string) error {
if flags.hard {
prompt := &survey.Confirm{
Message: "Are you sure you want to delete your devbox config?",
}
confirmed := false
if err := survey.AskOne(prompt, &confirmed); err != nil {
return err
}
if !confirmed {
return nil
}
}
return runCleanCmd(cmd, args, flags)
},
}

command.Flags().BoolVar(&flags.hard, "hard", false, "Also delete the devbox.json file")

flags.register(command)

return command
}

func runCleanCmd(cmd *cobra.Command, args []string, flags *cleanFlags) error {
path := pathArg(args)

filesToDelete := []string{
lock.FileName,
shellgen.DevboxHiddenDirName,
}

if flags.hard {
filesToDelete = append(filesToDelete, configfile.DefaultName)
}

return devconfig.Clean(path, filesToDelete, cmd.ErrOrStderr())
}
2 changes: 1 addition & 1 deletion internal/boxcli/multi/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func RootCmd() *cobra.Command {
command.AddCommand(globalCmd())
command.AddCommand(infoCmd())
command.AddCommand(initCmd())
command.AddCommand(cleanCmd())
command.AddCommand(installCmd())
command.AddCommand(integrateCmd())
command.AddCommand(listCmd())
Expand Down
27 changes: 27 additions & 0 deletions internal/devconfig/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

package devconfig

import (
"io"
"os"

"go.jetpack.io/devbox/internal/fileutil"
"go.jetpack.io/devbox/internal/ux"
)

func Clean(dir string, filesToDelete []string, w io.Writer) error {
for _, f := range filesToDelete {
if fileutil.Exists(f) {
ux.Finfof(w, "Deleting %s\n", f)
}
if err := os.RemoveAll(dir + f); err != nil {
return err
}
}

// TODO: should the devbox shell be killed here?

return nil
}
4 changes: 3 additions & 1 deletion internal/lock/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"go.jetpack.io/devbox/internal/cuecfg"
)

const FileName = "devbox.lock"

const lockFileVersion = "1"

// Lightly inspired by package-lock.json
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion internal/shellgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"go.jetpack.io/devbox/internal/redact"
)

const DevboxHiddenDirName = ".devbox"

//go:embed tmpl/*
var tmplFS embed.FS

Expand All @@ -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)
}
Expand Down
17 changes: 17 additions & 0 deletions testscripts/clean/clean.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Create a new devbox, install a package and run clean

! exists devbox.json
exec devbox init
exists devbox.json

exec devbox add go

exec devbox shell
exists devbox.json
exists devbox.lock
exists .devbox

exec devbox clean
! exists devbox.lock
! exists .devbox

18 changes: 18 additions & 0 deletions testscripts/clean/clean_hard.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Create a new devbox, install a package and run clean hard

! exists devbox.json
exec devbox init
exists devbox.json

exec devbox add go

exec devbox shell
exists devbox.json
exists devbox.lock
exists .devbox

exec devbox clean --hard
! exists devbox.json
! exists devbox.lock
! exists .devbox