use config for dotfile path, add some makefile scripts

pull/13/head
Marcusk19 2 years ago
parent 2060833611
commit bf92ff7c37

@ -0,0 +1,7 @@
clean:
rm -r test/bender_test
rm -r tmp
sandbox:
mkdir -p ./tmp/
cp -r ~/.config/ ./tmp/config

@ -34,16 +34,21 @@ func runAddCommand(cmd *cobra.Command, args []string) {
dirs := strings.Split(configSrc, "/")
name := dirs[len(dirs) - 1]
viper.Set(name, configSrc)
viper.WriteConfig()
err := viper.WriteConfig()
if err != nil {
fmt.Printf("Problem updating bender config %s", err)
}
dotfilePath := viper.Get("dotfile-path").(string)
dotfileDest := filepath.Join(DotfilePath, name)
dotfileDest := filepath.Join(dotfilePath, name)
if DryRun {
fmt.Printf("Will copy %s -> %s \n", configSrc, dotfileDest)
return
}
err := tools.CopyDir(fs, configSrc, dotfileDest)
err = tools.CopyDir(fs, configSrc, dotfileDest)
if err != nil {
log.Fatal(err)
}

@ -68,7 +68,10 @@ func runInitCommand(cmd *cobra.Command, args []string) {
panic(fmt.Errorf("Unable to create config file %w", err))
}
viper.WriteConfig()
err = viper.WriteConfig()
if err != nil {
fmt.Printf("Unable to write config on init: %s\n", err)
}
if (viper.Get("testing") != "true"){
_, err = git.PlainInit(DotfilePath, false)
@ -77,5 +80,5 @@ func runInitCommand(cmd *cobra.Command, args []string) {
}
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n")
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository at %s\n", DotfilePath)
}

@ -7,6 +7,7 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var linkCommand = &cobra.Command {
@ -21,16 +22,20 @@ func init() {
func runLinkCommand(cmd *cobra.Command, args []string) {
fs := UseFilesystem()
fmt.Println("Symlinking dotfiles...")
entries, err := afero.ReadDir(fs, DotfilePath)
dotfileRoot := viper.Get("dotfile-path").(string)
entries, err := afero.ReadDir(fs, dotfileRoot)
if err != nil {
log.Fatal(err)
}
for _, entry := range(entries) {
if entry.Name() == ".git" {
configName := entry.Name()
if configName == ".git" || configName == "bender" {
continue
}
dotPath := filepath.Join(DotfilePath, entry.Name())
configPath := filepath.Join(ConfigPath, entry.Name())
dotPath := filepath.Join(dotfileRoot, entry.Name())
configPath := viper.Get(configName).(string)
// destination needs to be removed before symlink
if(DryRun) {
@ -40,10 +45,16 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
fs.RemoveAll(configPath)
}
testing := viper.Get("testing")
if(DryRun) {
log.Printf("Will link %s -> %s\n", dotPath, configPath)
log.Printf("Will link %s -> %s\n", configPath, dotPath)
} else {
err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
if(testing == "true") {
fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath)
} else {
err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
}
}
if err != nil {
log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error())

@ -66,10 +66,11 @@ func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(filepath.Join(defaultDotPath, "bender"))
viper.AddConfigPath("./bender")
viper.AddConfigPath("./tmp/dotfiles/bender")
viper.AddConfigPath(filepath.Join(DotfilePath, "bender"))
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No config detected. You can generate one by using 'bender init'")
}

@ -20,13 +20,13 @@ func TestInitCommand(t *testing.T) {
bender.SetOut(actual)
bender.SetErr(actual)
bender.SetArgs([]string{"init", "--dotfile-path=bender_test/.dotfiles"})
bender.SetArgs([]string{"init", "--dotfile-path=bender_test/dotfiles"})
bender.Execute()
homedir := "bender_test/"
_, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/bender/config"))
_, err := afero.ReadFile(fs, filepath.Join(homedir, "dotfiles/bender/config"))
if err != nil {
t.Error(err.Error())
}

Loading…
Cancel
Save