forked from redhat-developer/odo
/
push.go
136 lines (111 loc) · 4.83 KB
/
push.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
126
127
128
129
130
131
132
133
134
135
136
package component
import (
"fmt"
"github.com/openshift/odo/pkg/component"
"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/completion"
"github.com/pkg/errors"
"github.com/spf13/cobra"
odoutil "github.com/openshift/odo/pkg/odo/util"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
var pushCmdExample = ktemplates.Examples(` # Push source code to the current component
%[1]s
# Push data to the current component from the original source.
%[1]s
# Push source code in ~/mycode to component called my-component
%[1]s my-component --context ~/mycode
`)
// PushRecommendedCommandName is the recommended push command name
const PushRecommendedCommandName = "push"
// PushOptions encapsulates options that push command uses
type PushOptions struct {
*CommonPushOptions
}
// NewPushOptions returns new instance of PushOptions
// with "default" values for certain values, for example, show is "false"
func NewPushOptions() *PushOptions {
return &PushOptions{
CommonPushOptions: NewCommonPushOptions(),
}
}
// Complete completes push args
func (po *PushOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
conf, err := config.NewLocalConfigInfo(po.componentContext)
if err != nil {
return errors.Wrap(err, "unable to retrieve configuration information")
}
// Set the necessary values within WatchOptions
po.localConfigInfo = conf
err = po.SetSourceInfo()
if err != nil {
return errors.Wrap(err, "unable to set source information")
}
// Apply ignore information
err = genericclioptions.ApplyIgnore(&po.ignores, po.sourcePath)
if err != nil {
return errors.Wrap(err, "unable to apply ignore information")
}
// Set the correct context
po.Context = genericclioptions.NewContextCreatingAppIfNeeded(cmd)
prjName := po.localConfigInfo.GetProject()
po.ResolveSrcAndConfigFlags()
err = po.ResolveProject(prjName)
if err != nil {
return err
}
return
}
// Validate validates the push parameters
func (po *PushOptions) Validate() (err error) {
log.Info("Validation")
// First off, we check to see if the component exists. This is ran each time we do `odo push`
s := log.Spinner("Checking component")
defer s.End(false)
po.isCmpExists, err = component.Exists(po.Context.Client, po.localConfigInfo.GetName(), po.localConfigInfo.GetApplication())
if err != nil {
return errors.Wrapf(err, "failed to check if component of name %s exists in application %s", po.localConfigInfo.GetName(), po.localConfigInfo.GetApplication())
}
if err = component.ValidateComponentCreateRequest(po.Context.Client, po.localConfigInfo.GetComponentSettings(), po.componentContext); err != nil {
s.End(false)
log.Info("Run 'odo catalog list components' for a list of supported component types")
return fmt.Errorf("Invalid component type %s, %v", *po.localConfigInfo.GetComponentSettings().Type, errors.Cause(err))
}
if !po.isCmpExists && po.pushSource && !po.pushConfig {
return fmt.Errorf("Component %s does not exist and hence cannot push only source. Please use `odo push` without any flags or with both `--source` and `--config` flags", po.localConfigInfo.GetName())
}
s.End(true)
return nil
}
// Run has the logic to perform the required actions as part of command
func (po *PushOptions) Run() (err error) {
return po.Push()
}
// NewCmdPush implements the push odo command
func NewCmdPush(name, fullName string) *cobra.Command {
po := NewPushOptions()
var pushCmd = &cobra.Command{
Use: fmt.Sprintf("%s [component name]", name),
Short: "Push source code to a component",
Long: `Push source code to a component.`,
Example: fmt.Sprintf(pushCmdExample, fullName),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(po, cmd, args)
},
}
genericclioptions.AddContextFlag(pushCmd, &po.componentContext)
pushCmd.Flags().BoolVar(&po.show, "show-log", false, "If enabled, logs will be shown when built")
pushCmd.Flags().StringSliceVar(&po.ignores, "ignore", []string{}, "Files or folders to be ignored via glob expressions.")
pushCmd.Flags().BoolVar(&po.pushConfig, "config", false, "Use config flag to only apply config on to cluster")
pushCmd.Flags().BoolVar(&po.pushSource, "source", false, "Use source flag to only push latest source on to cluster")
pushCmd.Flags().BoolVarP(&po.forceBuild, "force-build", "f", false, "Use force-build flag to force building the component")
// Add a defined annotation in order to appear in the help menu
pushCmd.Annotations = map[string]string{"command": "component"}
pushCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(pushCmd, completion.ComponentNameCompletionHandler)
completion.RegisterCommandFlagHandler(pushCmd, "context", completion.FileCompletionHandler)
return pushCmd
}