-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
98 lines (81 loc) · 3.29 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
package cli
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
doaLong = `
The OpenShift Image Checker is a CLI tool for finding and highlighting potential issues a Containerfile could have on an OpenShift cluster.
Find out more at https://github.com/redhat-developer/podman-desktop-image-checker-ext `
doaExample = `
# Analyze the Containerfile of a project:
doa analyze /your/local/project/path[/Containerfile_name]
`
rootHelpMessage = "To see a full list of commands, run 'doa --help'"
rootDefaultHelp = fmt.Sprintf("%s\n\nExamples:\n%s\n\n%s", doaLong, doaExample, rootHelpMessage)
)
func DockerOpenShiftAnalyzerCommands() *cobra.Command {
rootCmd := &cobra.Command{
Use: "doa",
Short: "doa",
Long: doaLong,
RunE: ShowHelp,
Example: doaExample,
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
// Create a custom help function that will exit when we enter an invalid command, for example:
// doa foobar --help
// which will exit with an error message: "unknown command 'foobar', type --help for a list of all commands"
helpCmd := rootCmd.HelpFunc()
rootCmd.SetHelpFunc(func(command *cobra.Command, args []string) {
// Simple way of checking to see if the command has a parent (if it doesn't, it does not exist)
if !command.HasParent() && len(args) > 0 {
RedirectErrorStringToStdErrAndExit(fmt.Sprintf("unknown command '%s', type --help for a list of all commands\n", args[0]))
}
helpCmd(command, args)
})
rootCmdList := append([]*cobra.Command{},
NewCmdAnalyze(),
)
rootCmd.AddCommand(rootCmdList...)
return rootCmd
}
func RedirectErrorStringToStdErrAndExit(err string) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// ShowHelp will show the help correctly (and whether or not the command is invalid...)
// Taken from: https://github.com/redhat-developer/odo/blob/f55a4f0a7af4cd5f7c4e56dd70a66d38be0643cf/pkg/odo/cli/cli.go#L272
func ShowHelp(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
// We will show a custom help when typing JUST `doa`, directing the user to use `doa --help` for a full help.
// Thus we will set cmd.SilenceUsage and cmd.SilenceErrors both to true so we do not output the usage or error out.
cmd.SilenceUsage = true
cmd.SilenceErrors = true
// Print out the default "help" usage
fmt.Println(rootDefaultHelp)
return nil
}
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
return errors.New("invalid command - see available commands/subcommands above")
//revive:enable:error-strings
}