This commit is contained in:
Alex Yatskov 2015-04-05 16:53:32 +09:00
parent eac74e82cb
commit 1f8da243cf
4 changed files with 32 additions and 82 deletions

View File

@ -36,12 +36,3 @@ func (this *config) install(srcDir, dstDir, taskName string, flags int) error {
return task.install(srcDir, dstDir, this, flags)
}
func (this *config) uninstall(dstDir, taskName string, flags int) error {
task, ok := this.Tasks[taskName]
if !ok {
return fmt.Errorf("Task not found: '%s'", taskName)
}
return task.uninstall(dstDir, this, flags)
}

View File

@ -75,7 +75,7 @@ func usage() {
flag.PrintDefaults()
}
func absPath(path string) string {
func makeAbsPath(path string) string {
path, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
@ -91,8 +91,7 @@ func main() {
}
taskName := flag.String("task", "default", "name of task to execute")
action := flag.String("action", "install", "install or uninstall symlinks")
dstDir := flag.String("dest", currUsr.HomeDir, "target directory for symlinks")
dstDir := flag.String("dest", currUsr.HomeDir, "target directory for tasks")
force := flag.Bool("force", true, "create parent directories to target")
clobber := flag.Bool("clobber", false, "delete files and directories at target")
verbose := flag.Bool("verbose", false, "verbose output")
@ -111,31 +110,16 @@ func main() {
flags |= optVerbose
}
if flag.NArg() == 0 {
usage()
os.Exit(2)
}
conf, err := parse(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
switch *action {
case "install":
if flag.NArg() >= 2 {
if err := conf.install(absPath(flag.Arg(1)), absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err)
}
} else {
usage()
os.Exit(2)
}
case "uninstall":
if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil {
if flag.NArg() == 2 {
conf, err := parse(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
default:
if err := conf.install(makeAbsPath(flag.Arg(1)), makeAbsPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err)
}
} else {
usage()
os.Exit(2)
}

39
link.go
View File

@ -57,20 +57,23 @@ func cleanPath(loc string, flags int) error {
return nil
}
func prepInstallPath(loc string, flags int) error {
if flags&optForce == optForce {
parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
if flags&optVerbose == optVerbose {
log.Printf("Force creating path: '%s'", parentDir)
}
if err := os.MkdirAll(parentDir, 0777); err != nil {
return err
}
func createPath(loc string, flags int) error {
if flags&optForce == 0 {
return nil
}
parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
if flags&optVerbose == optVerbose {
log.Printf("Force creating path: '%s'", parentDir)
}
if err := os.MkdirAll(parentDir, 0777); err != nil {
return err
}
}
return cleanPath(loc, flags)
return nil
}
func (this *link) source() string {
@ -106,7 +109,11 @@ func (this *link) install(srcDir, dstDir string, flags int) error {
return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath)
}
if err := prepInstallPath(dstPath, flags); err != nil {
if err := createPath(dstPath, flags); err != nil {
return err
}
if err := cleanPath(dstPath, flags); err != nil {
return err
}
@ -116,11 +123,3 @@ func (this *link) install(srcDir, dstDir string, flags int) error {
return os.Symlink(srcPath, dstPath)
}
func (this *link) uninstall(dstDir string, flags int) error {
if !this.valid() {
return fmt.Errorf("Link element is invalid")
}
return cleanPath(path.Join(dstDir, this.destination()), flags)
}

30
task.go
View File

@ -31,47 +31,23 @@ type task struct {
Links []link
}
func (this *task) walk(conf *config, depFunc func(depTask *task) error, linkFunc func(currLink *link) error) error {
func (this *task) install(srcDir, dstDir string, conf *config, flags int) error {
for _, depName := range this.Deps {
depTask, ok := conf.Tasks[depName]
if !ok {
return fmt.Errorf("Task dependency not found: '%s'", depName)
}
if err := depFunc(&depTask); err != nil {
if err := depTask.install(srcDir, dstDir, conf, flags); err != nil {
return err
}
}
for _, currLink := range this.Links {
if err := linkFunc(&currLink); err != nil {
if err := currLink.install(srcDir, dstDir, flags); err != nil {
return err
}
}
return nil
}
func (this *task) install(srcDir, dstDir string, conf *config, flags int) error {
depWalker := func(depTask *task) error {
return depTask.install(srcDir, dstDir, conf, flags)
}
linkWalker := func(currLink *link) error {
return currLink.install(srcDir, dstDir, flags)
}
return this.walk(conf, depWalker, linkWalker)
}
func (this *task) uninstall(dstDir string, conf *config, flags int) error {
depWalker := func(depTask *task) error {
return depTask.uninstall(dstDir, conf, flags)
}
linkWalker := func(currLink *link) error {
return currLink.uninstall(dstDir, flags)
}
return this.walk(conf, depWalker, linkWalker)
}