first commit

This commit is contained in:
Timofey.Kovalev
2025-10-14 10:35:42 +03:00
commit 9972005cb5
9 changed files with 386 additions and 0 deletions

54
internal/config/config.go Normal file
View File

@ -0,0 +1,54 @@
package config
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/go-yaml/yaml"
)
func LoadConfig(filePath string) (*Config, error) {
cfg := Config{}
isDefaultConfig := filePath == DefaultConfigPath
if strings.HasPrefix(filePath, "~") {
if v, ok := os.LookupEnv("HOME"); ok {
filePath = strings.Replace(filePath, "~", v, 1)
} else {
return nil, fmt.Errorf("$HOME not set in env")
}
}
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
if isDefaultConfig {
dir, _ := path.Split(filePath)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, err
}
err = os.WriteFile(filePath, []byte(DefaultConfig), 0644)
if err != nil {
return nil, err
}
}
return &cfg, nil
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
err = yaml.Unmarshal([]byte(data), &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}

15
internal/config/const.go Normal file
View File

@ -0,0 +1,15 @@
package config
const DefaultConfigPath = "~/.config/qmail/main.yaml"
const DefaultConfig = `
servers:
# - name: s1
# host: localhost
# port: 587
# use-tls: true
# auth:
# login: login
# password: password
`

24
internal/config/struct.go Normal file
View File

@ -0,0 +1,24 @@
package config
type Auth struct {
Login string `yaml:"login"`
Password string `yaml:"password"`
}
type Server struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
From string `yaml:"from"`
To string `yaml:"to"`
UseTLS bool `yaml:"use-tls"`
Auth *Auth `yaml:"auth"`
}
type Config struct {
Servers []Server
}