forked from rhd-gitops-example/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
125 lines (104 loc) · 3.46 KB
/
list.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package url
import (
"encoding/json"
"fmt"
"os"
"text/tabwriter"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/url"
"github.com/pkg/errors"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
const listRecommendedCommandName = "list"
var (
urlListShortDesc = `List URLs`
urlListLongDesc = ktemplates.LongDesc(`Lists all the available URLs which can be used to access the components.`)
urlListExample = ktemplates.Examples(` # List the available URLs
%[1]s
`)
)
// URLListOptions encapsulates the options for the odo url list command
type URLListOptions struct {
localConfigInfo *config.LocalConfigInfo
componentContext string
*genericclioptions.Context
}
// NewURLListOptions creates a new UrlCreateOptions instance
func NewURLListOptions() *URLListOptions {
return &URLListOptions{}
}
// Complete completes UrlListOptions after they've been Listed
func (o *URLListOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd)
o.localConfigInfo, err = config.NewLocalConfigInfo(o.componentContext)
if err != nil {
return errors.Wrap(err, "failed intiating local config")
}
return
}
// Validate validates the UrlListOptions based on completed values
func (o *URLListOptions) Validate() (err error) {
return util.CheckOutputFlag(o.OutputFlag)
}
// Run contains the logic for the odo url list command
func (o *URLListOptions) Run() (err error) {
urls, err := url.List(o.Client, o.Component(), o.Application)
if err != nil {
return err
}
localUrls := o.localConfigInfo.GetUrl()
if o.OutputFlag == "json" {
out, err := json.Marshal(urls)
if err != nil {
return err
}
fmt.Println(string(out))
} else {
if len(urls.Items) == 0 && len(localUrls) == 0 {
return fmt.Errorf("no URLs found for component %v in application %v", o.Component(), o.Application)
} else {
log.Infof("Found the following URLs for component %v in application %v:", o.Component(), o.Application)
tabWriterURL := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
//create headers
fmt.Fprintln(tabWriterURL, "NAME", "\t", "URL", "\t", "PORT")
for _, i := range localUrls {
var present bool
for _, u := range urls.Items {
if i.Name == u.Name {
fmt.Fprintln(tabWriterURL, u.Name, "\t", url.GetURLString(u.Spec.Protocol, u.Spec.Host), "\t", u.Spec.Port)
present = true
}
}
if !present {
fmt.Fprintln(tabWriterURL, i.Name, "\t", "<not created on cluster>", "\t", i.Port)
}
}
tabWriterURL.Flush()
fmt.Println("\nUse `odo push` to create url on cluster")
}
}
return
}
// NewCmdURLList implements the odo url list command.
func NewCmdURLList(name, fullName string) *cobra.Command {
o := NewURLListOptions()
urlListCmd := &cobra.Command{
Use: name,
Short: urlListShortDesc,
Long: urlListLongDesc,
Example: fmt.Sprintf(urlListExample, fullName),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
genericclioptions.AddOutputFlag(urlListCmd)
genericclioptions.AddContextFlag(urlListCmd, &o.componentContext)
completion.RegisterCommandFlagHandler(urlListCmd, "context", completion.FileCompletionHandler)
return urlListCmd
}