make links a map in config (#19)

* make links a map in config
pull/20/head
Marcus Kok 2 years ago committed by GitHub
parent f41e35d956
commit 0b042d8637
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -32,8 +32,11 @@ func runAddCommand(cmd *cobra.Command, args []string) {
configSrc := args[0]
dirs := strings.Split(configSrc, "/")
name := dirs[len(dirs) - 1]
viper.Set(name, configSrc)
name := dirs[len(dirs) - 1] // take the last section of the path, this should be the name
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)

@ -25,20 +25,17 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
fs := FileSystem
fmt.Println("Symlinking dotfiles...")
dotfileRoot := viper.Get("dotfile-path").(string)
entries, err := afero.ReadDir(fs, dotfileRoot)
if err != nil {
log.Fatalf("Could not read dotfiles directory: %s\n",err)
}
for _, entry := range(entries) {
configName := entry.Name()
links := viper.GetStringMapString("links")
for configName, configPath := range links {
if configName == ".git" || configName == "dotctl" {
continue
}
dotPath := filepath.Join(dotfileRoot, entry.Name())
dotPath := filepath.Join(dotfileRoot, configName)
configPath := viper.GetString(configName)
if configPath == ""{
fmt.Fprintf(cmd.OutOrStdout(), "Warning: could not find config for %s\n", entry.Name())
fmt.Fprintf(cmd.OutOrStdout(), "Warning: could not find config for %s\n", configName)
}
@ -58,12 +55,15 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
if(testing == true) {
fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath)
} else {
err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
err := afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
if err != nil {
log.Fatalf("Cannot symlink %s: %s\n", configName, err.Error())
} else {
fmt.Printf("%s linked\n", configName)
}
}
}
if err != nil {
log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error())
}
}
}

@ -43,7 +43,7 @@ func init() {
// Cobra also supports local flags, which will only run
// when this action is called directly.
defaultDotPath := os.Getenv("HOME") + "/.dotfiles/"
defaultDotPath := os.Getenv("HOME") + "/dotfiles/"
defaultConfPath := os.Getenv("HOME") + "/.config/"
RootCmd.PersistentFlags().StringVar(
&DotfilePath,
@ -69,6 +69,8 @@ func init() {
viper.AddConfigPath("./tmp/dotfiles/dotctl")
viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl"))
viper.SetDefault("links", map[string]string{})
err := viper.ReadInConfig()
if err != nil {

@ -26,7 +26,7 @@ func TestLinkCommand(t *testing.T) {
homedir := os.Getenv("HOME")
someconfig := filepath.Join(homedir, ".config/someconfig/")
somedot := filepath.Join(homedir, ".dotfiles/someconfig/")
somedot := filepath.Join(homedir, "dotfiles/someconfig/")
expected := fmt.Sprintf("%s,%s", someconfig, somedot)
@ -36,16 +36,17 @@ func TestLinkCommand(t *testing.T) {
}
func setUpTesting() {
viper.Set("testing", true)
fs := cmd.FileSystem
homedir := os.Getenv("HOME")
fs.MkdirAll(filepath.Join(homedir, ".dotfiles/dotctl"), 0755)
fs.Create(filepath.Join(homedir, ".dotfiles/dotctl/config"))
fs.MkdirAll(filepath.Join(homedir, ".dotfiles/someconfig/"), 0755)
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("dotfile-path", filepath.Join(homedir, "dotfiles"))
viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/"))
viper.Set("testing", true)
}
func tearDownTesting() {

Loading…
Cancel
Save