Skip to content

[clean] Add clean command #2347

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

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 38 additions & 0 deletions internal/boxcli/clean.go
Original file line number Diff line number Diff line change
@@ -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
}
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 @@ -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())
Expand Down
18 changes: 18 additions & 0 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
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