Skip to content

Commit 1b9989a

Browse files
committed
[clean] Add clean command
1 parent 9cb6297 commit 1b9989a

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

internal/boxcli/clean.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package boxcli
5+
6+
import (
7+
"github.com/AlecAivazis/survey/v2"
8+
"github.com/spf13/cobra"
9+
10+
"go.jetpack.io/devbox/internal/devbox"
11+
)
12+
13+
type cleanFlags struct {
14+
pathFlag
15+
}
16+
17+
func cleanCmd() *cobra.Command {
18+
flags := cleanFlags{}
19+
cmd := &cobra.Command{
20+
Use: "clean",
21+
Short: "Clean up devbox files from the current directory",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
prompt := &survey.Confirm{
24+
Message: "Are you sure you want to clean up devbox files?",
25+
}
26+
confirmed := false
27+
if err := survey.AskOne(prompt, &confirmed); err != nil {
28+
return err
29+
}
30+
if !confirmed {
31+
return nil
32+
}
33+
return devbox.Clean(flags.path, cmd.ErrOrStderr())
34+
},
35+
}
36+
flags.register(cmd)
37+
return cmd
38+
}

internal/boxcli/multi/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func collectLockfiles() ([]string, error) {
104104
return err
105105
}
106106

107-
if !dirEntry.IsDir() && filepath.Base(path) == "devbox.lock" {
107+
if !dirEntry.IsDir() && filepath.Base(path) == lock.FileName {
108108
lockfiles = append(lockfiles, path)
109109
}
110110

internal/boxcli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func RootCmd() *cobra.Command {
6060
command.AddCommand(authCmd())
6161
}
6262
command.AddCommand(cacheCmd())
63+
command.AddCommand(cleanCmd())
6364
command.AddCommand(createCmd())
6465
command.AddCommand(secretsCmd())
6566
command.AddCommand(generateCmd())

internal/devbox/devbox.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"go.jetpack.io/devbox/internal/devbox/envpath"
3333
"go.jetpack.io/devbox/internal/devbox/generate"
3434
"go.jetpack.io/devbox/internal/devconfig"
35+
"go.jetpack.io/devbox/internal/devconfig/configfile"
3536
"go.jetpack.io/devbox/internal/devpkg"
3637
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
3738
"go.jetpack.io/devbox/internal/envir"
@@ -1167,3 +1168,20 @@ func validateEnvironment(environment string) (string, error) {
11671168
environment,
11681169
)
11691170
}
1171+
1172+
func Clean(path string, w io.Writer) error {
1173+
toDelete := []string{
1174+
filepath.Join(path, lock.FileName),
1175+
filepath.Join(path, shellgen.DevboxHiddenDirName),
1176+
filepath.Join(path, configfile.DefaultName),
1177+
}
1178+
for _, path := range toDelete {
1179+
if fileutil.Exists(path) {
1180+
ux.Finfof(w, "Deleting %s\n", path)
1181+
}
1182+
if err := os.RemoveAll(path); err != nil {
1183+
return err
1184+
}
1185+
}
1186+
return nil
1187+
}

internal/lock/lockfile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"go.jetpack.io/devbox/internal/cuecfg"
2222
)
2323

24+
const FileName = "devbox.lock"
25+
2426
const lockFileVersion = "1"
2527

2628
// Lightly inspired by package-lock.json
@@ -232,7 +234,7 @@ func (f *File) isDirty() (bool, error) {
232234
}
233235

234236
func lockFilePath(projectDir string) string {
235-
return filepath.Join(projectDir, "devbox.lock")
237+
return filepath.Join(projectDir, FileName)
236238
}
237239

238240
func ResolveRunXPackage(ctx context.Context, pkg string) (types.PkgRef, error) {

internal/shellgen/generate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"go.jetpack.io/devbox/internal/redact"
2121
)
2222

23+
const DevboxHiddenDirName = ".devbox"
24+
2325
//go:embed tmpl/*
2426
var tmplFS embed.FS
2527

@@ -43,7 +45,7 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error {
4345
}
4446

4547
// Gitignore file is added to the .devbox directory
46-
err = writeFromTemplate(filepath.Join(devbox.ProjectDir(), ".devbox"), plan, ".gitignore", ".gitignore")
48+
err = writeFromTemplate(filepath.Join(devbox.ProjectDir(), DevboxHiddenDirName), plan, ".gitignore", ".gitignore")
4749
if err != nil {
4850
return errors.WithStack(err)
4951
}

0 commit comments

Comments
 (0)