packer/command/plugins_installed.go
Adrien Delorme 9f4a1281dd
Add packer plugins command and subcommands to interact with plugins (#11553)
* add basic docs for plugins command

* refactor docs

Co-Authored-By: Wilken Rivera <1749304+nywilken@users.noreply.github.com>

* add plugins command

* add plugins subcommands

they do nothing for now

* add plugins installed command + tests

* add plugins install command

* add remove plugin command

* better docs for the plugins install command

* remove duplicate content

* better output for installed plugins

* add plugins required command

* Update plugins_install.go

* add newline after `Usage:`

* Update plugins_remove.go

* Update plugins_required.go

* Update plugins_remove.go

* Update plugins_installed.go

* Update plugins_install.go

* add docs

* Update plugins_install.go

* fix typos

* Update plugins_test.go

* fix typos

Co-Authored-By: Wilken Rivera <1749304+nywilken@users.noreply.github.com>

* Update core_wrapper.go

Co-Authored-By: Wilken Rivera <1749304+nywilken@users.noreply.github.com>

* Update website/content/docs/commands/plugins/remove.mdx

Co-authored-by: Wilken Rivera <wilken@hashicorp.com>

* Update website/content/docs/commands/plugins/required.mdx

Co-authored-by: Wilken Rivera <wilken@hashicorp.com>

* Update website/content/docs/commands/plugins/required.mdx

Co-authored-by: Wilken Rivera <wilken@hashicorp.com>

* Update plugins_required.go

* Update install.mdx

* Update required.mdx

* plugins requirement, warn when no plugin was found

* Update website/content/docs/commands/plugins/required.mdx

Co-authored-by: Wilken Rivera <wilken@hashicorp.com>

Co-authored-by: Wilken Rivera <1749304+nywilken@users.noreply.github.com>
Co-authored-by: Wilken Rivera <wilken@hashicorp.com>
2022-02-10 16:53:50 -05:00

78 lines
1.7 KiB
Go

package command
import (
"context"
"crypto/sha256"
"log"
"runtime"
"strings"
plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
)
type PluginsInstalledCommand struct {
Meta
}
func (c *PluginsInstalledCommand) Synopsis() string {
return "List all installed Packer plugin binaries"
}
func (c *PluginsInstalledCommand) Help() string {
helpText := `
Usage: packer plugins installed
This command lists all installed plugin binaries that match with the current
OS and architecture. Packer's API version will be ignored.
`
return strings.TrimSpace(helpText)
}
func (c *PluginsInstalledCommand) Run(args []string) int {
ctx, cleanup := handleTermInterrupt(c.Ui)
defer cleanup()
return c.RunContext(ctx)
}
func (c *PluginsInstalledCommand) RunContext(buildCtx context.Context) int {
opts := plugingetter.ListInstallationsOptions{
FromFolders: c.Meta.CoreConfig.Components.PluginConfig.KnownPluginFolders,
BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{
OS: runtime.GOOS,
ARCH: runtime.GOARCH,
Checksummers: []plugingetter.Checksummer{
{Type: "sha256", Hash: sha256.New()},
},
},
}
if runtime.GOOS == "windows" && opts.Ext == "" {
opts.BinaryInstallationOptions.Ext = ".exe"
}
log.Printf("[TRACE] init: %#v", opts)
// a plugin requirement that matches them all
allPlugins := plugingetter.Requirement{
Accessor: "",
VersionConstraints: nil,
Identifier: nil,
Implicit: false,
}
installations, err := allPlugins.ListInstallations(opts)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
for _, installation := range installations {
c.Ui.Message(installation.BinaryPath)
}
return 0
}