Skip to content

Commit

Permalink
Initial implementation of runtime
Browse files Browse the repository at this point in the history
The runtime provides command run() methods with common tools such as
logger, reporter and various clients. This change will allow removing a
lot of boilerplate from the various commands and make refactoring them
simpler as extracting methods will require fewer parameters.
  • Loading branch information
tbrisker committed Jun 20, 2022
1 parent d55ae7d commit f3de500
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/rosa/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package rosa

import (
"os"

"github.com/openshift/rosa/pkg/aws"
"github.com/openshift/rosa/pkg/logging"
"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/reporter"
"github.com/sirupsen/logrus"
)

type Runtime struct {
Reporter *reporter.Object
Logger *logrus.Logger
OCMClient *ocm.Client
AWSClient aws.Client
Creator *aws.Creator
}

func NewRuntime() *Runtime {
reporter := reporter.CreateReporterOrExit()
logger := logging.NewLogger()
return &Runtime{Reporter: reporter, Logger: logger}
}

// Adds an OCM client to the runtime. Requires a deferred call to `.Cleanup()` to close connections.
func (r *Runtime) WithOCM() *Runtime {
if r.OCMClient == nil {
r.OCMClient = ocm.CreateNewClientOrExit(r.Logger, r.Reporter)
}
return r
}

// Adds an AWS client to the runtime
func (r *Runtime) WithAWS() *Runtime {
if r.AWSClient == nil {
r.AWSClient = aws.CreateNewClientOrExit(r.Logger, r.Reporter)
}
if r.Creator == nil {
var err error
r.Creator, err = r.AWSClient.GetCreator()
if err != nil {
r.Reporter.Errorf("Failed to get AWS creator: %v", err)
os.Exit(1)
}
}
return r
}

func (r *Runtime) Cleanup() {
if r.OCMClient != nil {
if err := r.OCMClient.Close(); err != nil {
r.Reporter.Errorf("Failed to close OCM connection: %v", err)
}
}
}

0 comments on commit f3de500

Please sign in to comment.