diff --git a/cmd/init.go b/cmd/init.go index b4f8d66..32695be 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -8,18 +8,19 @@ import ( "github.com/Marcusk19/bender/tools" "github.com/go-git/go-git/v5" + "github.com/spf13/afero" "github.com/spf13/cobra" + "github.com/spf13/viper" ) -var fs = tools.AppFs func init() { RootCmd.AddCommand(initCommand) } -func copyExistingConfigs(programs []string, destRootOpt ...string) { +func copyExistingConfigs(programs []string, fs afero.Fs, destRootOpt ...string) { // takes list of programs and backs up configs for them - destRoot := os.Getenv("HOME") + "/.dotfiles/" + destRoot := DotfilePath if len(destRootOpt) > 0 { destRoot = destRootOpt[0] } @@ -27,19 +28,18 @@ func copyExistingConfigs(programs []string, destRootOpt ...string) { configRoot := ConfigPath for _, program := range(programs) { // TODO: do something here - print(configRoot + program) err := tools.CopyDir(fs, filepath.Join(configRoot, program), filepath.Join(destRoot, program)) if err != nil { - log.Fatal(err) + log.Fatalf("Problem copying %s", err.Error()) } } } -func createDotfileStructure(programs []string) { +func createDotfileStructure(programs []string, fs afero.Fs) { // takes list of programs and creates dotfiles for them - dotfileRoot := os.Getenv("HOME") + "/.dotfiles/" + dotfileRoot := DotfilePath + fmt.Printf("creating dotfile directory structure at %s\n", dotfileRoot) for _, program := range(programs) { - fmt.Printf("attempting to create directory %s%s\n", dotfileRoot, program) if err := fs.MkdirAll(dotfileRoot + program, os.ModePerm); err != nil { log.Fatal(err) } @@ -51,6 +51,13 @@ var initCommand = &cobra.Command { Short: "Copy configs to dotfile directory", Long: "Searches existing config directory for configs and then copies them to dotfile directory", Run: func(cmd *cobra.Command, args []string) { + + fs := FileSystem + + if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { + log.Fatalf("wrong filesystem, got %s", fs.Name()) + } + var rootpath string if len(args) <= 0 { fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n") @@ -60,7 +67,7 @@ var initCommand = &cobra.Command { } if rootpath[len(rootpath)-1:] != "/" { - log.Fatal("path needs trailing slash") + log.Fatal("path needs trailing slash\n") } // TODO make a configurable list of binaries we want to look for @@ -69,10 +76,10 @@ var initCommand = &cobra.Command { acceptedprograms[0] = "nvim" acceptedprograms[1] = "tmux" acceptedprograms[2] = "alacritty" - - err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error { + + err := afero.Walk(fs, rootpath, func(path string, info os.FileInfo, err error) error { if err != nil { - log.Fatalf("problem walking path %s", err) + log.Fatalf("problem walking path %s\n", err) return nil } @@ -88,17 +95,19 @@ var initCommand = &cobra.Command { log.Fatal(err) } - fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") + fmt.Fprintf(cmd.OutOrStdout(), "binaries found: \n =======================\n") for _, program := range(programs) { fmt.Fprintf(cmd.OutOrStdout(), program + "\n" ) } - createDotfileStructure(programs) - copyExistingConfigs(programs) + createDotfileStructure(programs, fs) + copyExistingConfigs(programs, fs) - _, err = git.PlainInit(filepath.Join(os.Getenv("HOME"), "/.dotfiles"), false) - if err != nil { - log.Fatal(err) + if (viper.Get("testing") != true){ + _, err = git.PlainInit(DotfilePath, false) + if err != nil { + log.Fatal(err) + } } fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") }, diff --git a/cmd/link.go b/cmd/link.go index bb46d74..20cb3d9 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -19,6 +19,7 @@ var linkCommand = &cobra.Command { } func runLinkCommand(cmd *cobra.Command, args []string) { + fs := UseFilesystem() fmt.Println("Symlinking dotfiles...") entries, err := afero.ReadDir(fs, DotfilePath) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 8d67e5f..8878537 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,12 +1,13 @@ /* Copyright © 2024 Marcus Kok - */ package cmd import ( "os" + "path/filepath" + "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -32,6 +33,8 @@ func Execute() { var DotfilePath string var ConfigPath string +var FileSystem afero.Fs + func init() { // define flags and config sections @@ -39,6 +42,7 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. + print("init of root\n") defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" defaultConfPath := os.Getenv("HOME") + "/.config/" RootCmd.PersistentFlags().StringVar( @@ -54,7 +58,49 @@ func init() { "Path pointing to config directory", ) viper.BindPFlag("dotfile-path", RootCmd.PersistentFlags().Lookup("dotfile-path")) - RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + viper.BindPFlag("config-path", RootCmd.PersistentFlags().Lookup("config-path")) + + viper.BindEnv("testing") + viper.SetDefault("testing", false) + + FileSystem = UseFilesystem() + +} + +func UseFilesystem() afero.Fs { + testing := viper.Get("testing") + if(testing == true) { + print("Using temporary testing filesystem\n") + return afero.NewMemMapFs() + } else { + return afero.NewOsFs() + } } +func SetUpForTesting() afero.Fs { + print("Setting up testing environment\n") + viper.Set("testing", true) + fs := UseFilesystem() + + homedir := "bender_test/" + fs.MkdirAll(filepath.Join(homedir, ".config/"), 0755) + fs.MkdirAll(filepath.Join(homedir, ".dotfiles/"), 0755) + + fs.Mkdir("bin/", 0755) + fs.Create("bin/alacritty") + fs.Create("bin/nvim") + fs.Create("bin/tmux") + + fs.Mkdir(filepath.Join(homedir, ".config/alacritty"), 0755) + fs.Mkdir(filepath.Join(homedir, ".config/nvim"), 0755) + fs.Mkdir(filepath.Join(homedir, ".config/tmux"), 0755) + + fs.Create(filepath.Join(homedir, ".config/alacritty/alacritty.conf")) + fs.Create(filepath.Join(homedir, ".config/nvim/nvim.conf")) + fs.Create(filepath.Join(homedir, ".config/tmux/tmux.conf")) + + FileSystem = fs + + return fs +} diff --git a/test/init_test.go b/test/init_test.go new file mode 100644 index 0000000..26f8f1f --- /dev/null +++ b/test/init_test.go @@ -0,0 +1,35 @@ +package test + +import ( + "bytes" + "path/filepath" + "testing" + + "github.com/Marcusk19/bender/cmd" + "github.com/spf13/afero" + "github.com/spf13/viper" +) + +func TestInitCommand(t *testing.T) { + print("setting test var\n") + viper.Set("testing", true) + + fs := cmd.SetUpForTesting() + + bender := cmd.RootCmd + actual := new(bytes.Buffer) + + bender.SetOut(actual) + bender.SetErr(actual) + bender.SetArgs([]string{"init", "bin/", "--dotfile-path=bender_test/.dotfiles", "--config-path=bender_test/.config"}) + + bender.Execute() + + homedir := "bender_test/" + + _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/alacritty/alacritty.conf")) + if err != nil { + t.Error(err.Error()) + } + +}