code.cestus.io/tools/fabricator@v0.4.3/pkg/fabricator/types.go (about)

     1  package fabricator
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"syscall"
     8  
     9  	"github.com/onsi/ginkgo/v2"
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/pflag"
    12  	"gopkg.in/yaml.v3"
    13  )
    14  
    15  // TerminationSignals are signals that cause the program to exit in the
    16  // supported platforms (linux, darwin, windows).
    17  var TerminationSignals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}
    18  
    19  // Environment is a key value map for environment variables
    20  type Environment map[string]string
    21  
    22  // IOStreams provides the standard names for iostreams.  This is useful for embedding and for unit testing.
    23  // Inconsistent and different names make it hard to read and review code
    24  type IOStreams struct {
    25  	// In think, os.Stdin
    26  	In io.Reader
    27  	// Out think, os.Stdout
    28  	Out io.Writer
    29  	// ErrOut think, os.Stderr
    30  	ErrOut io.Writer
    31  }
    32  
    33  // NewTestIOStreams returns a valid IOStreams and in, out, errout buffers for unit tests
    34  func NewTestIOStreams() (IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
    35  	in := &bytes.Buffer{}
    36  	out := &bytes.Buffer{}
    37  	errOut := &bytes.Buffer{}
    38  
    39  	return IOStreams{
    40  		In:     in,
    41  		Out:    out,
    42  		ErrOut: errOut,
    43  	}, in, out, errOut
    44  }
    45  
    46  // NewTestIOStreamsDiscard returns a valid IOStreams that just discards
    47  func NewTestIOStreamsDiscard() IOStreams {
    48  	in := &bytes.Buffer{}
    49  	return IOStreams{
    50  		In:     in,
    51  		Out:    io.Discard,
    52  		ErrOut: io.Discard,
    53  	}
    54  }
    55  
    56  // NewGinkoTestIOStreams returns a valid IOStreams for use with ginkgotests
    57  func NewGinkoTestIOStreams() IOStreams {
    58  	in := &bytes.Buffer{}
    59  	return IOStreams{
    60  		In:     in,
    61  		Out:    ginkgo.GinkgoWriter,
    62  		ErrOut: ginkgo.GinkgoWriter,
    63  	}
    64  }
    65  
    66  // NewStdIOStreams returns an IOStreams instance using os std streams
    67  func NewStdIOStreams() IOStreams {
    68  	return IOStreams{
    69  		In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr,
    70  	}
    71  }
    72  
    73  // OptionProvider is an interface for command options
    74  type OptionProvider interface {
    75  	RegisterOptions(flagset *pflag.FlagSet)
    76  }
    77  
    78  // RootOptions defines a common set of options for all plugins
    79  type RootOptions struct {
    80  	FabricatorFile string
    81  	RootDirectory  string
    82  	PluginPath     string
    83  	Help           bool
    84  	FlagParser     FlagParser
    85  }
    86  
    87  // RegisterOptions implements the OptionsProvider interface
    88  func (o *RootOptions) RegisterOptions(flagset *pflag.FlagSet) {
    89  	flagset.StringVar(&o.FabricatorFile, "fabfile", "./.fabricator.yml", "fab-file to load")
    90  	flagset.StringVar(&o.RootDirectory, "rootdir", "./", "root directory for all file operations")
    91  	flagset.StringVarP(&o.PluginPath, "plugin-path", "p", "./", "path extension where plugins will be loaded from")
    92  	flagset.BoolP("help", "h", false, "Help for")
    93  }
    94  
    95  // FlagParser defines the signature for a function to parse commandline flags. It exists so that cobra's flag parsing can be less magical and give control over when and what is actually parsed
    96  type FlagParser func(cmd *cobra.Command) error
    97  
    98  // Typed for the fabricator config file
    99  
   100  type FabricatorConfig struct {
   101  	ApiVersion string               `yaml:"apiVersion" json:"apiVersion"`
   102  	Kind       string               `yaml:"kind" json:"kind"`
   103  	Components FabricatorComponents `yaml:"components" json:"components"`
   104  }
   105  
   106  type FabricatorComponent struct {
   107  	Name      string    `yaml:"name" json:"name"`
   108  	Generator string    `yaml:"generator" json:"generator"`
   109  	Spec      yaml.Node `yaml:"spec" json:"spec"`
   110  }
   111  
   112  type FabricatorComponents []FabricatorComponent