|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 |
| - "errors" |
6 | 4 | "fmt"
|
7 |
| - "io" |
8 | 5 | "os"
|
9 | 6 |
|
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" |
13 | 8 | )
|
14 | 9 |
|
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 |
| - |
56 | 10 | 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 { |
127 | 12 | fmt.Println(err)
|
128 | 13 | os.Exit(1)
|
129 | 14 | }
|
130 | 15 | }
|
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 |
| -} |
0 commit comments