github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/config/automation.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dustin/go-humanize"
     7  )
     8  
     9  // Automation encapsulates configuration for the automation subsystem
    10  type Automation struct {
    11  	Enabled         bool
    12  	RunStoreMaxSize string
    13  }
    14  
    15  // DefaultAutomation constructs an automation configuration with standard values
    16  func DefaultAutomation() *Automation {
    17  	return &Automation{
    18  		Enabled:         true,
    19  		RunStoreMaxSize: "100Mb",
    20  	}
    21  }
    22  
    23  // SetArbitrary is an interface implementation of base/fill/struct in order to safely
    24  // consume config files that have definitions beyond those specified in the struct.
    25  // This simply ignores all additional fields at read time.
    26  func (a *Automation) SetArbitrary(key string, val interface{}) error {
    27  	return nil
    28  }
    29  
    30  // Validate ensures a correct Automation configuration
    31  func (a *Automation) Validate() error {
    32  	if a.RunStoreMaxSize != "unlimited" && a.RunStoreMaxSize != "" {
    33  		if _, err := humanize.ParseBytes(a.RunStoreMaxSize); err != nil {
    34  			return fmt.Errorf("invalid RunStoreMaxSize: %w", err)
    35  		}
    36  	} else if a.RunStoreMaxSize != "unlimited" {
    37  		return fmt.Errorf("invalid RunStoreMaxSize value: %s", a.RunStoreMaxSize)
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // Copy creates a shallow copy of Automation
    44  func (a *Automation) Copy() *Automation {
    45  	return &Automation{
    46  		Enabled:         a.Enabled,
    47  		RunStoreMaxSize: a.RunStoreMaxSize,
    48  	}
    49  }