gmitohtml-upstream/config.go
Aaron Fischer 586b293bef Convert images to images instead of links
Image links will result in a `img` tag instead of a `a` tag. This
behaviour is disabled by default and can be enabled by adding the
following line to to config file:

convertimages: true

To get access to the config in the convert file, I had to move the
config file inside the gmitohtml namespace. This is specially handy
later on if the config file contains other settings which are useful for
the rest of the codebase.
2021-06-09 01:19:26 +02:00

82 lines
1.4 KiB
Go

package gmitohtml
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"code.rocketnine.space/tslocum/gmitohtml/pkg/gmitohtml"
"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 = 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
}