Skip to content

Commit 1e65afd

Browse files
committed
Add init command
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
1 parent 3c9a207 commit 1e65afd

2 files changed

Lines changed: 123 additions & 1 deletion

File tree

cmd/privatebin/main.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ var (
6565
skipTLSVerify bool
6666
proxy string
6767

68+
force bool
69+
initHost string
70+
6871
rootCmd = &cobra.Command{
6972
Use: "privatebin",
7073
Version: fmt.Sprintf("%s-%s (%s)", version, commit, date),
@@ -334,6 +337,63 @@ var (
334337
return nil
335338
},
336339
}
340+
341+
initCmd = &cobra.Command{
342+
Use: "init",
343+
Short: "Generate a configuration file",
344+
SilenceUsage: true,
345+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
346+
if cfgPath == "" {
347+
homeDir, err := os.UserHomeDir()
348+
if err != nil {
349+
return fmt.Errorf("cannot get user home directory: %w", err)
350+
}
351+
352+
cfgPath = path.Join(homeDir, ".config", "privatebin", "config.json")
353+
}
354+
355+
return nil
356+
},
357+
RunE: func(cmd *cobra.Command, args []string) error {
358+
if !force {
359+
if _, err := os.Stat(cfgPath); err == nil {
360+
return fmt.Errorf("configuration file already exists at %s, use --force to overwrite", cfgPath)
361+
}
362+
}
363+
364+
cfg := map[string]any{
365+
"bin": []map[string]any{
366+
{
367+
"name": "",
368+
"host": initHost,
369+
},
370+
},
371+
"expire": "1day",
372+
"formatter": "plaintext",
373+
"gzip": true,
374+
}
375+
376+
data, err := json.MarshalIndent(cfg, "", " ")
377+
if err != nil {
378+
return fmt.Errorf("cannot marshal configuration: %w", err)
379+
}
380+
381+
data = append(data, '\n')
382+
383+
dir := filepath.Dir(cfgPath)
384+
if err := os.MkdirAll(dir, 0o755); err != nil {
385+
return fmt.Errorf("cannot create configuration directory: %w", err)
386+
}
387+
388+
if err := os.WriteFile(cfgPath, data, 0o600); err != nil {
389+
return fmt.Errorf("cannot write configuration file: %w", err)
390+
}
391+
392+
fmt.Fprintf(os.Stdout, "%s\n", cfgPath)
393+
394+
return nil
395+
},
396+
}
337397
)
338398

339399
func init() {
@@ -358,7 +418,10 @@ func init() {
358418
showCmd.Flags().StringVar(&password, "password", "", "the paste password")
359419
showCmd.Flags().BoolVar(&skipTLSVerify, "skip-tls-verify", false, "skip TLS certificate verification")
360420

361-
rootCmd.AddCommand(showCmd, createCmd)
421+
initCmd.Flags().BoolVar(&force, "force", false, "overwrite existing configuration file")
422+
initCmd.Flags().StringVar(&initHost, "host", "https://privatebin.net", "the host of the default privatebin instance")
423+
424+
rootCmd.AddCommand(showCmd, createCmd, initCmd)
362425
}
363426

364427
func main() {

doc/privatebin-init.1.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: PRIVATEBIN-INIT
3+
header: Privatebin Manual
4+
footer: 1.0.0
5+
date: Feb 14, 2026
6+
section: 1
7+
---
8+
# NAME
9+
**privatebin-init** – generate a configuration file
10+
11+
# SYNOPSIS
12+
**privatebin init** [-h | -help] [-\-force] [-\-host=\<url\>]
13+
14+
# DESCRIPTION
15+
Generate a configuration file with sensible defaults and write it to
16+
the default location at _~/.config/privatebin/config.json_. If the
17+
**-\-config** flag is set, the file is written to that path instead.
18+
19+
The generated configuration contains a single bin entry pointing to the
20+
host specified by **-\-host** (defaulting to _https://privatebin.net_),
21+
with the following defaults: expire set to _1day_, formatter set to
22+
_plaintext_, and gzip enabled.
23+
24+
If a configuration file already exists at the target path, the command
25+
will fail unless **-\-force** is specified.
26+
27+
# OPTIONS
28+
**-h, -\-help**
29+
: Show help message.
30+
31+
**-\-force**
32+
: Overwrite existing configuration file.
33+
34+
**-\-host** \<url\>
35+
: The host URL of the default privatebin instance (default:
36+
_https://privatebin.net_).
37+
38+
# EXAMPLES
39+
Generate a default configuration file:
40+
41+
$ privatebin init
42+
43+
Generate a configuration file for a custom instance:
44+
45+
$ privatebin init --host https://bin.example.com
46+
47+
Overwrite an existing configuration file:
48+
49+
$ privatebin init --force
50+
51+
Generate a configuration file at a custom path:
52+
53+
$ privatebin init --config /path/to/config.json
54+
55+
# SEE ALSO
56+
**privatebin.conf**(5)
57+
58+
# AUTHORS
59+
Bryan Frimin.

0 commit comments

Comments
 (0)