This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
clean
cmd to remove dangling virtualenvs
resolves: #16
- Loading branch information
1 parent
9488b9a
commit 3ca609a
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/dhruvmanila/pie/internal/pathutil" | ||
"github.com/dhruvmanila/pie/internal/venv" | ||
"github.com/dhruvmanila/pie/internal/xdg" | ||
) | ||
|
||
var cleanCmd = &cobra.Command{ | ||
Use: "clean", | ||
Short: "Remove any dangling virtual environments", | ||
Long: `Remove any dangling virtual environments. | ||
A dangling virtual environment is one that is not associated with a project. | ||
`, | ||
Args: cobra.NoArgs, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
venvNames, err := venv.Names() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
count := 0 | ||
for _, venvName := range venvNames { | ||
projectPath, err := venv.ProjectPath(venvName) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if !pathutil.IsDir(projectPath) { | ||
venvDir := filepath.Join(xdg.DataDir, venvName) | ||
fmt.Printf("Removing virtualenv (%s)...\n", green.Sprint(venvDir)) | ||
if err = os.RemoveAll(venvDir); err != nil { | ||
log.Fatal(err) | ||
} | ||
count++ | ||
} | ||
} | ||
|
||
if count == 0 { | ||
green.Println("✔ No dangling virtual environments found") | ||
} else { | ||
green.Printf("✔ Removed %d dangling virtual environments\n", count) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(cleanCmd) | ||
} |