diff --git a/README.md b/README.md index b562535c..86718c04 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,17 @@ depending on how much space is still available. You can set your own thresholds: duf --avail-threshold="10G,1G" duf --usage-threshold="0.5,0.9" +### Configuration + +You can put default arguments to be picked up by duf in a `dufrc` file, placed in `XDG_CONFIG_HOME` +(e.g. `~/.config/dufrc`). +Each option needs to be put in a separate line, like so: + + -only + local + +`-norc` command-line argument can be used to disable loading of the `dufrc` file. + ### Bonus If you prefer your output as JSON: diff --git a/duf.1 b/duf.1 index 221cf56d..bce1ffcf 100644 --- a/duf.1 +++ b/duf.1 @@ -45,6 +45,10 @@ list inode information instead of block usage \fB--json\fP output all devices in JSON format .TP +\fB-norc\fP +do not process default arguments from a dufrc file (see +.BR CONFIGURATION ) +.TP \fB--only\fP show only specific devices, separated with commas: local, network, fuse, special, loops, binds .TP @@ -169,6 +173,16 @@ If you prefer your output as JSON: .PP $ duf --json .PP +.SH CONFIGURATION +You can put default arguments to be picked up by duf in a \fIdufrc\/\fR file, placed in \fIXDG_CONFIG_HOME\/\fR +(e.g. ~/.config/dufrc). +Each option needs to be put in a separate line, like so: + + --only + local + +\fI-norc\/\fR command-line argument can be used to disable loading of the \fIdufrc\/\fR file. +.PP .SH NOTES Portions of duf's code are copied and modified from https://github\&.com/shirou/gopsutil\&. .PP diff --git a/go.mod b/go.mod index 7dc3cc7f..a3b94470 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,8 @@ require ( github.com/clipperhouse/uax29/v2 v2.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/muesli/go-app-paths v0.2.2 // indirect github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/text v0.22.0 // indirect ) diff --git a/go.sum b/go.sum index e73e4f15..6e246fc6 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,10 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/muesli/go-app-paths v0.2.2 h1:NqG4EEZwNIhBq/pREgfBmgDmt3h1Smr1MjZiXbpZUnI= +github.com/muesli/go-app-paths v0.2.2/go.mod h1:SxS3Umca63pcFcLtbjVb+J0oD7cl4ixQWoBKhGEtEho= github.com/muesli/mango v0.2.0 h1:iNNc0c5VLQ6fsMgAqGQofByNUBH2Q2nEbD6TaI+5yyQ= github.com/muesli/mango v0.2.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4= github.com/muesli/mango-pflag v0.2.0 h1:QViokgKDZQCzKhYe1zH8D+UlPJzBSGoP9yx0hBG0t5k= diff --git a/main.go b/main.go index de48aa5b..6edc314b 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "bufio" "encoding/json" + "errors" "fmt" "os" "runtime/debug" @@ -11,6 +13,7 @@ import ( wildcard "github.com/IGLOU-EU/go-wildcard" "github.com/jedib0t/go-pretty/v6/table" + gap "github.com/muesli/go-app-paths" "github.com/muesli/termenv" flag "github.com/spf13/pflag" "golang.org/x/term" @@ -44,6 +47,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") @@ -145,6 +149,33 @@ func findInKey(str string, km map[string]struct{}) bool { return false } +func parseFlags() error { + rcFilePath, err := gap.NewScope(gap.User, "").ConfigPath("dufrc") + if err != nil { + return fmt.Errorf("failed to determine user config file location: %s", err) + } + 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 printVersion() { info, ok := debug.ReadBuildInfo() var buildTime time.Time @@ -195,7 +226,11 @@ func printVersion() { func main() { // hide -h from help, it's just for df compatibility _ = flag.CommandLine.MarkHidden("human-readable") - flag.Parse() + err := parseFlags() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } if *version { printVersion()