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 *gc.C) { jujudPath := filepath.Join(s.toolsDir, names.Jujud) err := ioutil.WriteFile(jujudPath, []byte("assume sane"), 0755) c.Assert(err, gc.IsNil) assertLink := func(path string) time.Time { target, err := symlink.Read(path) c.Assert(err, gc.IsNil) c.Assert(target, gc.Equals, jujudPath) fi, err := os.Lstat(path) c.Assert(err, gc.IsNil) return fi.ModTime() } // Check that EnsureJujucSymlinks writes appropriate symlinks. err = uniter.EnsureJujucSymlinks(s.toolsDir) c.Assert(err, gc.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, gc.IsNil) for tool, mtime := range mtimes { c.Assert(assertLink(tool), gc.Equals, mtime) } }
func (suite *HelpToolSuite) TestHelpTool(c *gc.C) { expectedNames := jujuc.CommandNames() output := badrun(c, 0, "help-tool") lines := strings.Split(strings.TrimSpace(output), "\n") for i, line := range lines { lines[i] = strings.Fields(line)[0] } c.Assert(lines, gc.DeepEquals, expectedNames) }
// 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("./jujud", 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 }