Skip to content

Commit 8edf35f

Browse files
Support 'bbctl delete -l'
1 parent 8ff78f2 commit 8edf35f

2 files changed

Lines changed: 65 additions & 17 deletions

File tree

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,11 @@ it from the Beeper servers (e.g. any rooms and ghost users it created).
166166
For official bridges, it will also delete the local data directory with the
167167
bridge config, database and python virtualenv (if applicable).
168168

169-
Note that deleting a bridge through the Beeper client settings will
170-
*not* delete the bridge database that is stored locally; you must
171-
delete that yourself, or use `bbctl delete` instead. The bridge
172-
databases are stored in `~/.local/share/bbctl/prod` by default.
173-
However, note that if you use any option that causes the bridge
174-
database to be stored in a separate location, such as `-l` which
175-
stores it in the current working directory, then `bbctl delete` will
176-
*not* delete the bridge database, and you will again have to delete it
177-
manually.
178-
179-
If you later re-add a self-hosted bridge after deleting it but not
180-
deleting the local database, you should expect errors, as the bridge
181-
will have been removed from Matrix rooms that it thinks it is a member
182-
of.
169+
If you ran the bridge using `bbctl run -l`, you should delete it using
170+
`bbctl delete -l` to ensure that the relevant bridge config is
171+
deleted.
172+
173+
If you later re-add a self-hosted bridge after deleting it from the
174+
Beeper servers but not deleting the local database, you should expect
175+
errors, as the bridge will have been removed from Matrix rooms that it
176+
thinks it is a member of.

cmd/bbctl/delete.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"io/fs"
77
"os"
8+
"path"
89
"path/filepath"
10+
"strings"
911

1012
"github.com/AlecAivazis/survey/v2"
1113
"github.com/fatih/color"
@@ -23,6 +25,12 @@ var deleteCommand = &cli.Command{
2325
Action: deleteBridge,
2426
Before: RequiresAuth,
2527
Flags: []cli.Flag{
28+
&cli.BoolFlag{
29+
Name: "local-dev",
30+
Aliases: []string{"l"},
31+
Usage: "Delete the bridge database and config from your current working directory. Useful for developing bridges.",
32+
EnvVars: []string{"BEEPER_BRIDGE_LOCAL"},
33+
},
2634
&cli.BoolFlag{
2735
Name: "force",
2836
Aliases: []string{"f"},
@@ -43,6 +51,18 @@ func deleteBridge(ctx *cli.Context) error {
4351
} else if bridge == "hungryserv" {
4452
return UserError{"You really shouldn't do that"}
4553
}
54+
var err error
55+
dataDir := GetEnvConfig(ctx).BridgeDataDir
56+
var bridgeDir string
57+
localDev := ctx.Bool("local-dev")
58+
if localDev {
59+
bridgeDir, err = os.Getwd()
60+
if err != nil {
61+
return fmt.Errorf("failed to get working directory: %w", err)
62+
}
63+
} else {
64+
bridgeDir = filepath.Join(dataDir, bridge)
65+
}
4666
homeserver := ctx.String("homeserver")
4767
accessToken := GetEnvConfig(ctx).AccessToken
4868
if !ctx.Bool("force") {
@@ -60,7 +80,7 @@ func deleteBridge(ctx *cli.Context) error {
6080
}
6181

6282
var confirmation bool
63-
err := survey.AskOne(&survey.Confirm{Message: fmt.Sprintf("Are you sure you want to permanently delete %s?", bridge)}, &confirmation)
83+
err = survey.AskOne(&survey.Confirm{Message: fmt.Sprintf("Are you sure you want to permanently delete %s?", bridge)}, &confirmation)
6484
if err != nil {
6585
return err
6686
} else if !confirmation {
@@ -71,12 +91,46 @@ func deleteBridge(ctx *cli.Context) error {
7191
return fmt.Errorf("error deleting bridge: %w", err)
7292
}
7393
fmt.Println("Started deleting bridge")
74-
bridgeDir := filepath.Join(GetEnvConfig(ctx).BridgeDataDir, bridge)
75-
err = os.RemoveAll(bridgeDir)
94+
err = deleteLocalBridgeData(bridgeDir, !localDev)
7695
if err != nil && !errors.Is(err, fs.ErrNotExist) {
7796
log.Printf("Failed to delete [magenta]%s[reset]: [red]%v[reset]", bridgeDir, err)
7897
} else {
7998
log.Printf("Deleted local bridge data from [magenta]%s[reset]", bridgeDir)
8099
}
81100
return nil
82101
}
102+
103+
func isLocalBridgeFile(name string) bool {
104+
if name == "config.yaml" {
105+
return true
106+
}
107+
if strings.HasSuffix(name, ".db") {
108+
return true
109+
}
110+
if strings.HasSuffix(name, ".db-shm") {
111+
return true
112+
}
113+
if strings.HasSuffix(name, ".db-wal") {
114+
return true
115+
}
116+
return false
117+
}
118+
119+
func deleteLocalBridgeData(bridgeDir string, deleteWholeDir bool) error {
120+
if deleteWholeDir {
121+
return os.RemoveAll(bridgeDir)
122+
}
123+
items, err := os.ReadDir(bridgeDir)
124+
if err != nil {
125+
return err
126+
}
127+
for _, item := range items {
128+
if isLocalBridgeFile(item.Name()) {
129+
err := os.Remove(path.Join(bridgeDir, item.Name()))
130+
if err != nil {
131+
return err
132+
}
133+
}
134+
}
135+
return nil
136+
}

0 commit comments

Comments
 (0)