-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 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
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,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") | ||
} |
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,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 | ||
} |