2020-11-24 18:29:57 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-11-27 05:43:03 +01:00
|
|
|
"gitlab.com/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
|
|
|
|
|
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),
|
|
|
|
|
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
|
|
|
|
}
|