forked from mamezou-tech/concourse-k8s-resource
/
runner.go
78 lines (69 loc) · 1.71 KB
/
runner.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
package kubectl
import (
"github.com/brettbourgeois/concourse-k8s-resource/pkg/models"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"log"
"sync"
)
type CommandConfig struct {
Clientset kubernetes.Interface
Discovery discovery.DiscoveryInterface
ClientConfig clientcmd.ClientConfig
Streams genericclioptions.IOStreams
Namespace string
Resources []models.WatchResource
Params *models.OutParams
}
type Command struct {
*cobra.Command
args []string
}
type CommandFactory interface {
create(config *CommandConfig) (commands []*Command, err error)
}
func RunCommand(factory CommandFactory, config *CommandConfig) error {
var wg sync.WaitGroup
cmds, err := factory.create(config)
if err != nil {
return err
}
wg.Add(len(cmds))
for _, cmd := range cmds {
printFlagsAndArgs(cmd)
go run(*cmd, &wg)
}
wg.Wait()
return nil
}
func run(cmd Command, wg *sync.WaitGroup) {
defer wg.Done()
log.Printf("running %s command... %v", cmd.Name(), cmd.args)
cmd.Run(cmd.Command, cmd.args)
}
func NewCommandFactory(params *models.OutParams) CommandFactory {
switch {
case params.Undo:
return &undoCommandFactory{}
case params.Delete:
return &deleteCommandFactory{}
case params.Diff:
return &diffCommandFactory{}
default:
return &applyCommandFactory{}
}
}
func printFlagsAndArgs(command *Command) {
log.Println("** kubectl flags **")
command.Flags().VisitAll(func(flag *pflag.Flag) {
log.Printf("- %s -> %s\n", flag.Name, flag.Value.String())
})
log.Println("** kubectl args **")
for _, arg := range command.args {
log.Printf("- %s\n", arg)
}
}