()
| 20 | } |
| 21 | |
| 22 | func listCmd() *cobra.Command { |
| 23 | flags := listCmdFlags{} |
| 24 | cmd := &cobra.Command{ |
| 25 | Use: "list", |
| 26 | Aliases: []string{"ls"}, |
| 27 | Short: "List installed packages", |
| 28 | PreRunE: ensureNixInstalled, |
| 29 | RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | box, err := devbox.Open(&devopt.Opts{ |
| 31 | Dir: flags.config.path, |
| 32 | Stderr: cmd.ErrOrStderr(), |
| 33 | }) |
| 34 | if err != nil { |
| 35 | return errors.WithStack(err) |
| 36 | } |
| 37 | |
| 38 | if flags.outdated { |
| 39 | return printOutdatedPackages(cmd, box) |
| 40 | } |
| 41 | |
| 42 | for _, pkg := range box.AllPackagesIncludingRemovedTriggerPackages() { |
| 43 | resolvedVersion, err := pkg.ResolvedVersion() |
| 44 | if err != nil { |
| 45 | // Continue to print the package even if we can't resolve the version |
| 46 | // so that the user can see the error for this package, as well as get the |
| 47 | // results for the other packages |
| 48 | resolvedVersion = "<error resolving version>" |
| 49 | } |
| 50 | msg := "" |
| 51 | |
| 52 | // Print the resolved version, unless the user has specified a version already |
| 53 | if strings.HasSuffix(pkg.Versioned(), "latest") && resolvedVersion != "" { |
| 54 | // Runx packages have a "v" prefix (why?). Trim for consistency. |
| 55 | resolvedVersion = strings.TrimPrefix(resolvedVersion, "v") |
| 56 | msg = fmt.Sprintf("* %s - %s\n", pkg.Versioned(), resolvedVersion) |
| 57 | } else { |
| 58 | msg = fmt.Sprintf("* %s\n", pkg.Versioned()) |
| 59 | } |
| 60 | fmt.Fprint(cmd.OutOrStdout(), msg) |
| 61 | } |
| 62 | return nil |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | cmd.Flags().BoolVar(&flags.outdated, "outdated", false, "List outdated packages") |
| 67 | flags.config.register(cmd) |
| 68 | return cmd |
| 69 | } |
| 70 | |
| 71 | // printOutdatedPackages prints a list of outdated packages. |
| 72 | func printOutdatedPackages(cmd *cobra.Command, box *devbox.Devbox) error { |
no test coverage detected