mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-11-22 22:18:39 +01:00
Added possibility to specify config via command line option.
This commit is contained in:
parent
d8ccb4152a
commit
1db063759b
2 changed files with 84 additions and 36 deletions
|
@ -5,6 +5,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
@ -17,7 +18,7 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var configPath string
|
var configPath, configFile string
|
||||||
|
|
||||||
type configuration struct {
|
type configuration struct {
|
||||||
ServerAddress string
|
ServerAddress string
|
||||||
|
@ -29,6 +30,13 @@ func main() {
|
||||||
Feeds []string
|
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.
|
// Get systems user config path.
|
||||||
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
|
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
|
||||||
if osConfigDir != "" {
|
if osConfigDir != "" {
|
||||||
|
@ -65,14 +73,16 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
configFile = configPath + "config.json"
|
||||||
|
}
|
||||||
|
|
||||||
// Check that config file is existing.
|
// 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)
|
log.Fatal("Error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read configuration file into variable config.
|
// Read configuration file into variable config.
|
||||||
file, _ := os.Open(configPath + "config.json")
|
file, _ := os.Open(configFile)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
config := configuration{}
|
config := configuration{}
|
||||||
|
@ -80,7 +90,7 @@ func main() {
|
||||||
log.Fatal("Error: ", err)
|
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)
|
err = os.MkdirAll(configPath, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error: ", err)
|
log.Fatal("Error: ", err)
|
||||||
|
@ -124,7 +134,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(config.Feeds); i++ {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,18 +18,55 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
//func getArticles(max int) ([]string, error) {
|
//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 {
|
type feedCache struct {
|
||||||
LastChange string
|
LastChange string
|
||||||
}
|
}
|
||||||
|
|
||||||
var output string
|
var output, cachePath string
|
||||||
var last time.Time
|
var last time.Time
|
||||||
var lastUpdate feedCache
|
var lastUpdate feedCache
|
||||||
var file *os.File
|
var file *os.File
|
||||||
var updateTime time.Time
|
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.
|
// Create a hash as identifier for the feed.
|
||||||
// The identifier will be used as filename for caching the update time.
|
// The identifier will be used as filename for caching the update time.
|
||||||
// ToDo: cachePath should probably be moved to ~/.cache/feed-to-muc
|
// ToDo: cachePath should probably be moved to ~/.cache/feed-to-muc
|
||||||
|
|
Loading…
Reference in a new issue