Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve install error when app doesnt exist #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
package cmd

import (
"errors"
"fmt"
"os"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/alexellis/arkade/cmd/apps"
"github.com/alexellis/arkade/pkg/get"
)

type ArkadeApp struct {
Expand Down Expand Up @@ -100,7 +103,7 @@ To request a new app, raise a GitHub issue at:
}
}
if app == nil {
return fmt.Errorf("no such app: %s, run \"arkade install --help\" for a list of apps", name)
return errors.New(checkForTool(name, get.MakeTools()))
}

return nil
Expand Down Expand Up @@ -181,3 +184,13 @@ func NewArkadeApp(cmd func() *cobra.Command, msg string) ArkadeApp {
InfoMessage: msg,
}
}

func checkForTool(appName string, tools []get.Tool) string {

for _, tool := range tools {
if strings.EqualFold(tool.Name, appName) {
return fmt.Sprintf("no such app. %s is available as a tool, run \"arkade get %s\" to get it", appName, appName)
}
}
return fmt.Sprintf("no such app: %s, run \"arkade install --help\" for a list of apps", appName)
}
91 changes: 91 additions & 0 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 (
"testing"

"github.com/alexellis/arkade/pkg/get"
)

type Tool = get.Tool

func TestCheckForTool(t *testing.T) {
tests := []struct {
name string
appName string
tools []Tool
expected string
expectFail bool
}{
{
name: "Tool exists but expected is wrong",
appName: "kubectl",
tools: []Tool{
{Name: "kubectl"},
{Name: "helm"},
{Name: "k9s"},
},
expected: "this test should fail",
expectFail: true,
},
{
name: "Tool exists",
appName: "kubectl",
tools: []Tool{
{Name: "kubectl"},
{Name: "helm"},
{Name: "k9s"},
},
expected: "no such app. kubectl is available as a tool, run \"arkade get kubectl\" to get it",
expectFail: false,
},
{
name: "Tool does not exist",
appName: "randomtool",
tools: []Tool{
{Name: "kubectl"},
{Name: "helm"},
},
expected: "no such app: randomtool, run \"arkade install --help\" for a list of apps",
expectFail: false,
},
{
name: "Case-insensitive match",
appName: "KUBECTL",
tools: []Tool{
{Name: "kubectl"},
{Name: "helm"},
},
expected: "no such app. KUBECTL is available as a tool, run \"arkade get KUBECTL\" to get it",
expectFail: false,
},
{
name: "Empty tool list",
appName: "kubectl",
tools: []Tool{},
expected: "no such app: kubectl, run \"arkade install --help\" for a list of apps",
expectFail: false,
},
{
name: "Empty app name",
appName: "",
tools: []Tool{
{Name: "kubectl"},
},
expected: "no such app: , run \"arkade install --help\" for a list of apps",
expectFail: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := checkForTool(tc.appName, tc.tools)
if result != tc.expected && !tc.expectFail {
t.Errorf("%s\nwant: %s\n got: %s",
tc.name, tc.expected, result)
}
})
}
}