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

35
link.go
View File

@ -34,11 +34,17 @@ type link struct {
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 {
parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
log.Printf("Force creating path: '%s'", parentDir)
if verbose {
log.Printf("Force creating path: '%s'", parentDir)
}
if err := os.MkdirAll(parentDir, 0777); err != nil {
return err
}
@ -47,12 +53,16 @@ func preparePath(loc string, force, clobber bool) error {
if info, _ := os.Lstat(loc); info != nil {
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
log.Printf("Removing symlink: '%s'", loc)
if verbose {
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 verbose {
log.Print("Clobbering path: '%s'", loc)
}
if err := os.RemoveAll(loc); err != nil {
return err
}
@ -62,7 +72,7 @@ func preparePath(loc string, force, clobber bool) error {
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 {
this.Dst = this.Src
}
@ -70,22 +80,17 @@ func (this link) install(srcDir, dstDir string) error {
srcPath := path.Join(srcDir, this.Src)
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) {
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
}
log.Printf("Linking: '%s' => '%s'", srcPath, dstPath)
if flags&optVerbose == optVerbose {
log.Printf("Linking: '%s' => '%s'", srcPath, dstPath)
}
return os.Symlink(srcPath, dstPath)
}

26
main.go
View File

@ -31,6 +31,7 @@ import (
"os"
"os/user"
"path"
"path/filepath"
)
const (
@ -53,16 +54,25 @@ func parse(filename string) (*config, error) {
return conf, nil
}
func printUsageAndExit() {
func fatalUsage() {
_, 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()
os.Exit(1)
}
func absPath(path string) string {
path, err := filepath.Abs(path)
if err != nil {
log.Fatal(err)
}
return path
}
func main() {
currUsr, err := user.Current()
if err == nil {
if err != nil {
log.Fatal(err)
}
@ -87,7 +97,7 @@ func main() {
}
if flag.NArg() == 0 {
printUsageAndExit()
fatalUsage()
}
conf, err := parse(flag.Arg(0))
@ -98,17 +108,17 @@ func main() {
switch *action {
case "install":
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)
}
} else {
printUsageAndExit()
fatalUsage()
}
case "uninstall":
if err := conf.uninstall(*dstDir, *taskName, flags); err != nil {
if err := conf.uninstall(absPath(*dstDir), *taskName, flags); err != nil {
log.Fatal(err)
}
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 {
if err := link.install(srcDir, dstDir); err != nil {
if err := link.install(srcDir, dstDir, flags); err != nil {
return err
}
}