pulumi/cmd/plugin_ls.go
joeduffy c04341edb2 Consult the program for its list of plugins
This change adds a GetRequiredPlugins RPC method to the language
host, enabling us to query it for its list of plugin requirements.
This is language-specific because it requires looking at the set
of dependencies (e.g., package.json files).

It also adds a call up front during any update/preview operation
to compute the set of plugins and require that they are present.
These plugins are populated in the cache and will be used for all
subsequent plugin-related operations during the engine's activity.

We now cache the language plugins, so that we may load them
eagerly too, which we never did previously due to the fact that
we needed to pass the monitor address at load time.  This was a
bit bizarre anyhow, since it's really the Run RPC function that
needs this information.  So, to enable caching and eager loading
-- which we need in order to invoke GetRequiredPlugins -- the
"phone home" monitor RPC address is passed at Run time.

In a subsequent change, we will switch to faulting in the plugins
that are missing -- rather than erroring -- in addition to
supporting the `pulumi plugin install` CLI command.
2018-02-18 08:08:15 -08:00

81 lines
2.1 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"sort"
"strconv"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
func newPluginLsCmd() *cobra.Command {
var projectOnly bool
cmd := &cobra.Command{
Use: "ls",
Short: "List plugins",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Produce a list of plugins, sorted by name and version.
plugins, err := workspace.GetPlugins()
if err != nil {
return errors.Wrapf(err, "loading plugins")
}
// Devote 26 characters to the name width, unless there is a longer name.
maxname := 26
for _, plugin := range plugins {
if len(plugin.Name) > maxname {
maxname = len(plugin.Name)
}
}
// Sort the plugins: by name first alphabetical ascending and version descending, so that plugins
// with the same name/kind sort by newest to oldest.
sort.Slice(plugins, func(i, j int) bool {
pi, pj := plugins[i], plugins[j]
if pi.Name < pj.Name {
return true
} else if pi.Name == pj.Name && pi.Kind == pj.Kind &&
(pi.Version == nil || (pj.Version != nil && pi.Version.GT(*pj.Version))) {
return true
}
return false
})
// And now pretty-print the list.
var totalSize uint64
fmt.Printf("%-"+strconv.Itoa(maxname)+"s %-12s %-26s %-18s %-18s %-18s\n",
"NAME", "KIND", "VERSION", "SIZE", "INSTALLED", "LAST USED")
for _, plugin := range plugins {
fmt.Printf("%-"+strconv.Itoa(maxname)+"s %-12s %-26s %-18s %-18s %-18s\n",
plugin.Name,
plugin.Kind,
plugin.Version.String(),
humanize.Bytes(uint64(plugin.Size)),
humanize.Time(plugin.InstallTime),
humanize.Time(plugin.LastUsedTime),
)
totalSize += uint64(plugin.Size)
}
fmt.Printf("\n")
fmt.Printf("TOTAL plugin cache size: %s\n", humanize.Bytes(totalSize))
return nil
}),
}
cmd.PersistentFlags().BoolVarP(
&projectOnly, "project", "p", false,
"List only the plugins used by the current project")
return cmd
}