2021-06-09 01:19:26 +02:00
|
|
|
package gmitohtml
|
2020-11-24 18:29:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2020-11-27 05:43:03 +01:00
|
|
|
"fmt"
|
2020-11-24 18:29:57 +01:00
|
|
|
"io/ioutil"
|
2020-11-27 05:43:03 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-11-24 18:29:57 +01:00
|
|
|
|
2021-04-08 05:49:29 +02:00
|
|
|
"code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml"
|
2020-11-24 18:29:57 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certConfig struct {
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
|
|
|
|
cert tls.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
type appConfig struct {
|
2020-11-27 05:43:03 +01:00
|
|
|
Bookmarks map[string]string
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
// Convert image links to images instead of normal links
|
|
|
|
ConvertImages bool
|
|
|
|
|
2020-11-24 18:29:57 +01:00
|
|
|
Certs map[string]*certConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = &appConfig{
|
2020-11-27 05:43:03 +01:00
|
|
|
Bookmarks: make(map[string]string),
|
|
|
|
|
2021-06-09 01:19:26 +02:00
|
|
|
ConvertImages: false,
|
|
|
|
|
2020-11-24 18:29:57 +01:00
|
|
|
Certs: make(map[string]*certConfig),
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:43:03 +01:00
|
|
|
func defaultConfigPath() string {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err == nil && homedir != "" {
|
|
|
|
return path.Join(homedir, ".config", "gmitohtml", "config.yaml")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:29:57 +01:00
|
|
|
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
|
|
|
|
}
|
2020-11-27 05:43:03 +01:00
|
|
|
|
|
|
|
func saveConfig(configPath string) error {
|
|
|
|
config.Bookmarks = gmitohtml.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
|
|
|
|
}
|