sk-matrix-project/backend/golang/matrix-middle-service/pkg/config/toml/config.go

33 lines
544 B
Go
Raw Normal View History

package toml
import (
"github.com/pelletier/go-toml/v2"
"matrix-middle-service/pkg/config"
"os"
"path/filepath"
)
func GenerateConfig() error {
p, _ := filepath.Abs("./config.toml")
flag := os.O_RDWR
_, err := os.Stat(p)
exist := !os.IsNotExist(err)
if !exist {
f, err := os.OpenFile(p, flag|os.O_CREATE, 0644)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
encoder := toml.NewEncoder(f)
encoder.SetIndentTables(true)
_ = encoder.Encode(config.DefaultConfig())
_ = f.Sync()
}
return nil
}