Skip to content

Commit a9a7757

Browse files
committed
Allow a path override for faas-cli plugin get
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 2aec1d6 commit a9a7757

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

commands/plugin_get.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import (
1616
"github.com/spf13/cobra"
1717
)
1818

19-
var pluginRegistry string
20-
var clientOS string
21-
var clientArch string
22-
var tag string
19+
var (
20+
pluginRegistry string
21+
clientOS string
22+
clientArch string
23+
tag string
24+
pluginPath string
25+
)
2326

2427
func init() {
2528
pluginGetCmd := &cobra.Command{
@@ -45,7 +48,9 @@ faas-cli plugin get NAME --registry ghcr.io/openfaasltd`,
4548
pluginGetCmd.Flags().StringVar(&clientArch, "arch", "", "The architecture to pull the plugin for, give a value or leave blank for auto-detection")
4649
pluginGetCmd.Flags().StringVar(&clientOS, "os", "", "The OS to pull the plugin for, give a value or leave blank for auto-detection")
4750
pluginGetCmd.Flags().StringVar(&tag, "version", "latest", "Version or SHA for plugin")
48-
pluginGetCmd.Flags().BoolVar(&verbose, "verbose", false, "Verbose output")
51+
pluginGetCmd.Flags().StringVar(&pluginPath, "path", "$HOME/.openfaas/plugins", "The path for the plugin")
52+
53+
pluginGetCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
4954

5055
pluginCmd.AddCommand(pluginGetCmd)
5156
}
@@ -81,10 +86,17 @@ func runPluginGetCmd(cmd *cobra.Command, args []string) error {
8186
}
8287

8388
var pluginDir string
84-
if runtime.GOOS == "windows" {
85-
pluginDir = os.Expand("$HOMEPATH/.openfaas/plugins", os.Getenv)
89+
90+
if cmd.Flags().Changed("path") {
91+
pluginPath = strings.ReplaceAll(pluginPath, "$HOME", os.Getenv("HOME"))
92+
93+
pluginDir = pluginPath
8694
} else {
87-
pluginDir = os.ExpandEnv("$HOME/.openfaas/plugins")
95+
if runtime.GOOS == "windows" {
96+
pluginDir = os.Expand("$HOMEPATH/.openfaas/plugins", os.Getenv)
97+
} else {
98+
pluginDir = os.ExpandEnv("$HOME/.openfaas/plugins")
99+
}
88100
}
89101

90102
if _, err := os.Stat(pluginDir); os.IsNotExist(err) {
@@ -121,6 +133,7 @@ func runPluginGetCmd(cmd *cobra.Command, args []string) error {
121133
if err != nil {
122134
return fmt.Errorf("failed to open %s: %w", tmpTar, err)
123135
}
136+
124137
defer tarFile.Close()
125138

126139
if verbose {
@@ -143,7 +156,11 @@ func runPluginGetCmd(cmd *cobra.Command, args []string) error {
143156
}
144157
}
145158

146-
fmt.Printf("Downloaded in (%ds)\n\nUsage:\n faas-cli %s\n", int(time.Since(st).Seconds()), pluginName)
159+
if cmd.Flags().Changed("path") {
160+
fmt.Printf("Wrote: %s (%s/%s) in (%s)\n", path.Join(pluginPath, pluginName), clientOS, clientArch, time.Since(st).Round(time.Millisecond))
161+
} else {
162+
fmt.Printf("Downloaded in (%s)\n\nUsage:\n faas-cli %s\n", time.Since(st).Round(time.Millisecond), pluginName)
163+
}
147164
return nil
148165
}
149166

0 commit comments

Comments
 (0)