github.com/kaydxh/golang@v0.0.131/pkg/crontab/config.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package crontab
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/go-playground/validator/v10"
    28  	viper_ "github.com/kaydxh/golang/pkg/viper"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/viper"
    31  )
    32  
    33  // Config ...
    34  type Config struct {
    35  	Proto Crontab
    36  
    37  	Validator *validator.Validate
    38  	opts      struct {
    39  		// If set, overrides params below
    40  		viper *viper.Viper
    41  	}
    42  }
    43  
    44  type completedConfig struct {
    45  	*Config
    46  	completeError error
    47  }
    48  
    49  // CompletedConfig ...
    50  type CompletedConfig struct {
    51  	// Embed a private pointer that cannot be instantiated outside of this package.
    52  	*completedConfig
    53  }
    54  
    55  func (c *completedConfig) New(ctx context.Context) (*CrontabSerivce, error) {
    56  
    57  	logrus.Infof("Installing Crontab")
    58  
    59  	if c.completeError != nil {
    60  		return nil, c.completeError
    61  	}
    62  
    63  	if !c.Proto.GetEnabled() {
    64  		return nil, nil
    65  	}
    66  
    67  	rs, err := c.install(ctx)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	logrus.Infof("Installed Crontab")
    72  
    73  	return rs, nil
    74  }
    75  
    76  func (c *completedConfig) install(ctx context.Context) (*CrontabSerivce, error) {
    77  	checkInterval := c.Proto.GetCheckInterval().AsDuration()
    78  	rs := NewCrontabSerivce(checkInterval)
    79  	return rs, nil
    80  }
    81  
    82  // Validate checks Config.
    83  func (c *completedConfig) Validate() error {
    84  	return c.Validator.Struct(c)
    85  }
    86  
    87  // Complete fills in any fields not set that are required to have valid data and can be derived
    88  // from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.
    89  func (c *Config) Complete() CompletedConfig {
    90  	err := c.loadViper()
    91  	if err != nil {
    92  		return CompletedConfig{&completedConfig{
    93  			Config:        c,
    94  			completeError: err,
    95  		}}
    96  	}
    97  
    98  	return CompletedConfig{&completedConfig{Config: c}}
    99  }
   100  
   101  func (c *Config) loadViper() error {
   102  	if c.opts.viper != nil {
   103  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  // NewConfig returns a Config struct with the default values
   110  func NewConfig(options ...ConfigOption) *Config {
   111  	c := &Config{}
   112  	c.ApplyOptions(options...)
   113  
   114  	return c
   115  }