github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/discovery/flatfile/options.go (about)

     1  // Copyright (c) 2021-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package flatfile
     6  
     7  import (
     8  	"io"
     9  
    10  	"github.com/choria-io/go-choria/protocol"
    11  )
    12  
    13  type SourceFormat int
    14  
    15  const (
    16  	unknownFormat SourceFormat = iota
    17  	// TextFormat reads nodes from a text file 1 node per line
    18  	TextFormat
    19  
    20  	// JSONFormat parses a JSON file expecting an array of nodes
    21  	JSONFormat
    22  
    23  	// YAMLFormat parses a YAML file expecting an array of nodes
    24  	YAMLFormat
    25  
    26  	// ChoriaResponsesFormat uses Choria responses as produced by choria req -j as source
    27  	ChoriaResponsesFormat
    28  )
    29  
    30  type dOpts struct {
    31  	source string
    32  	format SourceFormat
    33  	reader io.Reader
    34  	filter *protocol.Filter
    35  	do     map[string]string
    36  }
    37  
    38  // DiscoverOption configures the broadcast discovery method
    39  type DiscoverOption func(o *dOpts)
    40  
    41  // Filter sets the filter to use for the discovery, else a blank one is used
    42  func Filter(f *protocol.Filter) DiscoverOption {
    43  	return func(o *dOpts) {
    44  		o.filter = f
    45  	}
    46  }
    47  
    48  // Format specifies the file format
    49  func Format(f SourceFormat) DiscoverOption {
    50  	return func(o *dOpts) {
    51  		o.format = f
    52  	}
    53  }
    54  
    55  // File sets the file to read nodes from
    56  func File(f string) DiscoverOption {
    57  	return func(o *dOpts) {
    58  		o.source = f
    59  	}
    60  }
    61  
    62  // Reader specifies a io.Reader as source
    63  func Reader(r io.Reader) DiscoverOption {
    64  	return func(o *dOpts) {
    65  		o.reader = r
    66  	}
    67  }
    68  
    69  // DiscoveryOptions sets the key value pairs that make user supplied discovery options.
    70  //
    71  // Supported options:
    72  //
    73  //	filter - GJSON Path Syntax search over YAML or JSON data
    74  //	  file - set the file to read
    75  //	format - override format detection: json, yaml, yml, choriarpc, results, rpc, response, text, txt
    76  func DiscoveryOptions(opt map[string]string) DiscoverOption {
    77  	return func(o *dOpts) {
    78  		o.do = opt
    79  	}
    80  }