github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/worker/uniter/tools.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/juju/worker/uniter/jujuc"
    12  )
    13  
    14  // EnsureJujucSymlinks creates a symbolic link to jujuc within dir for each
    15  // hook command. If the commands already exist, this operation does nothing.
    16  func EnsureJujucSymlinks(dir string) (err error) {
    17  	for _, name := range jujuc.CommandNames() {
    18  		// The link operation fails when the target already exists,
    19  		// so this is a no-op when the command names already
    20  		// exist.
    21  		err := os.Symlink("./jujud", filepath.Join(dir, name))
    22  		if err == nil {
    23  			continue
    24  		}
    25  		// TODO(rog) drop LinkError check when fix is released (see http://codereview.appspot.com/6442080/)
    26  		if e, ok := err.(*os.LinkError); !ok || !os.IsExist(e.Err) {
    27  			return fmt.Errorf("cannot initialize hook commands in %q: %v", dir, err)
    28  		}
    29  	}
    30  	return nil
    31  }