github.com/hashicorp/packer@v1.14.3/command/plugins_installed.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package command
     5  
     6  import (
     7  	"context"
     8  	"crypto/sha256"
     9  	"log"
    10  	"runtime"
    11  	"strings"
    12  
    13  	plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
    14  )
    15  
    16  type PluginsInstalledCommand struct {
    17  	Meta
    18  }
    19  
    20  func (c *PluginsInstalledCommand) Synopsis() string {
    21  	return "List all installed Packer plugin binaries"
    22  }
    23  
    24  func (c *PluginsInstalledCommand) Help() string {
    25  	helpText := `
    26  Usage: packer plugins installed
    27  
    28    This command lists all installed plugin binaries that match with the current
    29    OS and architecture. Packer's API version will be ignored.
    30  
    31  `
    32  
    33  	return strings.TrimSpace(helpText)
    34  }
    35  
    36  func (c *PluginsInstalledCommand) Run(args []string) int {
    37  	ctx, cleanup := handleTermInterrupt(c.Ui)
    38  	defer cleanup()
    39  
    40  	return c.RunContext(ctx)
    41  }
    42  
    43  func (c *PluginsInstalledCommand) RunContext(buildCtx context.Context) int {
    44  
    45  	opts := plugingetter.ListInstallationsOptions{
    46  		PluginDirectory: c.Meta.CoreConfig.Components.PluginConfig.PluginDirectory,
    47  		BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{
    48  			OS:   runtime.GOOS,
    49  			ARCH: runtime.GOARCH,
    50  			Checksummers: []plugingetter.Checksummer{
    51  				{Type: "sha256", Hash: sha256.New()},
    52  			},
    53  		},
    54  	}
    55  
    56  	if runtime.GOOS == "windows" && opts.Ext == "" {
    57  		opts.BinaryInstallationOptions.Ext = ".exe"
    58  	}
    59  
    60  	log.Printf("[TRACE] init: %#v", opts)
    61  
    62  	// a plugin requirement that matches them all
    63  	allPlugins := plugingetter.Requirement{
    64  		Accessor:           "",
    65  		VersionConstraints: nil,
    66  		Identifier:         nil,
    67  	}
    68  
    69  	installations, err := allPlugins.ListInstallations(opts)
    70  	if err != nil {
    71  		c.Ui.Error(err.Error())
    72  		return 1
    73  	}
    74  
    75  	for _, installation := range installations {
    76  		c.Ui.Message(installation.BinaryPath)
    77  	}
    78  
    79  	return 0
    80  }