Added possibility to specify config via command line option.

This commit is contained in:
Martin Dosch 2018-07-15 11:03:19 +02:00
parent d8ccb4152a
commit 1db063759b
2 changed files with 84 additions and 36 deletions

View file

@ -5,6 +5,7 @@ package main
import (
"encoding/json"
"flag"
"log"
"os"
"os/user"
@ -17,7 +18,7 @@ import (
func main() {
var err error
var configPath string
var configPath, configFile string
type configuration struct {
ServerAddress string
@ -29,6 +30,13 @@ func main() {
Feeds []string
}
// Read path to config from command line option.
configFilePtr := flag.String("config", "none", "path to configuration file")
flag.Parse()
if *configFilePtr != "none" {
configFile = *configFilePtr
} else {
// Get systems user config path.
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
if osConfigDir != "" {
@ -65,14 +73,16 @@ func main() {
}
}
configFile = configPath + "config.json"
}
// Check that config file is existing.
if _, err := os.Stat(configPath + "config.json"); os.IsNotExist(err) {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatal("Error: ", err)
}
// Read configuration file into variable config.
file, _ := os.Open(configPath + "config.json")
file, _ := os.Open(configFile)
defer file.Close()
decoder := json.NewDecoder(file)
config := configuration{}
@ -80,7 +90,7 @@ func main() {
log.Fatal("Error: ", err)
}
if _, err := os.Stat(configPath + "config.json"); os.IsNotExist(err) {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0700)
if err != nil {
log.Fatal("Error: ", err)
@ -124,7 +134,7 @@ func main() {
}
for i := 0; i < len(config.Feeds); i++ {
output, err := getArticles(config.Feeds[i], config.MaxArticles, configPath)
output, err := getArticles(config.Feeds[i], config.MaxArticles)
if err != nil {
log.Fatal(err)
}

View file

@ -8,6 +8,7 @@ import (
"hash/fnv"
"log"
"os"
"os/user"
"strconv"
"strings"
"time"
@ -17,18 +18,55 @@ import (
)
//func getArticles(max int) ([]string, error) {
func getArticles(feedURL string, max int, cachePath string) (string, error) {
func getArticles(feedURL string, max int) (string, error) {
type feedCache struct {
LastChange string
}
var output string
var output, cachePath string
var last time.Time
var lastUpdate feedCache
var file *os.File
var updateTime time.Time
// Get systems user cache path.
osCacheDir := os.Getenv("$XDG_CACHE_HOME")
if osCacheDir != "" {
// Create configPath if not yet existing.
cachePath = osCacheDir + "/feed-to-muc/"
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err = os.MkdirAll(cachePath, 0700)
if err != nil {
log.Fatal("Error: ", err)
}
}
} else { // Get the current user.
curUser, err := user.Current()
if err != nil {
log.Fatal("Error: ", err)
return "", err
}
// Get home directory.
home := curUser.HomeDir
if home == "" {
log.Fatal("Error: No home directory available.")
return "", err
}
// Create cachePath if not yet existing.
cachePath = home + "/.cache/feed-to-muc/"
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err = os.MkdirAll(cachePath, 0700)
if err != nil {
log.Fatal("Error: ", err)
}
}
}
// Create a hash as identifier for the feed.
// The identifier will be used as filename for caching the update time.
// ToDo: cachePath should probably be moved to ~/.cache/feed-to-muc