Social sender
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.

42 lines
952 B

package config
import (
"fmt"
"os"
"ssender/internal/models"
"gopkg.in/yaml.v2"
)
func NewConfig(configPath string) (*models.Config, error) {
// Create config structure
config := &models.Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(config); err != nil {
return nil, err
}
// Validate required fields
if config.Dbpath == "" {
return nil, fmt.Errorf("dbpath is required in config")
}
if config.Telegram.Send && (config.Telegram.Token == "" || config.Telegram.ChatId == 0) {
return nil, fmt.Errorf("telegram token and chatid are required when send is enabled")
}
if config.VK.Send && (config.VK.Token == "" || config.VK.OwnerId == 0) {
return nil, fmt.Errorf("vk token and ownerid are required when send is enabled")
}
return config, nil
}