diff --git a/cmd/add.go b/cmd/add.go new file mode 100644 index 0000000..13546d1 --- /dev/null +++ b/cmd/add.go @@ -0,0 +1,51 @@ +package cmd + +import ( + "fmt" + "log" + "path/filepath" + "strings" + + "github.com/Marcusk19/bender/tools" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func init() { + RootCmd.AddCommand(addCommand) +} + +var addCommand = &cobra.Command { + Use: "add", + Short: "Adds config to be tracked by bender", + Long: "TODO: add longer description", // TODO add more description + Run: runAddCommand, +} + +func runAddCommand(cmd *cobra.Command, args []string) { + fs := FileSystem + + if len(args) <= 0 { + fmt.Println("ERROR: requires at least one argument") + return + } + + configSrc := args[0] + dirs := strings.Split(configSrc, "/") + name := dirs[len(dirs) - 1] + viper.Set(name, configSrc) + viper.WriteConfig() + + dotfileDest := filepath.Join(DotfilePath, name) + + if DryRun { + fmt.Printf("Will copy %s -> %s \n", configSrc, dotfileDest) + return + } + + err := tools.CopyDir(fs, configSrc, dotfileDest) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Copied %s -> %s\n", configSrc, dotfileDest) +} diff --git a/cmd/init.go b/cmd/init.go index b48550f..e95cdb9 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -63,16 +63,19 @@ func runInitCommand(cmd *cobra.Command, args []string) { log.Fatalf("Unable to create dotfile structure: %s", error.Error(err)) } - _, err = fs.Create(path.Join(DotfilePath, "bender/bender.yml")) + _, err = fs.Create(path.Join(DotfilePath, "bender/config")) if err != nil { panic(fmt.Errorf("Unable to create config file %w", err)) } + viper.WriteConfig() + 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 6f1bb66..8f5a5db 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -14,11 +14,8 @@ var linkCommand = &cobra.Command { Run: runLinkCommand, } -var DryRun bool - func init() { RootCmd.AddCommand(linkCommand) - linkCommand.Flags().BoolVarP(&DryRun, "dry-run", "d", false, "Only output which symlinks will be created") } func runLinkCommand(cmd *cobra.Command, args []string) { diff --git a/cmd/root.go b/cmd/root.go index f805061..ec99e4d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,7 @@ func Execute() { var DotfilePath string var ConfigPath string +var DryRun bool var FileSystem afero.Fs @@ -56,13 +57,14 @@ func init() { defaultConfPath, "Path pointing to config directory", ) + RootCmd.PersistentFlags().BoolVarP(&DryRun, "dry-run", "d", false, "Only output which symlinks will be created") viper.BindPFlag("dotfile-path", RootCmd.PersistentFlags().Lookup("dotfile-path")) viper.BindPFlag("config-path", RootCmd.PersistentFlags().Lookup("config-path")) viper.BindEnv("testing") viper.SetDefault("testing", false) - viper.SetConfigName("bender.yml") + viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(filepath.Join(defaultDotPath, "bender")) viper.AddConfigPath("./bender") diff --git a/test/init_test.go b/test/init_test.go index 1c1e481..e8b0c51 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -26,7 +26,7 @@ func TestInitCommand(t *testing.T) { homedir := "bender_test/" - _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/bender/bender.yml")) + _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/bender/config")) if err != nil { t.Error(err.Error()) } diff --git a/tools/copy.go b/tools/copy.go index e4773c6..5df6695 100644 --- a/tools/copy.go +++ b/tools/copy.go @@ -34,6 +34,7 @@ func CopyFile(os afero.Fs, srcFile, destFile string) error{ destination, err := os.Create(destFile) if err != nil { + fmt.Printf("Error creating destination file %s\n", destFile) return err } defer destination.Close() @@ -44,6 +45,7 @@ func CopyFile(os afero.Fs, srcFile, destFile string) error{ } func CopyDir(os afero.Fs, srcDir, destDir string) error { + os.Mkdir(destDir, 0755) entries, err := afero.ReadDir(os, srcDir) if err != nil { return err