func (c *HelpToolCommand) Run(ctx *cmd.Context) error { var hookctx dummyHookContext if c.tool == "" { // Ripped from SuperCommand. We could Run() a SuperCommand // with "help commands", but then the implicit "help" command // shows up. names := jujuc.CommandNames() cmds := make([]cmd.Command, 0, len(names)) longest := 0 for _, name := range names { if c, err := jujuc.NewCommand(hookctx, name); err == nil { if len(name) > longest { longest = len(name) } cmds = append(cmds, c) } } for _, c := range cmds { info := c.Info() fmt.Fprintf(ctx.Stdout, "%-*s %s\n", longest, info.Name, info.Purpose) } } else { c, err := jujuc.NewCommand(hookctx, c.tool) if err != nil { return err } info := c.Info() f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError) c.SetFlags(f) ctx.Stdout.Write(info.Help(f)) } return nil }
func (s *ToolsSuite) TestEnsureJujucSymlinks(c *C) { jujudPath := filepath.Join(s.toolsDir, "jujud") err := ioutil.WriteFile(jujudPath, []byte("assume sane"), 0755) c.Assert(err, IsNil) assertLink := func(path string) time.Time { target, err := os.Readlink(path) c.Assert(err, IsNil) c.Assert(target, Equals, "./jujud") fi, err := os.Lstat(path) c.Assert(err, IsNil) return fi.ModTime() } // Check that EnsureJujucSymlinks writes appropriate symlinks. err = uniter.EnsureJujucSymlinks(s.toolsDir) c.Assert(err, IsNil) mtimes := map[string]time.Time{} for _, name := range jujuc.CommandNames() { tool := filepath.Join(s.toolsDir, name) mtimes[tool] = assertLink(tool) } // Check that EnsureJujucSymlinks doesn't overwrite things that don't need to be. err = uniter.EnsureJujucSymlinks(s.toolsDir) c.Assert(err, IsNil) for tool, mtime := range mtimes { c.Assert(assertLink(tool), Equals, mtime) } }
// EnsureJujucSymlinks creates a symbolic link to jujuc within dir for each // hook command. If the commands already exist, this operation does nothing. func EnsureJujucSymlinks(dir string) (err error) { for _, name := range jujuc.CommandNames() { // The link operation fails when the target already exists, // so this is a no-op when the command names already // exist. err := os.Symlink("./jujuc", filepath.Join(dir, name)) if err == nil { continue } // TODO(rog) drop LinkError check when fix is released (see http://codereview.appspot.com/6442080/) if e, ok := err.(*os.LinkError); !ok || !os.IsExist(e.Err) { return fmt.Errorf("cannot initialize hook commands in %q: %v", dir, err) } } return nil }