From d5b897027919f473cec23ca4007882d3bce6efaf Mon Sep 17 00:00:00 2001 From: Marcus Kok <47163063+Marcusk19@users.noreply.github.com> Date: Thu, 4 Apr 2024 12:27:43 -0400 Subject: [PATCH] add stuff (#20) * add ability to view list of linked configs * hide password in input for sync command * confirm overwrite existing configs when adding --- Makefile | 1 + cmd/add.go | 15 +++++++++++++++ cmd/link.go | 23 ++++++++++++++++++++--- cmd/sync.go | 2 ++ test/link_test.go | 9 +++++---- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 432aa2d..3b052a7 100644 --- a/Makefile +++ b/Makefile @@ -8,3 +8,4 @@ sandbox: unit-test: TESTING=true go test -v ./test + rm -rf test/dotctl_test 2> /dev/null diff --git a/cmd/add.go b/cmd/add.go index 1a62ef4..9829360 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/Marcusk19/dotctl/tools" + "github.com/manifoldco/promptui" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -50,6 +51,20 @@ func runAddCommand(cmd *cobra.Command, args []string) { fmt.Printf("Will copy %s -> %s \n", configSrc, dotfileDest) return } + + _, err = fs.Stat(dotfileDest) + if err == nil { + fmt.Printf("Looks like %s exists in current dotfile directory\n", dotfileDest) + fmt.Println("Do you want to overwrite it?") + confirm := promptui.Prompt{ + Label: "overwrite config", + IsConfirm: true, + } + overwrite, _ := confirm.Run() + if strings.ToUpper(overwrite) != "Y" { + return + } + } err = tools.CopyDir(fs, configSrc, dotfileDest) if err != nil { diff --git a/cmd/link.go b/cmd/link.go index 7873001..69fb479 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -10,6 +10,12 @@ import ( "github.com/spf13/viper" ) +func init() { + RootCmd.AddCommand(linkCommand) + linkCommand.AddCommand(listCommand) +} + + var linkCommand = &cobra.Command { Use: "link", Run: runLinkCommand, @@ -17,9 +23,6 @@ var linkCommand = &cobra.Command { Long: "add longer description", // TODO add longer description here } -func init() { - RootCmd.AddCommand(linkCommand) -} func runLinkCommand(cmd *cobra.Command, args []string) { fs := FileSystem @@ -64,6 +67,20 @@ func runLinkCommand(cmd *cobra.Command, args []string) { } } } +} + +var listCommand = &cobra.Command { + Use: "list", + Run: runListCommand, + Short: "list configs that should be symlinked", + Long: "add longer description", // TODO add longer description here +} +func runListCommand(cmd *cobra.Command, args []string) { + links := viper.GetStringMapString("links") + fmt.Println("Configs added:") + for configName, configPath := range links { + fmt.Printf("%s: %s\n", configName, configPath) + } } diff --git a/cmd/sync.go b/cmd/sync.go index 88ce593..4d8eee0 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -98,6 +98,8 @@ func runSyncCommand(cmd *cobra.Command, args []string) { password := promptui.Prompt{ Label: "password", Validate: validateInput, + HideEntered: true, + Mask: '*', } usernameVal, err := username.Run() diff --git a/test/link_test.go b/test/link_test.go index b5a754c..231d178 100644 --- a/test/link_test.go +++ b/test/link_test.go @@ -14,6 +14,7 @@ import ( func TestLinkCommand(t *testing.T) { + oldDotfilePath := viper.GetString("dotfile-path") setUpTesting() dotctl := cmd.RootCmd actual := new(bytes.Buffer) @@ -32,7 +33,7 @@ func TestLinkCommand(t *testing.T) { assert.Equal(t, expected, actual.String(), "actual differs from expected") - tearDownTesting() + tearDownTesting(oldDotfilePath) } func setUpTesting() { @@ -49,7 +50,7 @@ func setUpTesting() { viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/")) } -func tearDownTesting() { - fs := cmd.FileSystem - fs.RemoveAll("dotctl_test/") +func tearDownTesting(oldDotfilePath string) { + viper.Set("dotfile-path", oldDotfilePath) + viper.WriteConfig() }