You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.2 KiB

package config
import (
"encoding/json"
"os"
"time"
"gopkg.in/yaml.v2"
"repository.basistech.ru/BASIS/dynamix-standard-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
}