Adding unlink parameter

This commit is contained in:
Alex Yatskov 2015-09-06 14:45:57 +09:00
parent 7f23ccc9f5
commit 67a97669ca
3 changed files with 40 additions and 26 deletions

View File

@ -35,9 +35,10 @@ const (
flagClobber = 1 << iota flagClobber = 1 << iota
flagForce flagForce
flagVerbose flagVerbose
flagNoCmd flagNoCmds
flagNoLink flagNoLinks
flagNoMacro flagNoMacro
flagUnlink = flagNoCmds | (1 << iota)
) )
func usage() { func usage() {
@ -58,9 +59,10 @@ func main() {
force := flag.Bool("force", true, "create parent directories to target") force := flag.Bool("force", true, "create parent directories to target")
clobber := flag.Bool("clobber", false, "delete files and directories at target") clobber := flag.Bool("clobber", false, "delete files and directories at target")
verbose := flag.Bool("verbose", false, "verbose output") verbose := flag.Bool("verbose", false, "verbose output")
nocmd := flag.Bool("nocmd", false, "don't execute commands") nocmds := flag.Bool("nocmds", false, "don't execute commands")
nolink := flag.Bool("nolink", false, "don't create links") nolinks := flag.Bool("nolinks", false, "don't create links")
variant := flag.String("variant", "", "execution variant") variant := flag.String("variant", "", "execution variant for tasks and macros")
unlink := flag.Bool("unlink", false, "remove existing links instead of creating them")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
@ -75,11 +77,14 @@ func main() {
if *verbose { if *verbose {
flags |= flagVerbose flags |= flagVerbose
} }
if *nocmd { if *nocmds {
flags |= flagNoCmd flags |= flagNoCmds
} }
if *nolink { if *nolinks {
flags |= flagNoLink flags |= flagNoLinks
}
if *unlink {
flags |= flagUnlink
} }
if flag.NArg() == 2 { if flag.NArg() == 2 {

39
link.go
View File

@ -115,23 +115,32 @@ func processLink(params []string, conf *config) error {
dstPathAbs = path.Join(conf.dstDir, dstPath) dstPathAbs = path.Join(conf.dstDir, dstPath)
} }
if _, err := os.Stat(srcPathAbs); os.IsNotExist(err) { if conf.flags&flagUnlink == 0 {
return fmt.Errorf("source path %s does not exist in filesystem", srcPathAbs) if _, err := os.Stat(srcPathAbs); os.IsNotExist(err) {
} return fmt.Errorf("source path %s does not exist in filesystem", srcPathAbs)
}
if err := try(func() error { return createPath(dstPathAbs, conf.flags, mode) }); err != nil { if err := try(func() error { return createPath(dstPathAbs, conf.flags, mode) }); err != nil {
return err return err
} }
if err := try(func() error { return cleanPath(dstPathAbs, conf.flags) }); err != nil { if err := try(func() error { return cleanPath(dstPathAbs, conf.flags) }); err != nil {
return err return err
} }
if conf.flags&flagVerbose != 0 { if conf.flags&flagVerbose != 0 {
log.Printf("linking %s to %s", srcPathAbs, dstPathAbs) log.Printf("linking %s to %s", srcPathAbs, dstPathAbs)
} }
return try(func() error { return try(func() error {
return os.Symlink(srcPathAbs, dstPathAbs) return os.Symlink(srcPathAbs, dstPathAbs)
}) })
} else {
stat, err := os.Lstat(dstPathAbs)
if os.IsNotExist(err) || stat.Mode()&os.ModeSymlink == 0 {
return nil
}
return try(func() error { return cleanPath(dstPathAbs, conf.flags) })
}
} }

View File

@ -47,7 +47,7 @@ func (t *task) process(conf *config) error {
} }
} }
if conf.flags&flagNoCmd == 0 { if conf.flags&flagNoCmds == 0 {
for _, currCmd := range t.Cmds { for _, currCmd := range t.Cmds {
if err := processCmd(currCmd, conf); err != nil { if err := processCmd(currCmd, conf); err != nil {
return err return err
@ -55,7 +55,7 @@ func (t *task) process(conf *config) error {
} }
} }
if conf.flags&flagNoLink == 0 { if conf.flags&flagNoLinks == 0 {
for _, currLink := range t.Links { for _, currLink := range t.Links {
if err := processLink(currLink, conf); err != nil { if err := processLink(currLink, conf); err != nil {
return err return err