github.com/VMitov/casper@v0.4.0/cmd/casper/context.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/miracl/casper"
    10  	"github.com/miracl/casper/source"
    11  	consulstorage "github.com/miracl/casper/storage/consul"
    12  	filestorage "github.com/miracl/casper/storage/file"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  type context struct {
    17  	path     string
    18  	template *os.File
    19  	storage  casper.Storage
    20  	source   *source.Source
    21  }
    22  
    23  func newContext(path string, opts ...func(*context) error) (*context, error) {
    24  	config := &context{
    25  		path: path,
    26  	}
    27  	for _, opt := range opts {
    28  		if err := opt(config); err != nil {
    29  			return nil, err
    30  		}
    31  	}
    32  
    33  	return config, nil
    34  }
    35  
    36  func (c *context) withSources(sources []string) error {
    37  	sourceTypes := map[string]getSourcer{
    38  		configScheme: getConfigSource,
    39  		"file":       getFileSource,
    40  	}
    41  
    42  	sourceList := make([]source.Getter, len(sources))
    43  	for i, s := range sources {
    44  		u, err := url.Parse(s)
    45  		if err != nil {
    46  			return errors.Wrapf(err, "parsing source %v failed", s)
    47  		}
    48  
    49  		if u.Scheme == "" {
    50  			if !strings.Contains(s, "=") {
    51  				return fmt.Errorf("invalid source: %v", s)
    52  			}
    53  
    54  			// default to config
    55  			u = &url.URL{
    56  				Scheme:   configScheme,
    57  				RawQuery: s,
    58  			}
    59  		}
    60  
    61  		getSourcer, ok := sourceTypes[u.Scheme]
    62  		if !ok {
    63  			return fmt.Errorf("invalid source format %v", u.Scheme)
    64  		}
    65  
    66  		sourceList[i], err = getSourcer(u)
    67  		if err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	var err error
    73  	c.source, err = source.NewMultiSourcer(sourceList...)
    74  	return err
    75  }
    76  
    77  func withSources(sources []string) func(*context) error {
    78  	return func(c *context) error {
    79  		return c.withSources(sources)
    80  	}
    81  }
    82  
    83  func (c *context) withTemplate(path string) error {
    84  	var err error
    85  	c.template, err = os.Open(path)
    86  	return errors.Wrapf(err, "getting template %v failed", path)
    87  }
    88  
    89  func withTemplate(path string) func(*context) error {
    90  	return func(c *context) error {
    91  		return c.withTemplate(path)
    92  	}
    93  }
    94  
    95  func (c *context) withFileStorage(path string) {
    96  	c.storage = filestorage.New(path)
    97  }
    98  
    99  func (c *context) withConsulStorage(addr string) error {
   100  	var err error
   101  	c.storage, err = consulstorage.New(addr)
   102  	return errors.Wrap(err, "creating Consul storage failed")
   103  }