forked from openshift/rosa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |