v1.0.0
This commit is contained in:
95
config/config.go
Normal file
95
config/config.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/validators"
|
||||
)
|
||||
|
||||
// Client configuration
|
||||
type Config struct {
|
||||
// ServiceAccount username
|
||||
// Required: false
|
||||
// Example : "admin_user"
|
||||
Username string `json:"username" yaml:"username" validate:"required"`
|
||||
|
||||
// ServiceAccount password
|
||||
// Required: false
|
||||
// Example: "[1o>hYkjnJr)HI78q7t&#%8Lm"
|
||||
Password string `json:"password" yaml:"password" validate:"required"`
|
||||
|
||||
// Platform token
|
||||
// Required: false
|
||||
// Example: "158e76424b0d4810b6086hgbhj928fc4a6bc06e"
|
||||
Token string `json:"token" yaml:"token"`
|
||||
|
||||
// Address of the platform on which the actions are planned
|
||||
// Required: true
|
||||
// Example: "https://192.168.0.1"
|
||||
VControlURL string `json:"vControlURL" yaml:"vControlURL" validate:"url"`
|
||||
|
||||
// Amount platform request attempts
|
||||
// Default value: 5
|
||||
// Required: false
|
||||
Retries uint64 `json:"retries" yaml:"retries"`
|
||||
|
||||
// Skip verify
|
||||
// Required: false
|
||||
SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"`
|
||||
|
||||
// HTTP client timeout, unlimited if left empty
|
||||
// Required: false
|
||||
Timeout Duration `json:"timeout" yaml:"timeout"`
|
||||
}
|
||||
|
||||
// SetTimeout is used to set HTTP client timeout.
|
||||
func (c *Config) SetTimeout(dur time.Duration) {
|
||||
c.Timeout = Duration(dur)
|
||||
}
|
||||
|
||||
// ParseConfigJSON parses Config from specified JSON-formatted file.
|
||||
func ParseConfigJSON(path string) (Config, error) {
|
||||
file, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
err = json.Unmarshal(file, &config)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
err = validators.ValidateConfig(config)
|
||||
if err != nil {
|
||||
return Config{}, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// ParseConfigYAML parses Config from specified YAML-formatted file.
|
||||
func ParseConfigYAML(path string) (Config, error) {
|
||||
file, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
err = yaml.Unmarshal(file, &config)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
err = validators.ValidateConfig(config)
|
||||
if err != nil {
|
||||
return Config{}, validators.ValidationErrors(validators.GetErrors(err))
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
54
config/timeouts.go
Normal file
54
config/timeouts.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Duration is a wrapper around time.Duration (used for better user experience)
|
||||
type Duration time.Duration
|
||||
|
||||
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var v interface{}
|
||||
if err := unmarshal(&v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case string:
|
||||
tmp, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(tmp)
|
||||
return nil
|
||||
case float64:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid duration %v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case string:
|
||||
tmp, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(tmp)
|
||||
return nil
|
||||
case float64:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid duration %v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Duration) Get() time.Duration {
|
||||
return time.Duration(*d)
|
||||
}
|
||||
Reference in New Issue
Block a user