Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse a dufrc file to allow for default settings #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -42,6 +45,7 @@ var (
width = flag.Uint("width", 0, "max output width")
themeOpt = flag.String("theme", defaultThemeName(), "color themes: dark, light, ansi")
styleOpt = flag.String("style", defaultStyleName(), "style: unicode, ascii")
norcOpt = flag.Bool("norc", false, "ignore dufrc file, falling back to default settings")

availThreshold = flag.String("avail-threshold", "10G,1G", "specifies the coloring threshold (yellow, red) of the avail column, must be integer with optional SI prefixes")
usageThreshold = flag.String("usage-threshold", "0.5,0.9", "specifies the coloring threshold (yellow, red) of the usage bars as a floating point number from 0 to 1")
Expand Down Expand Up @@ -142,8 +146,40 @@ func findInKey(str string, km map[string]struct{}) bool {
return false
}

func main() {
func parseFlags() error {
configHome := os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
configHome = filepath.Join(os.Getenv("HOME"), ".config")
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could consider os.UserConfigDir

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's a much better choice; thanks! Changed in 7b0fd48

rcFilePath := filepath.Join(configHome, "dufrc")
file, err := os.Open(rcFilePath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to open dufrc file at %s: %s", rcFilePath, err)
}

for _, arg := range os.Args {
// checking this manually to not avoid calling flag.Parse() twice -- not sure how defaults would react
if arg == "-norc" || arg == "--norc" {
flag.Parse()
return nil
}
}

scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
os.Args = append(os.Args, strings.TrimSpace(scanner.Text()))
}
flag.Parse()
return nil
}

func main() {
err := parseFlags()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

if *version {
if len(CommitSHA) > 7 {
Expand Down