forked from redhat-developer/odo
/
create.go
106 lines (91 loc) · 3.51 KB
/
create.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
package storage
import (
"fmt"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/storage"
"github.com/openshift/odo/pkg/util"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
const createRecommendedCommandName = "create"
var (
storageCreateShortDesc = `Create storage and mount to a component`
storageCreateLongDesc = ktemplates.LongDesc(`Create storage and mount to a component`)
storageCreateExample = ktemplates.Examples(`
# Create storage of size 1Gb to a component
%[1]s mystorage --path=/opt/app-root/src/storage/ --size=1Gi
`)
)
type StorageCreateOptions struct {
storageName string
storageSize string
storagePath string
componentContext string
localConfig *config.LocalConfigInfo
*genericclioptions.Context
}
// NewStorageCreateOptions creates a new StorageCreateOptions instance
func NewStorageCreateOptions() *StorageCreateOptions {
return &StorageCreateOptions{}
}
// Complete completes StorageCreateOptions after they've been created
func (o *StorageCreateOptions) 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
}
o.Context = genericclioptions.NewContext(cmd)
if len(args) != 0 {
o.storageName = args[0]
} else {
o.storageName = o.Component() + "-" + util.GenerateRandomString(4)
}
return
}
// Validate validates the StorageCreateOptions based on completed values
func (o *StorageCreateOptions) Validate() (err error) {
// validate storage path
return o.localConfig.ValidateStorage(o.storageName, o.storagePath)
}
// Run contains the logic for the odo storage create command
func (o *StorageCreateOptions) Run() (err error) {
storageResult, err := o.localConfig.StorageCreate(o.storageName, o.storageSize, o.storagePath)
if err != nil {
return err
}
storageResultMachineReadable := storage.GetMachineReadableFormat(storageResult.Name, storageResult.Size, storageResult.Path)
if log.IsJSON() {
machineoutput.OutputSuccess(storageResultMachineReadable)
} else {
log.Successf("Added storage %v to %v", o.storageName, o.localConfig.GetName())
log.Italic("\nPlease use `odo push` command to make the storage accessible to the component")
}
return
}
// NewCmdStorageCreate implements the odo storage create command.
func NewCmdStorageCreate(name, fullName string) *cobra.Command {
o := NewStorageCreateOptions()
storageCreateCmd := &cobra.Command{
Use: name,
Short: storageCreateShortDesc,
Long: storageCreateLongDesc,
Example: fmt.Sprintf(storageCreateExample, fullName),
Args: cobra.MaximumNArgs(1),
Annotations: map[string]string{"machineoutput": "json"},
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
storageCreateCmd.Flags().StringVar(&o.storageSize, "size", "", "Size of storage to add")
storageCreateCmd.Flags().StringVar(&o.storagePath, "path", "", "Path to mount the storage on")
_ = storageCreateCmd.MarkFlagRequired("path")
_ = storageCreateCmd.MarkFlagRequired("size")
genericclioptions.AddContextFlag(storageCreateCmd, &o.componentContext)
completion.RegisterCommandFlagHandler(storageCreateCmd, "context", completion.FileCompletionHandler)
return storageCreateCmd
}