Plumbing flags

This commit is contained in:
Alex Yatskov 2015-04-02 18:31:36 +09:00
parent 688fed0482
commit 6fad7caba6
3 changed files with 39 additions and 24 deletions

27
link.go
View File

@ -34,11 +34,17 @@ type link struct {
Src string Src string
} }
func preparePath(loc string, force, clobber bool) error { func preparePath(loc string, flags int) error {
clobber := flags&optClobber == optClobber
force := flags&optForce == optForce
verbose := flags&optVerbose == optVerbose
if force { if force {
parentDir, _ := path.Split(loc) parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) { if _, err := os.Stat(parentDir); os.IsNotExist(err) {
if verbose {
log.Printf("Force creating path: '%s'", parentDir) log.Printf("Force creating path: '%s'", parentDir)
}
if err := os.MkdirAll(parentDir, 0777); err != nil { if err := os.MkdirAll(parentDir, 0777); err != nil {
return err return err
} }
@ -47,12 +53,16 @@ func preparePath(loc string, force, clobber bool) error {
if info, _ := os.Lstat(loc); info != nil { if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == os.ModeSymlink { if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if verbose {
log.Printf("Removing symlink: '%s'", loc) log.Printf("Removing symlink: '%s'", loc)
}
if err := os.Remove(loc); err != nil { if err := os.Remove(loc); err != nil {
return err return err
} }
} else if clobber { } else if clobber {
if verbose {
log.Print("Clobbering path: '%s'", loc) log.Print("Clobbering path: '%s'", loc)
}
if err := os.RemoveAll(loc); err != nil { if err := os.RemoveAll(loc); err != nil {
return err return err
} }
@ -62,7 +72,7 @@ func preparePath(loc string, force, clobber bool) error {
return nil return nil
} }
func (this link) install(srcDir, dstDir string) error { func (this link) install(srcDir, dstDir string, flags int) error {
if len(this.Dst) == 0 { if len(this.Dst) == 0 {
this.Dst = this.Src this.Dst = this.Src
} }
@ -70,22 +80,17 @@ func (this link) install(srcDir, dstDir string) error {
srcPath := path.Join(srcDir, this.Src) srcPath := path.Join(srcDir, this.Src)
dstPath := path.Join(dstDir, this.Dst) dstPath := path.Join(dstDir, this.Dst)
if !path.IsAbs(dstPath) {
return fmt.Errorf("Destination path is not absolute: '%s'", dstPath)
}
if !path.IsAbs(srcPath) {
return fmt.Errorf("Source path is not absolute: '%s'", srcPath)
}
if _, err := os.Stat(srcPath); os.IsNotExist(err) { if _, err := os.Stat(srcPath); os.IsNotExist(err) {
return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath) return fmt.Errorf("Source path does not exist in filesystem: '%s'", srcPath)
} }
if err := preparePath(dstPath, true, true); err != nil { if err := preparePath(dstPath, flags); err != nil {
return err return err
} }
if flags&optVerbose == optVerbose {
log.Printf("Linking: '%s' => '%s'", srcPath, dstPath) log.Printf("Linking: '%s' => '%s'", srcPath, dstPath)
}
return os.Symlink(srcPath, dstPath) return os.Symlink(srcPath, dstPath)
} }

26
main.go
View File

@ -31,6 +31,7 @@ import (
"os" "os"
"os/user" "os/user"
"path" "path"
"path/filepath"
) )
const ( const (
@ -53,16 +54,25 @@ func parse(filename string) (*config, error) {
return conf, nil return conf, nil
} }
func printUsageAndExit() { func fatalUsage() {
_, executable := path.Split(os.Args[0]) _, executable := path.Split(os.Args[0])
fmt.Errorf("Usage: %s [options] config_file [target_path]", executable) fmt.Printf("Usage: %s [options] config_file [target_path]\n", executable)
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)
} }
func absPath(path string) string {
path, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
return path
}
func main() { func main() {
currUsr, err := user.Current() currUsr, err := user.Current()
if err == nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -87,7 +97,7 @@ func main() {
} }
if flag.NArg() == 0 { if flag.NArg() == 0 {
printUsageAndExit() fatalUsage()
} }
conf, err := parse(flag.Arg(0)) conf, err := parse(flag.Arg(0))
@ -98,17 +108,17 @@ func main() {
switch *action { switch *action {
case "install": case "install":
if flag.NArg() >= 2 { if flag.NArg() >= 2 {
if err := conf.install(flag.Arg(1), *dstDir, *taskName, flags); err != nil { if err := conf.install(absPath(flag.Arg(1)), absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {
printUsageAndExit() fatalUsage()
} }
case "uninstall": case "uninstall":
if err := conf.uninstall(*dstDir, *taskName, flags); err != nil { if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
default: default:
printUsageAndExit() fatalUsage()
} }
} }

View File

@ -42,7 +42,7 @@ func (this task) install(srcDir, dstDir string, conf *config, flags int) error {
} }
for _, link := range this.Links { for _, link := range this.Links {
if err := link.install(srcDir, dstDir); err != nil { if err := link.install(srcDir, dstDir, flags); err != nil {
return err return err
} }
} }