Skip to content

Commit

Permalink
Use XDG_CONFIG_HOME for config and theme (#34)
Browse files Browse the repository at this point in the history
* default config and theme to XDG_CONFIG_HOME
* copy default theme and config to XDG_CONFIG_HOME if they do not exist
  • Loading branch information
sboysel authored May 24, 2022
1 parent 2bc1583 commit ef75710
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ You can also specify theme, configuration and database manually.
./gorss -config gorss.conf -theme default.theme -db mydb.db
```

Gorss expect to have `gorss.conf` and `default.theme` in the same directory as `gorss` itself if not
starting it with parameters as above.
If either the configuration or theme files are not specified, gorss will attempt
to use`$XDG_CONFIG_HOME/gorss/gorss.conf` and
`$XDG_CONFIG_HOME/gorss/themes/default.theme`, respectively. These files will be
created from the defaults if not present.

To build and run use the makefile.
```
Expand Down
19 changes: 19 additions & 0 deletions cmd/gorss/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -55,6 +56,13 @@ func main() {
s := conf.QueryConfig(defaultConfig)
if s != "" {
cfg = s
} else {
cfg = fmt.Sprintf("%s/%s", configHome, defaultConfig)
// check if default config exists, copy if not
_, err := os.Stat(cfg)
if errors.Is(err, os.ErrNotExist) {
internal.CopyFile(defaultConfig, cfg)
}
}
}

Expand All @@ -63,6 +71,17 @@ func main() {
s := conf.QueryConfig(defaultTheme)
if s != "" {
theme = s
} else {
theme = fmt.Sprintf("%s/%s", configHome, defaultTheme)
// check if default config exists, copy if not
_, err := os.Stat(theme)
if errors.Is(err, os.ErrNotExist) {
themeDir := fmt.Sprintf("%s/%s", configHome, "themes")
if err := os.Mkdir(themeDir, os.ModePerm); err != nil {
log.Printf("Failed to create dir: %s\n", themeDir)
}
internal.CopyFile(defaultTheme, theme)
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal

import (
"fmt"
"io"
"os"
)

// copy file from src to dst
func CopyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()

destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}

0 comments on commit ef75710

Please sign in to comment.