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] configSrc := args[0]
dirs := strings.Split(configSrc, "/") dirs := strings.Split(configSrc, "/")
name := dirs[len(dirs) - 1] name := dirs[len(dirs) - 1] // take the last section of the path, this should be the name
viper.Set(name, configSrc)
links := viper.GetStringMap("links")
links[name] = configSrc
viper.Set("links", links)
err := viper.WriteConfig() err := viper.WriteConfig()
if err != nil { if err != nil {
fmt.Printf("Problem updating dotctl config %s", err) fmt.Printf("Problem updating dotctl config %s", err)

@ -25,20 +25,17 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
fs := FileSystem fs := FileSystem
fmt.Println("Symlinking dotfiles...") fmt.Println("Symlinking dotfiles...")
dotfileRoot := viper.Get("dotfile-path").(string) dotfileRoot := viper.Get("dotfile-path").(string)
entries, err := afero.ReadDir(fs, dotfileRoot)
if err != nil { links := viper.GetStringMapString("links")
log.Fatalf("Could not read dotfiles directory: %s\n",err)
} for configName, configPath := range links {
for _, entry := range(entries) {
configName := entry.Name()
if configName == ".git" || configName == "dotctl" { if configName == ".git" || configName == "dotctl" {
continue continue
} }
dotPath := filepath.Join(dotfileRoot, entry.Name()) dotPath := filepath.Join(dotfileRoot, configName)
configPath := viper.GetString(configName)
if configPath == ""{ 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) { if(testing == true) {
fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath) fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath)
} else { } 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 // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" defaultDotPath := os.Getenv("HOME") + "/dotfiles/"
defaultConfPath := os.Getenv("HOME") + "/.config/" defaultConfPath := os.Getenv("HOME") + "/.config/"
RootCmd.PersistentFlags().StringVar( RootCmd.PersistentFlags().StringVar(
&DotfilePath, &DotfilePath,
@ -69,6 +69,8 @@ func init() {
viper.AddConfigPath("./tmp/dotfiles/dotctl") viper.AddConfigPath("./tmp/dotfiles/dotctl")
viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl")) viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl"))
viper.SetDefault("links", map[string]string{})
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {

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

Loading…
Cancel
Save