From 5473a2cf9ba4b516918fc03c5a182dfa6e3f8b7a Mon Sep 17 00:00:00 2001 From: Marcus Kok Date: Fri, 5 Apr 2024 12:14:39 -0400 Subject: [PATCH] add status command --- cmd/add.go | 14 ++++++++----- cmd/status.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ test/status_test.go | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 cmd/status.go create mode 100644 test/status_test.go diff --git a/cmd/add.go b/cmd/add.go index 3fb6145..a36ef3c 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -26,8 +26,10 @@ var addCommand = &cobra.Command { func runAddCommand(cmd *cobra.Command, args []string) { fs := FileSystem + testing := viper.GetBool("testing") + if len(args) <= 0 { - fmt.Println("ERROR: requires at least one argument") + fmt.Println("ERROR: requires config path") return } @@ -38,9 +40,11 @@ func runAddCommand(cmd *cobra.Command, args []string) { links := viper.GetStringMap("links") links[name] = configSrc viper.Set("links", links) - err := viper.WriteConfig() - if err != nil { - fmt.Printf("Problem updating dotctl config %s", err) + if !testing { + err := viper.WriteConfig() + if err != nil { + fmt.Printf("Problem updating dotctl config %s", err) + } } dotfilePath := viper.Get("dotfile-path").(string) @@ -52,7 +56,7 @@ func runAddCommand(cmd *cobra.Command, args []string) { return } - _, err = fs.Stat(dotfileDest) + _, 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?") diff --git a/cmd/status.go b/cmd/status.go new file mode 100644 index 0000000..0b7cbae --- /dev/null +++ b/cmd/status.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" + "log" + "slices" + + "github.com/spf13/afero" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func init() { + RootCmd.AddCommand(statusCommand) +} + +var statusCommand = &cobra.Command { + Use: "status", + Short: "View status of dotctl", + Long: "TODO: add longer description", + Run: runStatusCommand, +} + +func runStatusCommand(cmd *cobra.Command, args[]string) { + fs := FileSystem + links := viper.GetStringMapString("links") + + var ignoredDirs = []string{".git", "dotctl", ".gitignore"} + + dotfiles, err := afero.ReadDir(fs, viper.GetString("dotfile-path")) + if err != nil { + log.Fatalf("Cannot read dotfile dir: %s\n", err) + } + + fmt.Fprintln(cmd.OutOrStdout(), "Config directories currently in dotfile path:") + for _, dotfileDir := range(dotfiles) { + dirName := dotfileDir.Name() + if !slices.Contains(ignoredDirs, dirName) { + if links[dirName] != "" { + fmt.Fprintf(cmd.OutOrStdout(), "%s - %s\n", dirName, links[dirName]) + } else { + fmt.Fprintln(cmd.OutOrStdout(), dirName) + } + } + } + +} + + diff --git a/test/status_test.go b/test/status_test.go new file mode 100644 index 0000000..e004769 --- /dev/null +++ b/test/status_test.go @@ -0,0 +1,44 @@ +package test + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/Marcusk19/dotctl/cmd" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestStatusCommand(t *testing.T) { + viper.Set("testing", true) + + fs := cmd.FileSystem + + homedir := os.Getenv("HOME") + fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755) + fs.MkdirAll(filepath.Join(homedir, "dotfiles/someconfig"), 0755) + fs.MkdirAll(filepath.Join(homedir, "dotfiles/somelinkedconfig"), 0755) + + var links = map[string]string { + "somelinkedconfig": "configpath", + } + + viper.Set("links", links) + + dotctl := cmd.RootCmd + + actual := new(bytes.Buffer) + + dotctl.SetOut(actual) + dotctl.SetErr(actual) + dotctl.SetArgs([]string{"status"}) + + dotctl.Execute() + + expected := "Config directories currently in dotfile path:\n" + + "someconfig\nsomelinkedconfig - configpath\n" + + assert.Equal(t, expected, actual.String(), "actual differs from expected") +}