-
Notifications
You must be signed in to change notification settings - Fork 51
/
condition.go
113 lines (97 loc) · 3.01 KB
/
condition.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
package condition
import (
"fmt"
"io"
"os"
"strings"
"github.com/jenkins-x-plugins/jx-gitops/pkg/filters"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
cmdLong = templates.LongDesc(`
Runs a command if the condition is true
`)
cmdExample = templates.Examples(`
# runs a command if the last commit messsage has a given prefix
%s condition --last-commit-msg-prefix 'Merge pull request' -- make all commit push
you can use ! in front of a filter to be the equivalant of not matching the condition. e.g.
# runs a command if the last commit message does not have a given prefix
%s condition --last-commit-msg-prefix '!Merge pull request' -- make all commit push
`)
pathSeparator = string(os.PathSeparator)
)
// KptOptions the options for the command
type Options struct {
Dir string
Args []string
LastCommitMessageFilter filters.StringFilter
BatchMode bool
CommandRunner cmdrunner.CommandRunner
Out io.Writer
Err io.Writer
}
// NewCmdCondition creates a command object for the command
func NewCmdCondition() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "condition [flags] command arguments...",
Short: "Runs a command if the condition is true",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Dir, "dir", "d", "", "the directory to run the git push command from")
o.LastCommitMessageFilter.AddFlags(cmd, "last-commit-msg", "last commit message")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
if len(o.Args) == 0 {
return errors.Errorf("no command or command arguments specified")
}
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.DefaultCommandRunner
}
c := &cmdrunner.Command{
Dir: o.Dir,
Name: "git",
Args: []string{"log", "-1", "--pretty=%B"},
}
lastCommitMessage, err := o.CommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run %s", c.CLI())
}
lastCommitMessage = strings.TrimSpace(lastCommitMessage)
log.Logger().Infof("found last commit message: %s", termcolor.ColorStatus(lastCommitMessage))
if o.LastCommitMessageFilter.Matches(lastCommitMessage) {
if o.Out == nil {
o.Out = os.Stdout
}
if o.Err == nil {
o.Err = os.Stderr
}
c = &cmdrunner.Command{
Dir: o.Dir,
Name: o.Args[0],
Args: o.Args[1:],
Out: o.Out,
Err: o.Err,
}
_, err = o.CommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run %s", c.CLI())
}
}
return nil
}