make links a map in config

pull/19/head
Marcus Kok 2 years ago
parent f41e35d956
commit 60f33f14da

@ -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 {

Loading…
Cancel
Save