github.com/Rookout/GoSDK@v0.1.48/pkg/augs/actions/action_run_processor.go (about)

     1  package actions
     2  
     3  import (
     4  	"github.com/Rookout/GoSDK/pkg/com_ws"
     5  	"github.com/Rookout/GoSDK/pkg/processor/namespaces"
     6  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
     7  	"github.com/Rookout/GoSDK/pkg/types"
     8  )
     9  
    10  type Processor interface {
    11  	Process(namespace namespaces.Namespace)
    12  }
    13  
    14  type ProcessorFactory interface {
    15  	GetProcessor(configuration []types.AugConfiguration) (Processor, rookoutErrors.RookoutError)
    16  }
    17  
    18  type Action interface {
    19  	Execute(augID types.AugID, reportID string, namespace namespaces.Namespace, output com_ws.Output) rookoutErrors.RookoutError
    20  }
    21  
    22  type actionRunProcessor struct {
    23  	processor     Processor
    24  	postProcessor Processor
    25  }
    26  
    27  func NewActionRunProcessor(arguments types.AugConfiguration, processorFactory ProcessorFactory) (Action, rookoutErrors.RookoutError) {
    28  	rawOps, ok := arguments["operations"].([]interface{})
    29  	if !ok {
    30  		return nil, rookoutErrors.NewRookInvalidActionConfiguration(arguments)
    31  	}
    32  	var ops []types.AugConfiguration
    33  	for _, rawOp := range rawOps {
    34  		ops = append(ops, rawOp.(map[string]interface{}))
    35  	}
    36  	p, err := processorFactory.GetProcessor(ops)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var postProcessor Processor
    42  	if rawPostOps, ok := arguments["post_operations"].([]interface{}); ok {
    43  		var postOps []types.AugConfiguration
    44  		for _, rawPostOp := range rawPostOps {
    45  			postOps = append(postOps, rawPostOp.(map[string]interface{}))
    46  		}
    47  		postProcessor, _ = processorFactory.GetProcessor(postOps)
    48  	}
    49  
    50  	return &actionRunProcessor{
    51  		processor:     p,
    52  		postProcessor: postProcessor,
    53  	}, nil
    54  }
    55  
    56  func (a *actionRunProcessor) Execute(augID types.AugID, reportID string, namespace namespaces.Namespace, output com_ws.Output) rookoutErrors.RookoutError {
    57  	a.processor.Process(namespace)
    58  	attribute, err := namespace.ReadAttribute("store")
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if _, ok := attribute.(*namespaces.ContainerNamespace); ok {
    64  		attribute.(*namespaces.ContainerNamespace).OnClose = namespace.(*namespaces.ContainerNamespace).OnClose
    65  	}
    66  	output.SendUserMessage(augID, reportID, attribute)
    67  
    68  	if a.postProcessor != nil {
    69  		a.postProcessor.Process(namespace)
    70  	}
    71  
    72  	return nil
    73  }