github.com/kaydxh/golang@v0.0.131/pkg/file-cleanup/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 filecleanup
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/go-playground/validator/v10"
    28  	disk_ "github.com/kaydxh/golang/pkg/file-cleanup/disk"
    29  	viper_ "github.com/kaydxh/golang/pkg/viper"
    30  	"github.com/sirupsen/logrus"
    31  	"github.com/spf13/viper"
    32  )
    33  
    34  // Config ...
    35  type Config struct {
    36  	Proto disk_.DiskCleaner
    37  
    38  	Validator *validator.Validate
    39  	opts      struct {
    40  		// If set, overrides params below
    41  		viper       *viper.Viper
    42  		diskOptions []disk_.DiskCleanerConfigOption
    43  	}
    44  }
    45  
    46  type completedConfig struct {
    47  	*Config
    48  	completeError error
    49  }
    50  
    51  // CompletedConfig ...
    52  type CompletedConfig struct {
    53  	// Embed a private pointer that cannot be instantiated outside of this package.
    54  	*completedConfig
    55  }
    56  
    57  func (c *completedConfig) New(ctx context.Context) (*disk_.DiskCleanerSerivce, error) {
    58  
    59  	logrus.Infof("Installing DiskCleanerSerivce")
    60  
    61  	if c.completeError != nil {
    62  		return nil, c.completeError
    63  	}
    64  
    65  	if !c.Proto.GetEnabled() {
    66  		return nil, nil
    67  	}
    68  
    69  	rs, err := c.install(ctx)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	logrus.Infof("Installed Crontab")
    74  
    75  	return rs, nil
    76  }
    77  
    78  func (c *completedConfig) install(ctx context.Context) (*disk_.DiskCleanerSerivce, error) {
    79  
    80  	opts := []disk_.DiskCleanerConfigOption{
    81  		disk_.WithDiskCheckInterval(c.Proto.GetCheckInterval().AsDuration()),
    82  		disk_.WithDiskBaseExpired(c.Proto.GetBaseExpired().AsDuration()),
    83  		disk_.WithDiskMinExpired(c.Proto.GetMinExpired().AsDuration()),
    84  	}
    85  	opts = append(opts, c.opts.diskOptions...)
    86  
    87  	return disk_.NewDiskCleanerSerivce(
    88  		c.Proto.GetDiskUsage(),
    89  		c.Proto.GetPaths(),
    90  		c.Proto.GetExts(),
    91  		opts...,
    92  	)
    93  }
    94  
    95  // Validate checks Config.
    96  func (c *completedConfig) Validate() error {
    97  	return c.Validator.Struct(c)
    98  }
    99  
   100  // Complete fills in any fields not set that are required to have valid data and can be derived
   101  // from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver.
   102  func (c *Config) Complete(options ...ConfigOption) CompletedConfig {
   103  	err := c.loadViper()
   104  	if err != nil {
   105  		return CompletedConfig{&completedConfig{
   106  			Config:        c,
   107  			completeError: err,
   108  		}}
   109  	}
   110  
   111  	c.ApplyOptions(options...)
   112  
   113  	return CompletedConfig{&completedConfig{Config: c}}
   114  }
   115  
   116  func (c *Config) loadViper() error {
   117  	if c.opts.viper != nil {
   118  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  // NewConfig returns a Config struct with the default values
   125  func NewConfig(options ...ConfigOption) *Config {
   126  	c := &Config{}
   127  	c.ApplyOptions(options...)
   128  
   129  	return c
   130  }