add add command

pull/12/head
Marcusk19 2 years ago
parent f2ca64670a
commit ec72e4a6ad

@ -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)
}

@ -63,16 +63,19 @@ func runInitCommand(cmd *cobra.Command, args []string) {
log.Fatalf("Unable to create dotfile structure: %s", error.Error(err)) 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 { if err != nil {
panic(fmt.Errorf("Unable to create config file %w", err)) panic(fmt.Errorf("Unable to create config file %w", err))
} }
viper.WriteConfig()
if (viper.Get("testing") != "true"){ if (viper.Get("testing") != "true"){
_, err = git.PlainInit(DotfilePath, false) _, err = git.PlainInit(DotfilePath, false)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n")
} }

@ -14,11 +14,8 @@ var linkCommand = &cobra.Command {
Run: runLinkCommand, Run: runLinkCommand,
} }
var DryRun bool
func init() { func init() {
RootCmd.AddCommand(linkCommand) 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) { func runLinkCommand(cmd *cobra.Command, args []string) {

@ -33,6 +33,7 @@ func Execute() {
var DotfilePath string var DotfilePath string
var ConfigPath string var ConfigPath string
var DryRun bool
var FileSystem afero.Fs var FileSystem afero.Fs
@ -56,13 +57,14 @@ func init() {
defaultConfPath, defaultConfPath,
"Path pointing to config directory", "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("dotfile-path", RootCmd.PersistentFlags().Lookup("dotfile-path"))
viper.BindPFlag("config-path", RootCmd.PersistentFlags().Lookup("config-path")) viper.BindPFlag("config-path", RootCmd.PersistentFlags().Lookup("config-path"))
viper.BindEnv("testing") viper.BindEnv("testing")
viper.SetDefault("testing", false) viper.SetDefault("testing", false)
viper.SetConfigName("bender.yml") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.AddConfigPath(filepath.Join(defaultDotPath, "bender")) viper.AddConfigPath(filepath.Join(defaultDotPath, "bender"))
viper.AddConfigPath("./bender") viper.AddConfigPath("./bender")

@ -26,7 +26,7 @@ func TestInitCommand(t *testing.T) {
homedir := "bender_test/" 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 { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} }

@ -34,6 +34,7 @@ func CopyFile(os afero.Fs, srcFile, destFile string) error{
destination, err := os.Create(destFile) destination, err := os.Create(destFile)
if err != nil { if err != nil {
fmt.Printf("Error creating destination file %s\n", destFile)
return err return err
} }
defer destination.Close() 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 { func CopyDir(os afero.Fs, srcDir, destDir string) error {
os.Mkdir(destDir, 0755)
entries, err := afero.ReadDir(os, srcDir) entries, err := afero.ReadDir(os, srcDir)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save