Skip to content

Commit 7605dd4

Browse files
committed
config: add interactive database URL generator
Add a "pw config url" subcommand that prompts for database connection parameters and produces a properly URL-encoded connection string. This helps avoid encoding mistakes with special characters in passwords. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 92b9279 commit 7605dd4

2 files changed

Lines changed: 136 additions & 4 deletions

File tree

‎cmd/pw/config/main.go‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Patchwork - automated patch tracking system
2+
// Copyright (C) The Patchwork Contributors (see CONTRIBUTORS)
3+
//
4+
// SPDX-License-Identifier: GPL-2.0-or-later
5+
6+
package config
7+
8+
import (
9+
"bufio"
10+
"fmt"
11+
"net/url"
12+
"os"
13+
"strings"
14+
15+
"golang.org/x/term"
16+
17+
"github.com/getpatchwork/patchwork/cmd/pw/pw"
18+
"github.com/getpatchwork/patchwork/pkg/config"
19+
)
20+
21+
type PrintCmd struct{}
22+
23+
type URLCmd struct{}
24+
25+
type CLI struct {
26+
Print PrintCmd `cmd:"" default:"withargs" help:"Print default configuration to stdout."`
27+
URL URLCmd `cmd:"" help:"Generate a database connection URL."`
28+
}
29+
30+
func (c *PrintCmd) Run(ctx *pw.Context) error {
31+
return config.Generate(ctx.Config, os.Stdout)
32+
}
33+
34+
func (c *URLCmd) Run(*pw.Context) error {
35+
reader := bufio.NewReader(os.Stdin)
36+
37+
scheme, err := prompt(reader, "Database type (postgres, mysql, sqlite)", "postgres")
38+
if err != nil {
39+
return err
40+
}
41+
42+
switch scheme {
43+
case "sqlite":
44+
return c.sqliteURL(reader)
45+
case "postgres", "mysql":
46+
return c.networkURL(reader, scheme)
47+
default:
48+
return fmt.Errorf("unsupported database type: %s", scheme)
49+
}
50+
}
51+
52+
func (c *URLCmd) sqliteURL(reader *bufio.Reader) error {
53+
path, err := prompt(reader, "Database file path", "patchwork.db")
54+
if err != nil {
55+
return err
56+
}
57+
fmt.Printf("\nurl = %q\n", "sqlite://"+path)
58+
return nil
59+
}
60+
61+
func (c *URLCmd) networkURL(reader *bufio.Reader, scheme string) error {
62+
defaultPort := "5432"
63+
if scheme == "mysql" {
64+
defaultPort = "3306"
65+
}
66+
67+
host, err := prompt(reader, "Host", "localhost")
68+
if err != nil {
69+
return err
70+
}
71+
port, err := prompt(reader, "Port", defaultPort)
72+
if err != nil {
73+
return err
74+
}
75+
dbname, err := prompt(reader, "Database name", "patchwork")
76+
if err != nil {
77+
return err
78+
}
79+
user, err := prompt(reader, "Username", "patchwork")
80+
if err != nil {
81+
return err
82+
}
83+
password, err := promptPassword("Password: ")
84+
if err != nil {
85+
return err
86+
}
87+
88+
u := &url.URL{
89+
Scheme: scheme,
90+
User: url.UserPassword(user, password),
91+
Host: host,
92+
Path: dbname,
93+
}
94+
if port != defaultPort {
95+
u.Host += ":" + port
96+
}
97+
98+
fmt.Printf("\nurl = %q\n", u.String())
99+
return nil
100+
}
101+
102+
func prompt(reader *bufio.Reader, label, defaultVal string) (string, error) {
103+
if defaultVal != "" {
104+
fmt.Fprintf(os.Stderr, "%s [%s]: ", label, defaultVal)
105+
} else {
106+
fmt.Fprintf(os.Stderr, "%s: ", label)
107+
}
108+
line, err := reader.ReadString('\n')
109+
if err != nil {
110+
return "", err
111+
}
112+
line = strings.TrimSpace(line)
113+
if line == "" {
114+
return defaultVal, nil
115+
}
116+
return line, nil
117+
}
118+
119+
func promptPassword(label string) (string, error) {
120+
fmt.Fprint(os.Stderr, label)
121+
password, err := term.ReadPassword(int(os.Stdin.Fd()))
122+
fmt.Fprintln(os.Stderr)
123+
if err != nil {
124+
return "", err
125+
}
126+
return string(password), nil
127+
}

‎cmd/pw/main.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ package main
88
import (
99
"context"
1010
"fmt"
11-
"os"
1211
"runtime"
12+
"strings"
1313

1414
"github.com/alecthomas/kong"
1515

1616
"github.com/getpatchwork/patchwork/cmd/pw/admin"
17+
pwcfg "github.com/getpatchwork/patchwork/cmd/pw/config"
1718
pwdb "github.com/getpatchwork/patchwork/cmd/pw/db"
1819
"github.com/getpatchwork/patchwork/cmd/pw/http"
1920
"github.com/getpatchwork/patchwork/cmd/pw/ingress"
@@ -28,7 +29,7 @@ type CLI struct {
2829

2930
ShowVersion VersionFlag `name:"version" help:"Print patchwork version and exit."`
3031

31-
GenCfg struct{} `cmd:"" name:"config" help:"Print default configuration to stdout."`
32+
Cfg pwcfg.CLI `cmd:"" name:"config" help:"Configuration utilities."`
3233
Admin admin.CLI `cmd:"" help:"Administration CLI."`
3334
DB pwdb.CLI `cmd:"" help:"Database management."`
3435
Ingress ingress.CLI `cmd:"" help:"Ingress SMTP/LMTP daemon."`
@@ -57,8 +58,12 @@ func main() {
5758

5859
k := config.Parse(&cli, "Patchwork runtime commands.")
5960

60-
if k.Command() == "config" {
61-
k.FatalIfErrorf(config.Generate(&cli.Config, os.Stdout))
61+
if strings.HasPrefix(k.Command(), "config") {
62+
k.FatalIfErrorf(k.Run(&pw.Context{
63+
Context: context.Background(),
64+
Config: &cli.Config,
65+
Version: Version,
66+
}))
6267
return
6368
}
6469

0 commit comments

Comments
 (0)