github.com/release-engineering/exodus-rsync@v1.11.2/internal/conf/structs.go (about)

     1  package conf
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/release-engineering/exodus-rsync/internal/args"
     8  )
     9  
    10  type sharedConfig struct {
    11  	GwEnvRaw          string `yaml:"gwenv"`
    12  	GwCertRaw         string `yaml:"gwcert"`
    13  	GwKeyRaw          string `yaml:"gwkey"`
    14  	GwURLRaw          string `yaml:"gwurl"`
    15  	GwPollIntervalRaw int    `yaml:"gwpollinterval"`
    16  	GwBatchSizeRaw    int    `yaml:"gwbatchsize"`
    17  	GwCommitRaw       string `yaml:"gwcommit"`
    18  	GwMaxAttemptsRaw  int    `yaml:"gwmaxattempts"`
    19  	GwMaxBackoffRaw   int    `yaml:"gwmaxbackoff"`
    20  	RsyncModeRaw      string `yaml:"rsyncmode"`
    21  	LogLevelRaw       string `yaml:"loglevel"`
    22  	LoggerRaw         string `yaml:"logger"`
    23  	DiagRaw           bool   `yaml:"diag"`
    24  	StripRaw          string `yaml:"strip"`
    25  	UploadThreadsRaw  int    `yaml:"uploadthreads"`
    26  }
    27  
    28  type environment struct {
    29  	sharedConfig `yaml:",inline"`
    30  	args         args.Config `embed:"1"`
    31  
    32  	PrefixRaw string `yaml:"prefix"`
    33  
    34  	parent *globalConfig
    35  }
    36  
    37  type globalConfig struct {
    38  	sharedConfig `yaml:",inline"`
    39  	args         args.Config `embed:"1"`
    40  
    41  	// Configuration for each environment.
    42  	EnvironmentsRaw []environment `yaml:"environments"`
    43  }
    44  
    45  // MissingConfigFile is an error type for cases in which no config file is found.
    46  type MissingConfigFile struct {
    47  	// Configuration file paths that were checked.
    48  	candidates []string
    49  }
    50  
    51  func (m *MissingConfigFile) Error() string {
    52  	return fmt.Sprintf("no existing config file in: %s", strings.Join(m.candidates, ", "))
    53  }
    54  
    55  func (g *globalConfig) GwCert() string {
    56  	return g.GwCertRaw
    57  }
    58  
    59  func (g *globalConfig) GwKey() string {
    60  	return g.GwKeyRaw
    61  }
    62  
    63  func (g *globalConfig) GwURL() string {
    64  	return g.GwURLRaw
    65  }
    66  
    67  func (g *globalConfig) GwEnv() string {
    68  	return g.GwEnvRaw
    69  }
    70  
    71  func (g *globalConfig) GwPollInterval() int {
    72  	return nonEmptyInt(g.GwPollIntervalRaw, 5000)
    73  }
    74  
    75  func (g *globalConfig) GwBatchSize() int {
    76  	return nonEmptyInt(g.GwBatchSizeRaw, 10000)
    77  }
    78  
    79  func (g *globalConfig) GwCommit() string {
    80  	return g.GwCommitRaw
    81  }
    82  
    83  func (g *globalConfig) GwMaxAttempts() int {
    84  	return nonEmptyInt(g.GwMaxAttemptsRaw, 10)
    85  }
    86  
    87  func (g *globalConfig) GwMaxBackoff() int {
    88  	return nonEmptyInt(g.GwMaxBackoffRaw, 20000)
    89  }
    90  
    91  func (g *globalConfig) UploadThreads() int {
    92  	return nonEmptyInt(g.UploadThreadsRaw, 4)
    93  }
    94  
    95  func nonEmptyString(a, b string) string {
    96  	if a != "" {
    97  		return a
    98  	}
    99  	return b
   100  }
   101  
   102  func nonEmptyInt(a, b int) int {
   103  	if a != 0 {
   104  		return a
   105  	}
   106  	return b
   107  }
   108  
   109  func (g *globalConfig) RsyncMode() string {
   110  	return nonEmptyString(g.RsyncModeRaw, "exodus")
   111  }
   112  
   113  func (g *globalConfig) LogLevel() string {
   114  	return nonEmptyString(g.LogLevelRaw, "info")
   115  }
   116  
   117  func (g *globalConfig) Logger() string {
   118  	return nonEmptyString(g.LoggerRaw, "auto")
   119  }
   120  
   121  func (g *globalConfig) Verbosity() int {
   122  	return g.args.Verbose
   123  }
   124  
   125  func (g *globalConfig) Diag() bool {
   126  	return g.args.Diag || g.DiagRaw
   127  }
   128  
   129  func (g *globalConfig) Strip() string {
   130  	return g.StripRaw
   131  }
   132  
   133  func (e *environment) GwCert() string {
   134  	return nonEmptyString(e.GwCertRaw, e.parent.GwCert())
   135  }
   136  
   137  func (e *environment) GwKey() string {
   138  	return nonEmptyString(e.GwKeyRaw, e.parent.GwKey())
   139  }
   140  
   141  func (e *environment) GwURL() string {
   142  	return nonEmptyString(e.GwURLRaw, e.parent.GwURL())
   143  }
   144  
   145  func (e *environment) GwEnv() string {
   146  	return nonEmptyString(e.GwEnvRaw, e.parent.GwEnv())
   147  }
   148  
   149  func (e *environment) GwPollInterval() int {
   150  	return nonEmptyInt(e.GwPollIntervalRaw, e.parent.GwPollInterval())
   151  }
   152  
   153  func (e *environment) GwBatchSize() int {
   154  	return nonEmptyInt(e.GwBatchSizeRaw, e.parent.GwBatchSize())
   155  }
   156  
   157  func (e *environment) GwCommit() string {
   158  	return nonEmptyString(e.GwCommitRaw, e.parent.GwCommit())
   159  }
   160  
   161  func (e *environment) GwMaxAttempts() int {
   162  	return nonEmptyInt(e.GwMaxAttemptsRaw, e.parent.GwMaxAttempts())
   163  }
   164  
   165  func (e *environment) GwMaxBackoff() int {
   166  	return nonEmptyInt(e.GwMaxBackoffRaw, e.parent.GwMaxBackoff())
   167  }
   168  
   169  func (e *environment) RsyncMode() string {
   170  	return nonEmptyString(e.RsyncModeRaw, e.parent.RsyncMode())
   171  }
   172  
   173  func (e *environment) LogLevel() string {
   174  	return nonEmptyString(e.LogLevelRaw, e.parent.LogLevel())
   175  }
   176  
   177  func (e *environment) Logger() string {
   178  	return nonEmptyString(e.LoggerRaw, e.parent.Logger())
   179  }
   180  
   181  func (e *environment) Verbosity() int {
   182  	return nonEmptyInt(e.args.Verbose, e.parent.Verbosity())
   183  }
   184  
   185  func (e *environment) Diag() bool {
   186  	return e.DiagRaw || e.parent.Diag()
   187  }
   188  
   189  func (e *environment) Prefix() string {
   190  	return e.PrefixRaw
   191  }
   192  
   193  func (e *environment) Strip() string {
   194  	// If the 'strip:' key is defined in the global config, the environment's prefix will not
   195  	// be stripped from the destination path by default. The prefix is only stripped from the
   196  	// destination path if the 'strip:' key is undefined.
   197  	return nonEmptyString(nonEmptyString(e.StripRaw, e.parent.Strip()), e.PrefixRaw)
   198  }
   199  
   200  func (e *environment) UploadThreads() int {
   201  	return nonEmptyInt(e.UploadThreadsRaw, e.parent.UploadThreads())
   202  }