From b548ed7d151d5897ab4a5ea6d25df107624d851e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 1 Apr 2015 18:44:10 +0900 Subject: [PATCH] Work in progress --- config.go | 4 ++-- config.toml | 10 +-------- link.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++- main.go | 2 -- profile.go | 10 +++++++-- 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/config.go b/config.go index 7ea0546..2a7e9c3 100644 --- a/config.go +++ b/config.go @@ -28,11 +28,11 @@ type config struct { Profs map[string]profile } -func (this config) process(name, src, dst string) error { +func (this config) process(name, srcDir, dstDir string) error { prof, ok := this.Profs[name] if !ok { return errors.New("Profile not found") } - return prof.process(src, dst, this) + return prof.process(srcDir, dstDir, this) } diff --git a/config.toml b/config.toml index 0bc7ab8..ac3737b 100644 --- a/config.toml +++ b/config.toml @@ -1,12 +1,4 @@ [profs.dev] - [[profs.dev.links]] - src = "vim/.vimrc" - dst = ".vimrc" - - [[profs.dev.links]] - src = "vim/.vim" - dst = ".vim" - [[profs.dev.links]] src = ".gitconfig" @@ -21,7 +13,7 @@ src = ".filezilla" [[profs.base.links]] - src = "profsile" + src = "profile" [profs.study] [[profs.study.links]] diff --git a/link.go b/link.go index 704d9d3..66b9557 100644 --- a/link.go +++ b/link.go @@ -22,11 +22,71 @@ package main +import ( + "errors" + "fmt" + "log" + "os" + "path" +) + type link struct { Dst string Src string } -func (this link) process(src, dst string) error { +func preparePath(loc string, force, clobber bool) error { + if force { + parentDir, _ := path.Split(loc) + if _, err := os.Stat(parentDir); os.IsNotExist(err) { + log.Printf("Force creating path: '%s'", parentDir) + if err := os.MkdirAll(parentDir, 0777); err != nil { + return err + } + } + } + + if info, _ := os.Lstat(loc); info != nil { + if info.Mode()&os.ModeSymlink == os.ModeSymlink { + log.Printf("Removing symlink: '%s'", loc) + if err := os.Remove(loc); err != nil { + return err + } + } else if clobber { + log.Print("Clobbering path: '%s'", loc) + if err := os.RemoveAll(loc); err != nil { + return err + } + } + } + return nil } + +func (this link) process(srcDir, dstDir string) error { + if len(this.Dst) == 0 { + this.Dst = this.Src + } + + srcPath := path.Join(srcDir, this.Src) + dstPath := path.Join(dstDir, this.Dst) + + if !path.IsAbs(dstPath) { + return errors.New(fmt.Sprintf("Destination path is not absolute: '%s'", dstPath)) + } + + if !path.IsAbs(srcPath) { + return errors.New(fmt.Sprintf("Source path is not absolute: '%s'", srcPath)) + } + + if _, err := os.Stat(srcPath); os.IsNotExist(err) { + return errors.New(fmt.Sprintf("Source path does not exist in filesystem: '%s'", srcPath)) + } + + if err := preparePath(dstPath, true, true); err != nil { + return err + } + + log.Printf("Linking: '%s' => '%s'", srcPath, dstPath) + return os.Symlink(srcPath, dstPath) +} diff --git a/main.go b/main.go index 8a558c5..eed1b68 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,4 @@ func main() { if err := conf.process("flatline", "/mnt/storage/sync/Dropbox", "/mnt/storage/projects/blah"); err != nil { log.Fatal(err) } - - log.Print(conf) } diff --git a/profile.go b/profile.go index 799d676..009a1ad 100644 --- a/profile.go +++ b/profile.go @@ -29,14 +29,20 @@ type profile struct { Links []link } -func (this profile) process(src, dst string, conf config) error { +func (this profile) process(srcDir, dstDir string, conf config) error { for _, name := range this.Deps { prof, ok := conf.Profs[name] if !ok { return errors.New("Profile dependency not found") } - if err := prof.process(src, dst, conf); err != nil { + if err := prof.process(srcDir, dstDir, conf); err != nil { + return err + } + } + + for _, link := range this.Links { + if err := link.process(srcDir, dstDir); err != nil { return err } }