forked from redhat-developer/odo
/
log.go
89 lines (70 loc) · 2.51 KB
/
log.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
package component
import (
"fmt"
"os"
"github.com/openshift/odo/pkg/odo/genericclioptions"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
"github.com/openshift/odo/pkg/odo/util/completion"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/component"
"github.com/spf13/cobra"
)
// LogRecommendedCommandName is the recommended watch command name
const LogRecommendedCommandName = "log"
var logExample = ktemplates.Examples(` # Get the logs for the nodejs component
%[1]s nodejs
`)
// LogOptions contains log options
type LogOptions struct {
logFollow bool
componentContext string
*ComponentOptions
}
// NewLogOptions returns new instance of LogOptions
func NewLogOptions() *LogOptions {
return &LogOptions{false, "", &ComponentOptions{}}
}
// Complete completes log args
func (lo *LogOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
err = lo.ComponentOptions.Complete(name, cmd, args)
return
}
// Validate validates the log parameters
func (lo *LogOptions) Validate() (err error) {
return
}
// Run has the logic to perform the required actions as part of command
func (lo *LogOptions) Run() (err error) {
stdout := os.Stdout
// Retrieve the log
err = component.GetLogs(lo.Context.Client, lo.componentName, lo.Context.Application, lo.logFollow, stdout)
return
}
// NewCmdLog implements the log odo command
func NewCmdLog(name, fullName string) *cobra.Command {
o := NewLogOptions()
var logCmd = &cobra.Command{
Use: fmt.Sprintf("%s [component_name]", name),
Short: "Retrieve the log for the given component",
Long: `Retrieve the log for the given component`,
Example: fmt.Sprintf(logExample, fullName),
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
logCmd.Flags().BoolVarP(&o.logFollow, "follow", "f", false, "Follow logs")
// Add a defined annotation in order to appear in the help menu
logCmd.Annotations = map[string]string{"command": "component"}
logCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(logCmd, completion.ComponentNameCompletionHandler)
// Adding `--context` flag
genericclioptions.AddContextFlag(logCmd, &o.componentContext)
//Adding `--project` flag
projectCmd.AddProjectFlag(logCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(logCmd)
return logCmd
}