Skip to content

Commit

Permalink
fix cli usage with registry
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Oct 14, 2024
1 parent cfd66d6 commit d144356
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
16 changes: 9 additions & 7 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ var executeCmd = &cobra.Command{
logger.SetLevel(logrus.DebugLevel)
}

solution := args[0]
if solution != "saptunesolutionapply" {
return fmt.Errorf("solution %s provided as argument, is invalid", solution)
}
operatorName := args[0]

opArgs := make(operator.OperatorArguments)
err := json.Unmarshal([]byte(arguments), &opArgs)
if err != nil {
return fmt.Errorf("could not unmarhsal %s into arguments", arguments)
}

op := operator.NewSaptuneApplySolution(opArgs, "test-cli", operator.OperatorOptions[operator.SaptuneApplySolution]{
BaseOperatorOptions: []operator.BaseOption{operator.WithLogger(logger)},
})
registry := operator.StandardRegistry(operator.WithCustomLogger(logger))

builder, err := registry.GetOperatorBuilder(operatorName)
if err != nil {
return err
}

op := builder("test-cli", opArgs)

report := op.Run(context.Background())
if report.Error != nil {
Expand Down
23 changes: 16 additions & 7 deletions pkg/operator/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ const (

type BaseOperationOption Option[baseOperation]

func WithCustomLogger(logger *logrus.Logger) BaseOperationOption {
return func(b *baseOperation) {
b.loggerInstance = logger
}
}

type baseOperation struct {
arguments OperatorArguments
resources map[string]any
logger *logrus.Entry
arguments OperatorArguments
resources map[string]any
loggerInstance *logrus.Logger
logger *logrus.Entry
}

func newBaseOperator(
Expand All @@ -25,15 +32,17 @@ func newBaseOperator(
options ...BaseOperationOption,
) baseOperation {
base := &baseOperation{
arguments: arguments,
resources: make(map[string]any),
logger: logrus.StandardLogger().WithField("operation_id", operationID),
arguments: arguments,
resources: make(map[string]any),
loggerInstance: logrus.StandardLogger(),
}

for _, opt := range options {
opt(base)
}

base.logger = base.loggerInstance.WithField("operation_id", operationID)

return *base
}

Expand All @@ -42,5 +51,5 @@ func (b *baseOperation) standardDiff(_ context.Context) map[string]any {
diff["before"] = b.resources[beforeDiffField]
diff["after"] = b.resources[afterFieldDiff]

return nil
return diff
}
6 changes: 3 additions & 3 deletions pkg/operator/saptuneapplysolution_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (sa *saptuneApplySolution) plan(_ context.Context) error {
return errors.New("could not call saptune solution applied")
}

sa.resources["planSolutionAppliedOutput"] = solutionAppliedOutput
sa.resources[beforeDiffField] = solutionAppliedOutput

return nil
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (sa *saptuneApplySolution) verify(_ context.Context) error {
}

if alreadyApplied := isSaptuneSolutionAlreadyApplied(solutionAppliedOutput, sa.parsedArguments.solution); alreadyApplied {
sa.resources["verifySolutionAppliedOutput"] = solutionAppliedOutput
sa.resources[afterFieldDiff] = solutionAppliedOutput
return nil
}

Expand All @@ -147,7 +147,7 @@ func (sa *saptuneApplySolution) rollback(_ context.Context) error {
}

func (sa *saptuneApplySolution) operationDiff(ctx context.Context) map[string]any {
return sa.operationDiff(ctx)
return sa.standardDiff(ctx)
}

func isSaptuneVersionSupported(version string) bool {
Expand Down

0 comments on commit d144356

Please sign in to comment.