Skip to content

Commit 2a46b55

Browse files
feat: add cli argument to render a cluster in non interactive mode
1 parent e0ba330 commit 2a46b55

File tree

5 files changed

+233
-153
lines changed

5 files changed

+233
-153
lines changed

cmd/main.go

Lines changed: 2 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,15 @@
11
package main
22

33
import (
4-
"context"
5-
"errors"
64
"fmt"
7-
"io"
85
"os"
96

10-
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/menu"
11-
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/project"
12-
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/template"
7+
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/cmd"
138
)
149

15-
var (
16-
projectConfig = &project.ProjectConfig{}
17-
)
18-
19-
const (
20-
PROJECTFILENAME = "PROJECT.yaml"
21-
)
22-
23-
// check for project file and load it
24-
func init() {
25-
_, err := os.Stat(PROJECTFILENAME)
26-
if err != nil && !errors.Is(err, os.ErrNotExist) {
27-
fmt.Println("An error occurred while checking for the PROJECT.yaml file", err)
28-
os.Exit(1)
29-
return
30-
}
31-
if errors.Is(err, os.ErrNotExist) {
32-
f, err := os.Create(PROJECTFILENAME)
33-
if err != nil {
34-
fmt.Println(err)
35-
os.Exit(1)
36-
return
37-
}
38-
defer f.Close()
39-
_, err = f.WriteString("basePath: overlays/\ntemplateBasePath: templates/\n")
40-
if err != nil {
41-
fmt.Println(err)
42-
os.Exit(1)
43-
return
44-
}
45-
}
46-
47-
pc, err := project.ParseConfig(PROJECTFILENAME)
48-
if err != nil {
49-
fmt.Println("An error occurred while parsing the PROJECT.yaml file", err)
50-
os.Exit(1)
51-
return
52-
}
53-
projectConfig = pc
54-
}
55-
5610
func main() {
57-
eventsPipeline := make(chan menu.Event, 100)
58-
ctx, cf := context.WithCancel(context.Background())
59-
defer cf()
60-
61-
go func(ctx context.Context) {
62-
// handle config file updates
63-
for {
64-
select {
65-
case <-ctx.Done():
66-
close(eventsPipeline)
67-
return
68-
case event := <-eventsPipeline:
69-
// we only need to update the config file if the action is a post action
70-
// because we need to update the config only, if the action was successful
71-
if event.Runtime == menu.EventRuntimePost {
72-
// update config file
73-
err := project.UpdateOrCreateConfig(PROJECTFILENAME, projectConfig)
74-
if err != nil {
75-
fmt.Println("An error occurred while updating the project config", err)
76-
return
77-
}
78-
}
79-
80-
if event.Origin == menu.EventOriginAddon {
81-
if event.Runtime == menu.EventRuntimePre {
82-
addonPath := projectConfig.Addons[event.Environment].Path
83-
_, err := template.LoadManifest(projectConfig.Addons[event.Environment].Path)
84-
if err != nil {
85-
fmt.Printf("An error occurred while loading the addon [%s] manifest file: %s, %v\n", event.Environment, addonPath, err)
86-
os.Exit(1)
87-
return
88-
}
89-
}
90-
continue
91-
}
92-
93-
if event.Environment != "" && event.Stage == "" && event.Cluster == "" {
94-
env := projectConfig.GetEnvironment(event.Environment)
95-
err := executeHook(os.Stdout, os.Stderr, event.Type, event.Runtime, env.Actions)
96-
if err != nil {
97-
fmt.Println(err)
98-
return
99-
}
100-
}
101-
102-
if event.Environment != "" && event.Stage != "" && event.Cluster == "" {
103-
stage := projectConfig.GetStage(event.Environment, event.Stage)
104-
err := executeHook(os.Stdout, os.Stderr, event.Type, event.Runtime, stage.Actions)
105-
if err != nil {
106-
fmt.Println(err)
107-
return
108-
}
109-
}
110-
111-
if event.Environment != "" && event.Stage != "" && event.Cluster != "" {
112-
cluster := projectConfig.GetCluster(event.Environment, event.Stage, event.Cluster)
113-
if event.Type == menu.EventTypeCreate || event.Type == menu.EventTypeUpdate {
114-
err := cluster.Render(projectConfig, event.Environment, event.Stage)
115-
if err != nil {
116-
fmt.Printf("An error occurred while rendering the cluster [%s] configuration: %v", event.Cluster, err)
117-
return
118-
}
119-
}
120-
}
121-
}
122-
}
123-
}(ctx)
124-
125-
err := menu.RootMenu(projectConfig, eventsPipeline)
126-
if err != nil {
11+
if err := cmd.RootCmd.Execute(); err != nil {
12712
fmt.Println(err)
12813
os.Exit(1)
12914
}
13015
}
131-
132-
func executeHook(stdout, errout io.Writer, t menu.EventType, r menu.EventRuntime, actions project.Actions) error {
133-
switch t {
134-
case menu.EventTypeCreate:
135-
if r == menu.EventRuntimePre {
136-
err := actions.ExecutePreCreateHooks(stdout, errout)
137-
if err != nil {
138-
return err
139-
}
140-
}
141-
if r == menu.EventRuntimePost {
142-
err := actions.ExecutePostCreateHooks(stdout, errout)
143-
if err != nil {
144-
return err
145-
}
146-
}
147-
return nil
148-
case menu.EventTypeUpdate:
149-
if r == menu.EventRuntimePre {
150-
err := actions.ExecutePreUpdateHooks(stdout, errout)
151-
if err != nil {
152-
return err
153-
}
154-
}
155-
if r == menu.EventRuntimePost {
156-
err := actions.ExecutePostUpdateHooks(stdout, errout)
157-
if err != nil {
158-
return err
159-
}
160-
}
161-
case menu.EventTypeDelete:
162-
default:
163-
return fmt.Errorf("unknown event type: %v", t)
164-
}
165-
return nil
166-
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Masterminds/sprig/v3 v3.3.0
77
github.com/google/go-cmp v0.7.0
88
github.com/manifoldco/promptui v0.9.0
9+
github.com/spf13/cobra v1.9.1
910
sigs.k8s.io/yaml v1.4.0
1011
)
1112

@@ -16,10 +17,12 @@ require (
1617
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
1718
github.com/google/uuid v1.6.0 // indirect
1819
github.com/huandu/xstrings v1.5.0 // indirect
20+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1921
github.com/mitchellh/copystructure v1.2.0 // indirect
2022
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2123
github.com/shopspring/decimal v1.4.0 // indirect
2224
github.com/spf13/cast v1.7.0 // indirect
25+
github.com/spf13/pflag v1.0.6 // indirect
2326
golang.org/x/crypto v0.31.0 // indirect
2427
golang.org/x/sys v0.28.0 // indirect
2528
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
1212
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
1313
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
1414
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
15+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
1516
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1617
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1718
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
@@ -23,6 +24,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2324
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2425
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
2526
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
27+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
28+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2629
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2730
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2831
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -37,10 +40,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3740
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3841
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3942
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
43+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4044
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
4145
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
4246
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
4347
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
48+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
49+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
50+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
51+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4452
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
4553
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
4654
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=

0 commit comments

Comments
 (0)