github.com/kubevela/workflow@v0.6.0/pkg/tasks/discover.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tasks
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	monitorContext "github.com/kubevela/pkg/monitor/context"
    25  
    26  	"github.com/kubevela/workflow/pkg/tasks/builtin"
    27  	"github.com/kubevela/workflow/pkg/tasks/custom"
    28  	"github.com/kubevela/workflow/pkg/types"
    29  )
    30  
    31  type taskDiscover struct {
    32  	builtin            map[string]types.TaskGenerator
    33  	customTaskDiscover *custom.TaskLoader
    34  }
    35  
    36  // NewTaskDiscover new task discover
    37  func NewTaskDiscover(ctx monitorContext.Context, options types.StepGeneratorOptions) types.TaskDiscover {
    38  	return &taskDiscover{
    39  		builtin: map[string]types.TaskGenerator{
    40  			types.WorkflowStepTypeStepGroup: builtin.StepGroup,
    41  		},
    42  		customTaskDiscover: custom.NewTaskLoader(options.TemplateLoader.LoadTemplate, options.PackageDiscover, options.Providers, options.LogLevel, options.ProcessCtx),
    43  	}
    44  }
    45  
    46  // GetTaskGenerator get task generator by name.
    47  func (td *taskDiscover) GetTaskGenerator(ctx context.Context, name string) (types.TaskGenerator, error) {
    48  	tg, ok := td.builtin[name]
    49  	if ok {
    50  		return tg, nil
    51  	}
    52  	if td.customTaskDiscover != nil {
    53  		var err error
    54  		tg, err = td.customTaskDiscover.GetTaskGenerator(ctx, name)
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		return tg, nil
    59  
    60  	}
    61  	return nil, errors.Errorf("can't find task generator: %s", name)
    62  }