Skip to content

Commit 4bc869f

Browse files
committed
Support falling back to a global configuration file
Signed-off-by: Alex Kopp <alex@kopp.dev>
1 parent 4625bd1 commit 4bc869f

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Attempt to read a global system configuration file if `~/.config/privatebin/config.json` is not found. On Windows, `C:\ProgramData\privatebin\config.json` and on other operating systems, `/etc/privatebin/config.json`.
14+
1115
## [2.1.1] - 2025-09-08
1216

1317
### Fixed

cmd/privatebin/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"path"
2727
"path/filepath"
28+
"runtime"
2829
"strings"
2930

3031
"github.com/spf13/cobra"
@@ -83,6 +84,22 @@ var (
8384
}
8485

8586
cfgPath = path.Join(homeDir, ".config", "privatebin", "config.json")
87+
88+
// Try to load from user's config first
89+
_, err = os.Stat(cfgPath)
90+
if err != nil {
91+
// If user config doesn't exist, try system-wide config
92+
var systemPath string
93+
if runtime.GOOS == "windows" {
94+
systemPath = path.Join("C:", "ProgramData", "privatebin", "config.json")
95+
} else {
96+
systemPath = path.Join(string(os.PathSeparator), "etc", "privatebin", "config.json")
97+
}
98+
99+
if _, err := os.Stat(systemPath); err == nil {
100+
cfgPath = systemPath
101+
}
102+
}
86103
}
87104

88105
cfg, err := loadCfgFile(cfgPath)
@@ -303,7 +320,7 @@ var (
303320

304321
func init() {
305322
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "the command output format")
306-
rootCmd.PersistentFlags().StringVarP(&cfgPath, "config", "c", "", "the config file (default is $HOME/.config/privatebin/config.json)")
323+
rootCmd.PersistentFlags().StringVarP(&cfgPath, "config", "c", "", "the config file (default is $HOME/.config/privatebin/config.json, or system-wide /etc/privatebin/config.json on Linux/macOS, C:\\ProgramData\\privatebin\\config.json on Windows)")
307324
rootCmd.PersistentFlags().StringVarP(&binName, "bin", "b", "", "the name of the privatebin instance to use (default \"\")")
308325
rootCmd.PersistentFlags().StringSliceVarP(&extraHeaderFields, "header", "H", []string{}, "extra HTTP header fields to include in the request sent")
309326

doc/privatebin.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ instances.
3030
**-c, -\-config** \<path\>
3131
: The path of the configuration file (default
3232
"~/.config/privatebin/config.json").
33+
If not found, the CLI will also look for a system-wide configuration file at
34+
"/etc/privatebin/config.json" (Linux/macOS) or "C:\\ProgramData\\privatebin\\config.json" (Windows).
3335

3436
**-H, -\-header** \<key=value\>
3537
: The extra HTTP header fields to include in the request sent.

doc/privatebin.conf.5.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ A bit more complete configuration file:
124124
# FILES
125125

126126
_~/.config/privatebin/config.json_
127-
: Default location of the privatebin configuration. The file has to be
128-
created manually as it is not installed with a standard installation.
127+
: Default location of the privatebin configuration. The file has to be created manually as it is not installed with a standard installation.
128+
129+
_/etc/privatebin/config.json_ (Linux/macOS)
130+
: System-wide configuration file location, used if the user config is not found.
131+
132+
_C:\\ProgramData\\privatebin\\config.json_ (Windows)
133+
: System-wide configuration file location, used if the user config is not found.
129134

130135
# AUTHORS
131136

0 commit comments

Comments
 (0)