Files
decort-golang-sdk/config/legacy-config.go

96 lines
2.3 KiB
Go
Raw Normal View History

2023-03-01 19:05:53 +03:00
package config
2023-03-24 17:09:30 +03:00
import (
"encoding/json"
"os"
2023-06-01 16:50:10 +03:00
"time"
2023-03-24 17:09:30 +03:00
"gopkg.in/yaml.v3"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
2023-03-01 19:05:53 +03:00
// Legacy client configuration
type LegacyConfig struct {
// ServiceAccount username
// Required: true
// Example : "osh_mikoev"
2023-03-24 17:09:30 +03:00
Username string `json:"username" yaml:"username" validate:"required"`
2023-03-01 19:05:53 +03:00
// ServiceAccount password
// Required: true
// Example: "[1o>hYkjnJr)HI78q7t&#%8Lm"
2023-03-24 17:09:30 +03:00
Password string `json:"password" yaml:"password" validate:"required"`
2023-03-01 19:05:53 +03:00
// Platform token
// Required: false
// Example: "158e76424b0d4810b6086hgbhj928fc4a6bc06e"
2023-03-24 17:09:30 +03:00
Token string `json:"token" yaml:"token"`
2023-03-01 19:05:53 +03:00
// Address of the platform on which the actions are planned
// Required: true
// Example: "https://mr4.digitalenergy.online"
2023-03-24 17:09:30 +03:00
DecortURL string `json:"decortUrl" yaml:"decortUrl" validate:"url"`
2023-03-01 19:05:53 +03:00
// Amount platform request attempts
// Default value: 5
// Required: false
2023-03-24 17:09:30 +03:00
Retries uint64 `json:"retries" yaml:"retries"`
2023-03-01 19:05:53 +03:00
2023-09-24 12:11:31 +03:00
// Skip verify
2023-03-01 19:05:53 +03:00
// Required: false
2023-03-24 17:09:30 +03:00
SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"`
2023-06-01 16:50:10 +03:00
// 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 *LegacyConfig) SetTimeout(dur time.Duration) {
c.Timeout = Duration(dur)
2023-03-24 17:09:30 +03:00
}
// ParseLegacyConfigJSON parses LegacyConfig from specified JSON-formatted file.
func ParseLegacyConfigJSON(path string) (LegacyConfig, error) {
file, err := os.ReadFile(path)
if err != nil {
return LegacyConfig{}, err
}
var config LegacyConfig
err = json.Unmarshal(file, &config)
if err != nil {
return LegacyConfig{}, err
}
err = validators.ValidateConfig(config)
if err != nil {
2023-10-25 17:37:18 +03:00
return LegacyConfig{}, validators.ValidationErrors(validators.GetErrors(err))
2023-03-24 17:09:30 +03:00
}
return config, nil
}
// ParseLegacyConfigYAML parses LegacyConfig from specified YAML-formatted file.
func ParseLegacyConfigYAML(path string) (LegacyConfig, error) {
file, err := os.ReadFile(path)
if err != nil {
return LegacyConfig{}, err
}
var config LegacyConfig
err = yaml.Unmarshal(file, &config)
if err != nil {
return LegacyConfig{}, err
}
err = validators.ValidateConfig(config)
if err != nil {
2023-10-25 17:37:18 +03:00
return LegacyConfig{}, validators.ValidationErrors(validators.GetErrors(err))
2023-03-24 17:09:30 +03:00
}
return config, nil
2023-03-01 19:05:53 +03:00
}