Skip to content

Commit

Permalink
feat: add 'purge' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Madh93 committed Apr 17, 2023
1 parent e8b2306 commit 0a1d921
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
install Install a provider
purge Purge ALL installed providers
uninstall Uninstall a provider

Flags:
Expand Down
48 changes: 48 additions & 0 deletions cmd/purge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"
"log"
"strings"

"github.com/Madh93/tpm/internal/tpm"
"github.com/spf13/cobra"
)

var purgeCmd = &cobra.Command{
Use: "purge",
Aliases: []string{"p"},
Short: "Purge ALL installed providers",
Run: func(cmd *cobra.Command, args []string) {
skipConfirmation := getBoolFlag(cmd, "yes")

// Request user confirmation
if !skipConfirmation {
// Read user input
log.Print("Are you sure you want to purge ALL installed providers? (yes/no)")
var confirmation string
_, err := fmt.Scanln(&confirmation)
if err != nil {
log.Fatal(err)
}
// Parse user input
if strings.ToLower(confirmation) != "yes" && strings.ToLower(confirmation) != "y" {
log.Println("Operation cancelled.")
return
}
}

// Purge ALL providers
err := tpm.Purge()
if err != nil {
log.Fatal("Error: ", err)
}
},
}

func init() {
rootCmd.AddCommand(purgeCmd)

// Local Flags
purgeCmd.Flags().BoolP("yes", "y", false, "skips the confirmation prompt and proceeds with the action")
}
38 changes: 38 additions & 0 deletions internal/tpm/purge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tpm

import (
"log"
"os"
"path/filepath"

"github.com/spf13/viper"
)

func Purge() (err error) {
log.Println("Removing all providers...")

registryPath := filepath.Join(
viper.GetString("terraform_plugin_cache_dir"),
viper.GetString("terraform_registry"),
)

if viper.GetBool("debug") {
log.Printf("Providers should be located in '%s' directory\n", registryPath)
}

// Check provider already exists
if _, err = os.Stat(registryPath); os.IsNotExist(err) {
log.Printf("Registry path under '%s' does not exist! Ignoring...\n", registryPath)
return nil
}

// Remove provider
err = os.RemoveAll(registryPath)
if err != nil {
return
}

log.Println("All providers were removed sucessfully!")

return nil
}

0 comments on commit 0a1d921

Please sign in to comment.