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

     1  package manager
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/Jeffail/benthos/v3/lib/config"
    10  	"github.com/Jeffail/benthos/v3/lib/stream"
    11  )
    12  
    13  //------------------------------------------------------------------------------
    14  
    15  // LoadStreamConfigsFromDirectory reads a map of stream ids to configurations
    16  // by walking a directory of .json and .yaml files.
    17  //
    18  // Deprecated: The streams builder is using ./internal/config now.
    19  func LoadStreamConfigsFromDirectory(replaceEnvVars bool, dir string) (map[string]stream.Config, error) {
    20  	streamMap := map[string]stream.Config{}
    21  
    22  	dir = filepath.Clean(dir)
    23  
    24  	if info, err := os.Stat(dir); err != nil {
    25  		if os.IsNotExist(err) {
    26  			return streamMap, nil
    27  		}
    28  		return nil, err
    29  	} else if !info.IsDir() {
    30  		return streamMap, nil
    31  	}
    32  
    33  	err := filepath.Walk(dir, func(path string, info os.FileInfo, werr error) error {
    34  		if werr != nil {
    35  			return werr
    36  		}
    37  		if info.IsDir() ||
    38  			(!strings.HasSuffix(info.Name(), ".yaml") &&
    39  				!strings.HasSuffix(info.Name(), ".json")) {
    40  			return nil
    41  		}
    42  
    43  		var id string
    44  		if id, werr = filepath.Rel(dir, path); werr != nil {
    45  			return werr
    46  		}
    47  		id = strings.Trim(id, string(filepath.Separator))
    48  		id = strings.ReplaceAll(id, string(filepath.Separator), "_")
    49  
    50  		if strings.HasSuffix(info.Name(), ".yaml") {
    51  			id = strings.TrimSuffix(id, ".yaml")
    52  		} else {
    53  			id = strings.TrimSuffix(id, ".json")
    54  		}
    55  
    56  		if _, exists := streamMap[id]; exists {
    57  			return fmt.Errorf("stream id (%v) collision from file: %v", id, path)
    58  		}
    59  
    60  		conf := config.New()
    61  		if _, readerr := config.Read(path, true, &conf); readerr != nil {
    62  			// TODO: Read and report linting errors.
    63  			return readerr
    64  		}
    65  
    66  		streamMap[id] = conf.Config
    67  		return nil
    68  	})
    69  
    70  	return streamMap, err
    71  }
    72  
    73  //------------------------------------------------------------------------------