github.com/Jeffail/benthos/v3@v3.65.0/lib/stream/manager/from_path.go (about)

     1  package manager
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/Jeffail/benthos/v3/internal/config"
    10  	"github.com/Jeffail/benthos/v3/lib/stream"
    11  )
    12  
    13  func loadFile(dir, path, testSuffix string, confs map[string]stream.Config) ([]string, error) {
    14  	id, err := config.InferStreamID(dir, path)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	// Do not run unit test files
    20  	if len(testSuffix) > 0 && strings.HasSuffix(id, testSuffix) {
    21  		return nil, nil
    22  	}
    23  
    24  	if _, exists := confs[id]; exists {
    25  		return nil, fmt.Errorf("stream id (%v) collision from file: %v", id, path)
    26  	}
    27  
    28  	conf, lints, err := config.ReadStreamFile(path)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	confs[id] = conf
    34  	return lints, nil
    35  }
    36  
    37  // LoadStreamConfigsFromPath reads a map of stream ids to configurations
    38  // by either walking a directory of .json and .yaml files or by reading a file
    39  // directly. Returns linting errors prefixed with their path.
    40  //
    41  // Deprecated: The streams builder is using ./internal/config now.
    42  func LoadStreamConfigsFromPath(target, testSuffix string, streamMap map[string]stream.Config) ([]string, error) {
    43  	pathLints := []string{}
    44  	target = filepath.Clean(target)
    45  
    46  	if info, err := os.Stat(target); err != nil {
    47  		return nil, err
    48  	} else if !info.IsDir() {
    49  		if pathLints, err = loadFile("", target, "", streamMap); err != nil {
    50  			return nil, fmt.Errorf("failed to load config '%v': %v", target, err)
    51  		}
    52  		return pathLints, nil
    53  	}
    54  
    55  	err := filepath.Walk(target, func(path string, info os.FileInfo, werr error) error {
    56  		if werr != nil {
    57  			return werr
    58  		}
    59  		if info.IsDir() ||
    60  			(!strings.HasSuffix(info.Name(), ".yaml") &&
    61  				!strings.HasSuffix(info.Name(), ".yml")) {
    62  			return nil
    63  		}
    64  
    65  		var lints []string
    66  		if lints, werr = loadFile(target, path, testSuffix, streamMap); werr != nil {
    67  			return fmt.Errorf("failed to load config '%v': %v", path, werr)
    68  		}
    69  
    70  		pathLints = append(pathLints, lints...)
    71  		return nil
    72  	})
    73  
    74  	return pathLints, err
    75  }