Work in progress

This commit is contained in:
Alex Yatskov 2015-04-01 18:44:10 +09:00
parent 4adc700f1c
commit b548ed7d15
5 changed files with 72 additions and 16 deletions

View File

@ -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)
}

View File

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

62
link.go
View File

@ -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)
}

View File

@ -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)
}

View File

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