From 3a6284e45eb0248fd1d05b3161bdc655a2d691c0 Mon Sep 17 00:00:00 2001 From: Marcus Kok <47163063+Marcusk19@users.noreply.github.com> Date: Fri, 5 Apr 2024 12:31:58 -0400 Subject: [PATCH] add status command (#25) * add status command * new memmapfs on tests * fixing all tests --- cmd/add.go | 14 ++++++++----- cmd/init.go | 11 +++++----- cmd/status.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ test/init_test.go | 5 +++-- test/link_test.go | 35 +++++++++++--------------------- test/status_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 35 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/init.go b/cmd/init.go index d280648..3417bf9 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -55,6 +55,7 @@ func runInitCommand(cmd *cobra.Command, args []string) { fs := FileSystem // if user has passed a dotfile path flag need to add it to // viper's search path for a config file + testing := viper.GetBool("testing") viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl")) if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { @@ -71,12 +72,12 @@ func runInitCommand(cmd *cobra.Command, args []string) { panic(fmt.Errorf("Unable to create config file %w", err)) } - err = viper.WriteConfig() - if err != nil && viper.Get("testing") != true { - log.Fatalf("Unable to write config on init: %s\n", err) - } + if !testing { + err = viper.WriteConfig() + if err != nil && viper.Get("testing") != true { + log.Fatalf("Unable to write config on init: %s\n", err) + } - if (viper.Get("testing") != "true"){ _, err = git.PlainInit(DotfilePath, false) if err != nil { log.Fatal(err) 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/init_test.go b/test/init_test.go index 96f7fce..414e3eb 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -2,6 +2,7 @@ package test import ( "bytes" + "os" "path/filepath" "testing" @@ -20,11 +21,11 @@ func TestInitCommand(t *testing.T) { dotctl.SetOut(actual) dotctl.SetErr(actual) - dotctl.SetArgs([]string{"init", "--dotfile-path=dotctl_test/dotfiles"}) + dotctl.SetArgs([]string{"init"}) dotctl.Execute() - homedir := "dotctl_test/" + homedir := os.Getenv("HOME") _, err := afero.ReadFile(fs, filepath.Join(homedir, "dotfiles/dotctl/config")) if err != nil { diff --git a/test/link_test.go b/test/link_test.go index 231d178..3dcd3f4 100644 --- a/test/link_test.go +++ b/test/link_test.go @@ -8,14 +8,24 @@ import ( "testing" "github.com/Marcusk19/dotctl/cmd" + "github.com/spf13/afero" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) func TestLinkCommand(t *testing.T) { - oldDotfilePath := viper.GetString("dotfile-path") - setUpTesting() + viper.Set("testing", true) + cmd.FileSystem = afero.NewMemMapFs() + fs := cmd.FileSystem + homedir := os.Getenv("HOME") + + fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755) + links := map[string]string { + "someconfig": filepath.Join(homedir, ".config/someconfig"), + } + viper.Set("links", links) + dotctl := cmd.RootCmd actual := new(bytes.Buffer) @@ -25,32 +35,11 @@ func TestLinkCommand(t *testing.T) { dotctl.Execute() - homedir := os.Getenv("HOME") someconfig := filepath.Join(homedir, ".config/someconfig/") somedot := filepath.Join(homedir, "dotfiles/someconfig/") expected := fmt.Sprintf("%s,%s", someconfig, somedot) assert.Equal(t, expected, actual.String(), "actual differs from expected") - - tearDownTesting(oldDotfilePath) } -func setUpTesting() { - viper.Set("testing", true) - - fs := cmd.FileSystem - homedir := os.Getenv("HOME") - fakeLinks := map[string]string {"someconfig": filepath.Join(homedir, ".config/someconfig")} - viper.Set("links", fakeLinks) - fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755) - fs.Create(filepath.Join(homedir, "dotfiles/dotctl/config")) - - viper.Set("dotfile-path", filepath.Join(homedir, "dotfiles")) - viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/")) -} - -func tearDownTesting(oldDotfilePath string) { - viper.Set("dotfile-path", oldDotfilePath) - viper.WriteConfig() -} diff --git a/test/status_test.go b/test/status_test.go new file mode 100644 index 0000000..1eacf07 --- /dev/null +++ b/test/status_test.go @@ -0,0 +1,46 @@ +package test + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/Marcusk19/dotctl/cmd" + "github.com/spf13/afero" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestStatusCommand(t *testing.T) { + cmd.FileSystem = afero.NewMemMapFs() + 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") +}