mirror of
https://code.rocketnine.space/tslocum/gmitohtml.git
synced 2024-11-15 09:47:51 +01:00
81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
package gmitohtml
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type certConfig struct {
|
|
Cert string
|
|
Key string
|
|
|
|
cert tls.Certificate
|
|
}
|
|
|
|
type appConfig struct {
|
|
Bookmarks map[string]string
|
|
|
|
// Convert image links to images instead of normal links
|
|
ConvertImages bool
|
|
|
|
Certs map[string]*certConfig
|
|
}
|
|
|
|
var Config = &appConfig{
|
|
Bookmarks: make(map[string]string),
|
|
|
|
ConvertImages: false,
|
|
|
|
Certs: make(map[string]*certConfig),
|
|
}
|
|
|
|
func DefaultConfigPath() string {
|
|
homedir, err := os.UserHomeDir()
|
|
if err == nil && homedir != "" {
|
|
return path.Join(homedir, ".config", "gmitohtml", "config.yaml")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func ReadConfig(configPath string) error {
|
|
if configPath == "" {
|
|
return errors.New("file unspecified")
|
|
}
|
|
|
|
configData, err := ioutil.ReadFile(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var newConfig *appConfig
|
|
err = yaml.Unmarshal(configData, &newConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Config = newConfig
|
|
|
|
return nil
|
|
}
|
|
|
|
func SaveConfig(configPath string) error {
|
|
Config.Bookmarks = GetBookmarks()
|
|
|
|
out, err := yaml.Marshal(Config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal configuration: %s", err)
|
|
}
|
|
|
|
os.MkdirAll(path.Dir(configPath), 0755) // Ignore error
|
|
|
|
err = ioutil.WriteFile(configPath, out, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to save configuration to %s: %s", configPath, err)
|
|
}
|
|
return nil
|
|
}
|