1
0
mirror of https://github.com/alexellis/arkade.git synced 2022-05-07 18:22:49 +03:00
Files
arkade-kubernetes/cmd/get.go
Alex Ellis (OpenFaaS Ltd) be5a8cc594 Update thanks message to support message
Adds a message to downloads and K8s apps to show how to
support arkade.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
2022-04-11 09:30:13 +01:00

185 lines
4.7 KiB
Go

// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package cmd
import (
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"syscall"
units "github.com/docker/go-units"
"github.com/morikuni/aec"
"github.com/spf13/cobra"
"github.com/alexellis/arkade/pkg"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
)
// MakeGet creates the Get command to download software
func MakeGet() *cobra.Command {
tools := get.MakeTools()
sort.Sort(tools)
var validToolOptions []string = make([]string, len(tools))
for _, t := range tools {
validToolOptions = append(validToolOptions, t.Name)
}
var command = &cobra.Command{
Use: "get",
Short: `The get command downloads a tool`,
Long: `The get command downloads a CLI or application from the specific tool's
releases or downloads page. The tool is usually downloaded in binary format
and provides a fast and easy alternative to a package manager.`,
Example: ` arkade get helm
# Options for the download
arkade get linkerd2 --stash=false
arkade get kubectl --progress=false
# Override the version
arkade get kubectl@v1.19.3
arkade get terraform --version=0.12.0
# Override the OS
arkade get helm --os darwin --arch aarch64
arkade get helm --os linux --arch armv7l
# Get a complete list of CLIs to download:
arkade get`,
SilenceUsage: true,
Aliases: []string{"g", "d", "download"},
ValidArgs: validToolOptions,
}
clientArch, clientOS := env.GetClientArch()
command.Flags().Bool("progress", true, "Display a progress bar")
command.Flags().StringP("output", "o", "", "Output format of the list of tools (table/markdown/list)")
command.Flags().Bool("stash", true, "When set to true, stash binary in HOME/.arkade/bin/, otherwise store in /tmp/")
command.Flags().StringP("version", "v", "", "Download a specific version")
command.Flags().String("arch", clientArch, "CPU architecture for the tool")
command.Flags().String("os", clientOS, "Operating system for the tool")
command.Flags().Bool("quiet", false, "Suppress most additional output")
command.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
output, _ := command.Flags().GetString("output")
if len(output) > 0 {
if get.TableFormat(output) == get.MarkdownStyle {
get.CreateToolsTable(tools, get.MarkdownStyle)
} else if get.TableFormat(output) == get.ListStyle {
for _, r := range tools {
fmt.Printf("%s\n", r.Name)
}
} else {
get.CreateToolsTable(tools, get.TableStyle)
}
} else {
get.CreateToolsTable(tools, get.TableStyle)
}
return nil
}
version := ""
if command.Flags().Changed("version") {
version, _ = command.Flags().GetString("version")
}
downloadURLs, err := get.GetDownloadURLs(tools, args, version)
if err != nil {
return err
}
stash, _ := command.Flags().GetBool("stash")
progress, _ := command.Flags().GetBool("progress")
quiet, _ := command.Flags().GetBool("quiet")
if quiet && !command.Flags().Changed("progress") {
progress = false
}
if p, ok := os.LookupEnv("ARKADE_PROGRESS"); ok {
b, err := strconv.ParseBool(p)
if err != nil {
return fmt.Errorf("ARKADE_PROGRESS is not a valid boolean")
}
progress = b
}
dlMode := get.DownloadTempDir
if stash {
dlMode = get.DownloadArkadeDir
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
os.Exit(2)
}()
var outFilePath string
var localToolsStore []get.ToolLocal
arch, _ := command.Flags().GetString("arch")
if err := get.ValidateArch(arch); err != nil {
return err
}
operatingSystem, _ := command.Flags().GetString("os")
if err := get.ValidateOS(operatingSystem); err != nil {
return err
}
for _, tool := range downloadURLs {
if !quiet {
fmt.Printf("Downloading: %s\n", tool.Name)
}
outFilePath, _, err = get.Download(&tool,
arch,
operatingSystem,
version,
dlMode,
progress,
quiet)
if err != nil {
return err
}
localToolsStore = append(localToolsStore, get.ToolLocal{Name: tool.Name, Path: outFilePath})
if !quiet {
size := ""
stat, err := os.Stat(outFilePath)
if err == nil {
size = "(" + units.HumanSize(float64(stat.Size())) + ")"
}
fmt.Printf("\nWrote: %s %s\n\n", outFilePath, size)
}
}
nl := ""
if !quiet {
nl = "\n"
msg, err := get.PostInstallationMsg(dlMode, localToolsStore)
if err != nil {
return err
}
fmt.Printf("%s\n", msg)
}
fmt.Printf("%s%s\n", nl, aec.Bold.Apply(pkg.SupportMessageShort))
return err
}
return command
}