github.com/oam-dev/kubevela@v1.9.11/pkg/builtin/registry/registry_runner.go (about)

     1  /*
     2  Copyright 2021 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 registry
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  	"sync"
    24  
    25  	"cuelang.org/go/cue"
    26  	"cuelang.org/go/cue/errors"
    27  
    28  	"github.com/kubevela/workflow/pkg/cue/model/value"
    29  )
    30  
    31  // Meta provides context for running a task.
    32  type Meta struct {
    33  	Context context.Context
    34  	Stdin   io.Reader
    35  	Stdout  io.Writer
    36  	Stderr  io.Writer
    37  	Obj     cue.Value
    38  	Err     error
    39  }
    40  
    41  // Lookup fetches the value of context by filed
    42  func (m *Meta) Lookup(field string) cue.Value {
    43  	f := m.Obj.LookupPath(value.FieldPath(field))
    44  	if !f.Exists() {
    45  		m.Err = fmt.Errorf("invalid lookup argument")
    46  		return cue.Value{}
    47  	}
    48  	if err := f.Err(); err != nil {
    49  		m.Err = errors.Promote(err, "lookup")
    50  	}
    51  	return f
    52  }
    53  
    54  // Int64 fetch the value formatted int64 of context by filed
    55  func (m *Meta) Int64(field string) int64 {
    56  	f := m.Obj.LookupPath(value.FieldPath(field))
    57  	value, err := f.Int64()
    58  	if err != nil {
    59  		m.Err = fmt.Errorf("invalid int64 argument, %w", err)
    60  
    61  		return 0
    62  	}
    63  	return value
    64  }
    65  
    66  // String fetch the value formatted string of context by filed
    67  func (m *Meta) String(field string) string {
    68  	f := m.Obj.LookupPath(value.FieldPath(field))
    69  	value, err := f.String()
    70  	if err != nil {
    71  		m.Err = fmt.Errorf("invalid string argument, %w", err)
    72  		return ""
    73  	}
    74  	return value
    75  }
    76  
    77  // Bytes fetch the value formatted bytes of context by filed
    78  func (m *Meta) Bytes(field string) []byte {
    79  	f := m.Obj.LookupPath(value.FieldPath(field))
    80  	value, err := f.Bytes()
    81  	if err != nil {
    82  		m.Err = fmt.Errorf("invalid bytes argument, %w", err)
    83  		return nil
    84  	}
    85  	return value
    86  }
    87  
    88  // RunnerFunc creates a Runner.
    89  type RunnerFunc func(v cue.Value) (Runner, error)
    90  
    91  // Runner defines a command type.
    92  type Runner interface {
    93  	// Init is called with the original configuration before any task is run.
    94  	// As a result, the configuration may be incomplete, but allows some
    95  	// validation before tasks are kicked off.
    96  	// Init(v cue.Value)
    97  
    98  	// Runner runs given the current value and returns a new value which is to
    99  	// be unified with the original result.
   100  	Run(meta *Meta) (results interface{}, err error)
   101  }
   102  
   103  // RegisterRunner registers a task for cue commands.
   104  func RegisterRunner(key string, f RunnerFunc) {
   105  	runners.Store(key, f)
   106  }
   107  
   108  // LookupRunner returns the RunnerFunc for a key.
   109  func LookupRunner(key string) RunnerFunc {
   110  	v, ok := runners.Load(key)
   111  	if !ok {
   112  		return nil
   113  	}
   114  	return v.(RunnerFunc)
   115  }
   116  
   117  var runners sync.Map