/
runner.go
76 lines (60 loc) · 1.27 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
package organization
import (
"context"
"fmt"
"io"
"os"
"github.com/giantswarm/microerror"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
template "github.com/giantswarm/kubectl-gs/pkg/template/organization"
)
type runner struct {
flag *flag
stdout io.Writer
stderr io.Writer
}
func (r *runner) Run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
err := r.flag.Validate()
if err != nil {
return microerror.Mask(err)
}
err = r.run(ctx, cmd, args)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error {
var err error
config := template.Config{
Name: r.flag.Name,
}
var outputWriter io.Writer
{
if r.flag.Output == "" {
outputWriter = r.stdout
} else {
f, err := os.Create(r.flag.Output)
if err != nil {
return microerror.Mask(err)
}
defer f.Close()
outputWriter = f
}
}
organizationCR, err := template.NewOrganizationCR(config)
if err != nil {
return microerror.Mask(err)
}
organizationCRYaml, err := yaml.Marshal(organizationCR)
if err != nil {
return microerror.Mask(err)
}
_, err = fmt.Fprint(outputWriter, string(organizationCRYaml))
if err != nil {
return microerror.Mask(err)
}
return nil
}