forked from mamezou-tech/concourse-k8s-resource
/
diff_command_factory.go
45 lines (38 loc) · 1.34 KB
/
diff_command_factory.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
package kubectl
import (
"github.com/emicklei/go-restful/log"
"github.com/spf13/cobra"
"k8s.io/client-go/util/exec"
"k8s.io/kubectl/pkg/cmd/apply"
"k8s.io/kubectl/pkg/cmd/diff"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
type diffCommandFactory struct{}
var _ CommandFactory = &diffCommandFactory{}
func (*diffCommandFactory) create(config *CommandConfig) (commands []*Command, err error) {
factory := createKubectlFactory(config)
options := diff.NewDiffOptions(config.Streams)
command := &cobra.Command{
Use: "diff",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckDiffErr(options.Complete(factory, cmd))
if err := options.Run(); err != nil {
// exit code == 1 -> there is difference(not error!)
if ee, ok := err.(exec.ExitError); ok && ee.ExitStatus() == 1 {
log.Printf("found difference!")
return
}
// exit with error code(>2)
log.Printf("ERR! %+v", err)
cmdutil.CheckDiffErr(err)
}
},
}
cmdutil.AddFilenameOptionFlags(command, &options.FilenameOptions, "contains the configuration to diff")
cmdutil.AddServerSideApplyFlags(command)
cmdutil.AddFieldManagerFlagVar(command, &options.FieldManager, apply.FieldManagerClientSideApply)
setManifestPath(command, config.Params)
commands = append(commands, &Command{command, []string{}})
return
}