forked from redhat-developer/odo
/
component.go
73 lines (61 loc) · 2.14 KB
/
component.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
package search
import (
"fmt"
"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/spf13/cobra"
)
const componentRecommendedCommandName = "component"
var componentExample = ` # Search for a component
%[1]s python`
// SearchComponentOptions encapsulates the options for the odo catalog describe service command
type SearchComponentOptions struct {
searchTerm string
components []string
// generic context options common to all commands
*genericclioptions.Context
}
// NewSearchComponentOptions creates a new SearchComponentOptions instance
func NewSearchComponentOptions() *SearchComponentOptions {
return &SearchComponentOptions{}
}
// Complete completes SearchComponentOptions after they've been created
func (o *SearchComponentOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd)
o.searchTerm = args[0]
o.components, err = catalog.Search(o.Client, o.searchTerm)
return err
}
// Validate validates the SearchComponentOptions based on completed values
func (o *SearchComponentOptions) Validate() (err error) {
if len(o.components) == 0 {
return fmt.Errorf("no component matched the query: %s", o.searchTerm)
}
return
}
// Run contains the logic for the command associated with SearchComponentOptions
func (o *SearchComponentOptions) Run() (err error) {
log.Infof("The following components were found:")
for _, component := range o.components {
fmt.Printf("- %v\n", component)
}
return
}
// NewCmdCatalogSearchComponent implements the odo catalog search component command
func NewCmdCatalogSearchComponent(name, fullName string) *cobra.Command {
o := NewSearchComponentOptions()
return &cobra.Command{
Use: name,
Short: "Search component type in catalog",
Long: `Search component type in catalog.
This searches for a partial match for the given search term in all the available
components.
`,
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(componentExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
}