Add and enforce golangci-lint (#16)

This commit is contained in:
Dave Gallant
2020-07-05 22:44:05 -04:00
committed by GitHub
parent 2ec9bca107
commit d9474be233
13 changed files with 141 additions and 59 deletions

View File

@@ -1,31 +1,53 @@
package config
import (
"errors"
"github.com/juju/errors"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/davegallant/srv/file"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)
// ConfigPath defines where the configuration is stored
const ConfigPath = ".config/srv/config.yml"
// Configuration stores the global config
type Configuration struct {
Feeds []string `yaml:"feeds"`
ExternalViewer string `yaml:"externalViewer,omitempty"`
ExternalViewerArgs []string `yaml:"externalViewerArgs,omitempty"`
Path string
}
// DefaultConfiguration can be used if a config is missing
var DefaultConfiguration = Configuration{
Feeds: []string{
"https://news.ycombinator.com/rss",
"https://www.reddit.com/r/golang/.rss",
"https://www.reddit.com/r/linux/.rss",
"https://aws.amazon.com/blogs/security/feed/",
"https://www.phoronix.com/rss.php",
"https://www.zdnet.com/topic/security/rss.xml",
},
Path: ConfigPath,
}
// GetUGetUGetUserConfigPath returns the full configuration path for the current user
func GetUserConfigPath() (string, error) {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
createConfigPath := []string{usr.HomeDir}
createConfigPath = append(createConfigPath, strings.Split(ConfigPath, "/")...)
return path.Join(createConfigPath...), nil
}
// DetermineExternalViewer checks the OS to decide the default viewer
@@ -40,12 +62,19 @@ func DetermineExternalViewer() (string, error) {
return "", errors.New("Unable to determine a default external viewer")
}
// LoadConfiguration takes a filename (configuration) and loads it.
func LoadConfiguration(f string) Configuration {
// EnsureConfigDirExists ensures directory exists with correct permissions
func EnsureConfigDirExists(d string) error {
var AppFs = afero.NewOsFs()
return AppFs.MkdirAll(d, 0700)
}
// LoadConfiguration loads a configuration from a file
func LoadConfiguration(f string) (Configuration, error) {
var config Configuration
if !file.Exists(f) {
WriteConfig(DefaultConfiguration, f)
err := WriteConfig(DefaultConfiguration, f)
return DefaultConfiguration, errors.Annotate(err, "failed to load configuration")
}
data, err := ioutil.ReadFile(f)
@@ -67,19 +96,26 @@ func LoadConfiguration(f string) Configuration {
if err != nil {
log.Panicln(err)
}
return config
return config, nil
}
// WriteConfig writes a config to disk
func WriteConfig(config Configuration, file string) error {
c, err := yaml.Marshal(&config)
func WriteConfig(config Configuration, f string) error {
d := filepath.Dir(f)
err := EnsureConfigDirExists(d)
if err != nil {
log.Fatalf("Unable to marshal default config: %v", err)
return errors.Annotatef(err, "Unable to to create config directory '%s'", d)
}
err = ioutil.WriteFile(file, c, 0600)
c, err := yaml.Marshal(&config)
if err != nil {
log.Fatalf("Unable to write default config: %v", err)
return errors.Annotatef(err, "Unable to marshal config '%s'", f)
}
err = ioutil.WriteFile(f, c, 0600)
if err != nil {
return errors.Annotatef(err, "Unable to write default config: '%s'", f)
}
return nil
}