lifecycleCommand provides install/upgrade/uninstall against the tools.yaml registry. The three verbs share a single implementation because Python treats them as aliases internally — install and upgrade do the same npm/ curl install, uninstall removes the package or binary.
(name, alias string)
| 15 | // treats them as aliases internally — install and upgrade do the same npm/ |
| 16 | // curl install, uninstall removes the package or binary. |
| 17 | func (a *App) lifecycleCommand(name, alias string) *cobra.Command { |
| 18 | var ( |
| 19 | dryRun bool |
| 20 | verbose bool |
| 21 | force bool |
| 22 | ) |
| 23 | title := strings.ToTitle(name[:1]) + name[1:] |
| 24 | cmd := &cobra.Command{ |
| 25 | Use: name + " [TARGET]", |
| 26 | Aliases: []string{alias}, |
| 27 | Short: title + " tools", |
| 28 | Args: cobra.MaximumNArgs(1), |
| 29 | RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | target := "all" |
| 31 | if len(args) > 0 { |
| 32 | target = args[0] |
| 33 | } |
| 34 | registry, err := tools.LoadDefault() |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | candidates, err := pickTargets(registry, target) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | out := cmd.OutOrStdout() |
| 43 | errw := cmd.ErrOrStderr() |
| 44 | if dryRun { |
| 45 | fmt.Fprintf(out, "Would %s %s\n", name, target) |
| 46 | for _, tool := range candidates { |
| 47 | fmt.Fprintf(out, " - %s\n", tool.Name) |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | failures := 0 |
| 52 | for _, tool := range candidates { |
| 53 | switch name { |
| 54 | case "install", "upgrade": |
| 55 | fmt.Fprintf(out, "%s %s ...\n", title, tool.Name) |
| 56 | code, err := tools.Install(tool, nil, ifVerbose(out, verbose), errw) |
| 57 | if errors.Is(err, tools.ErrNoInstallCommand) { |
| 58 | fmt.Fprintf(out, " %s has no install_cmd; skipping\n", tool.Name) |
| 59 | continue |
| 60 | } |
| 61 | if err != nil || code != 0 { |
| 62 | failures++ |
| 63 | fmt.Fprintf(errw, " failed (exit %d): %v\n", code, err) |
| 64 | continue |
| 65 | } |
| 66 | fmt.Fprintf(out, " %s OK\n", tool.Name) |
| 67 | case "uninstall": |
| 68 | if !force { |
| 69 | fmt.Fprintf(out, "Uninstalling %s (use --force to skip prompt next time)\n", tool.Name) |
| 70 | } |
| 71 | code, msg, err := tools.Uninstall(tool, nil, ifVerbose(out, verbose), errw) |
| 72 | if err != nil || code != 0 { |
| 73 | failures++ |
| 74 | fmt.Fprintf(errw, " failed (exit %d): %s — %v\n", code, msg, err) |
no test coverage detected