Work in progress
This commit is contained in:
parent
4adc700f1c
commit
b548ed7d15
@ -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)
|
||||
}
|
||||
|
10
config.toml
10
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]]
|
||||
|
62
link.go
62
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)
|
||||
}
|
||||
|
2
main.go
2
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)
|
||||
}
|
||||
|
10
profile.go
10
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user