Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 2 additions & 153 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,166 +1,15 @@
package main

import (
"context"
"errors"
"fmt"
"io"
"os"

"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/menu"
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/project"
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/template"
"github.com/leonsteinhaeuser/openshift-gitops-cli/internal/cmd"
)

var (
projectConfig = &project.ProjectConfig{}
)

const (
PROJECTFILENAME = "PROJECT.yaml"
)

// check for project file and load it
func init() {
_, err := os.Stat(PROJECTFILENAME)
if err != nil && !errors.Is(err, os.ErrNotExist) {
fmt.Println("An error occurred while checking for the PROJECT.yaml file", err)
os.Exit(1)
return
}
if errors.Is(err, os.ErrNotExist) {
f, err := os.Create(PROJECTFILENAME)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
defer f.Close()
_, err = f.WriteString("basePath: overlays/\ntemplateBasePath: templates/\n")
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
}

pc, err := project.ParseConfig(PROJECTFILENAME)
if err != nil {
fmt.Println("An error occurred while parsing the PROJECT.yaml file", err)
os.Exit(1)
return
}
projectConfig = pc
}

func main() {
eventsPipeline := make(chan menu.Event, 100)
ctx, cf := context.WithCancel(context.Background())
defer cf()

go func(ctx context.Context) {
// handle config file updates
for {
select {
case <-ctx.Done():
close(eventsPipeline)
return
case event := <-eventsPipeline:
// we only need to update the config file if the action is a post action
// because we need to update the config only, if the action was successful
if event.Runtime == menu.EventRuntimePost {
// update config file
err := project.UpdateOrCreateConfig(PROJECTFILENAME, projectConfig)
if err != nil {
fmt.Println("An error occurred while updating the project config", err)
return
}
}

if event.Origin == menu.EventOriginAddon {
if event.Runtime == menu.EventRuntimePre {
addonPath := projectConfig.Addons[event.Environment].Path
_, err := template.LoadManifest(projectConfig.Addons[event.Environment].Path)
if err != nil {
fmt.Printf("An error occurred while loading the addon [%s] manifest file: %s, %v\n", event.Environment, addonPath, err)
os.Exit(1)
return
}
}
continue
}

if event.Environment != "" && event.Stage == "" && event.Cluster == "" {
env := projectConfig.GetEnvironment(event.Environment)
err := executeHook(os.Stdout, os.Stderr, event.Type, event.Runtime, env.Actions)
if err != nil {
fmt.Println(err)
return
}
}

if event.Environment != "" && event.Stage != "" && event.Cluster == "" {
stage := projectConfig.GetStage(event.Environment, event.Stage)
err := executeHook(os.Stdout, os.Stderr, event.Type, event.Runtime, stage.Actions)
if err != nil {
fmt.Println(err)
return
}
}

if event.Environment != "" && event.Stage != "" && event.Cluster != "" {
cluster := projectConfig.GetCluster(event.Environment, event.Stage, event.Cluster)
if event.Type == menu.EventTypeCreate || event.Type == menu.EventTypeUpdate {
err := cluster.Render(projectConfig, event.Environment, event.Stage)
if err != nil {
fmt.Printf("An error occurred while rendering the cluster [%s] configuration: %v", event.Cluster, err)
return
}
}
}
}
}
}(ctx)

err := menu.RootMenu(projectConfig, eventsPipeline)
if err != nil {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func executeHook(stdout, errout io.Writer, t menu.EventType, r menu.EventRuntime, actions project.Actions) error {
switch t {
case menu.EventTypeCreate:
if r == menu.EventRuntimePre {
err := actions.ExecutePreCreateHooks(stdout, errout)
if err != nil {
return err
}
}
if r == menu.EventRuntimePost {
err := actions.ExecutePostCreateHooks(stdout, errout)
if err != nil {
return err
}
}
return nil
case menu.EventTypeUpdate:
if r == menu.EventRuntimePre {
err := actions.ExecutePreUpdateHooks(stdout, errout)
if err != nil {
return err
}
}
if r == menu.EventRuntimePost {
err := actions.ExecutePostUpdateHooks(stdout, errout)
if err != nil {
return err
}
}
case menu.EventTypeDelete:
default:
return fmt.Errorf("unknown event type: %v", t)
}
return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.3.0
github.com/google/go-cmp v0.7.0
github.com/manifoldco/promptui v0.9.0
github.com/spf13/cobra v1.9.1
sigs.k8s.io/yaml v1.4.0
)

Expand All @@ -16,10 +17,12 @@ require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand All @@ -23,6 +24,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -37,10 +40,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
Expand Down
Loading
Loading