forked from rhd-gitops-example/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
116 lines (96 loc) · 3.69 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
package storage
import (
"fmt"
"os"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/storage"
"text/tabwriter"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/genericclioptions"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"github.com/spf13/cobra"
)
const listRecommendedCommandName = "list"
var (
storageListShortDesc = `List storage attached to a component`
storageListLongDesc = ktemplates.LongDesc(`List storage attached to a component`)
storageListExample = ktemplates.Examples(` # List all storage attached or mounted to the current component and
# all unattached or unmounted storage in the current application
%[1]s
`)
)
type StorageListOptions struct {
componentContext string
localConfig *config.LocalConfigInfo
*genericclioptions.Context
}
// NewStorageListOptions creates a new StorageListOptions instance
func NewStorageListOptions() *StorageListOptions {
return &StorageListOptions{}
}
// Complete completes StorageListOptions after they've been created
func (o *StorageListOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd)
o.localConfig, err = config.NewLocalConfigInfo(o.componentContext)
if err != nil {
return err
}
return
}
// Validate validates the StorageListOptions based on completed values
func (o *StorageListOptions) Validate() (err error) {
return nil
}
// Run contains the logic for the odo storage list command
func (o *StorageListOptions) Run() (err error) {
storageListConfig, err := o.localConfig.StorageList()
if err != nil {
return err
}
storageListMachineReadable := []storage.Storage{}
for _, storageConfig := range storageListConfig {
storageListMachineReadable = append(storageListMachineReadable, storage.GetMachineReadableFormat(storageConfig.Name, storageConfig.Size, storageConfig.Path))
}
storageListResultMachineReadable := storage.GetMachineReadableFormatForList(storageListMachineReadable)
if log.IsJSON() {
machineoutput.OutputSuccess(storageListResultMachineReadable)
} else {
// defining the column structure of the table
tabWriterMounted := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent)
// create headers of mounted storage table
fmt.Fprintln(tabWriterMounted, "NAME", "\t", "SIZE", "\t", "PATH")
// iterating over all mounted storage and put in the mount storage table
if len(storageListConfig) > 0 {
for _, mStorage := range storageListConfig {
fmt.Fprintln(tabWriterMounted, mStorage.Name, "\t", mStorage.Size, "\t", mStorage.Path)
}
// print all mounted storage of the given component
log.Infof("The component '%v' has the following storage attached:", o.localConfig.GetName())
tabWriterMounted.Flush()
} else {
log.Infof("The component '%v' has no storage attached", o.localConfig.GetName())
}
fmt.Println("")
}
return
}
// NewCmdStorageList implements the odo storage list command.
func NewCmdStorageList(name, fullName string) *cobra.Command {
o := NewStorageListOptions()
storageListCmd := &cobra.Command{
Use: name,
Short: storageListShortDesc,
Long: storageListLongDesc,
Example: fmt.Sprintf(storageListExample, fullName),
Args: cobra.MaximumNArgs(1),
Annotations: map[string]string{"machineoutput": "json"},
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
genericclioptions.AddContextFlag(storageListCmd, &o.componentContext)
completion.RegisterCommandFlagHandler(storageListCmd, "context", completion.FileCompletionHandler)
return storageListCmd
}