Skip to content

Commit

Permalink
Change default config path
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 1, 2023
1 parent 9651a59 commit 928e941
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions cmd/bbctl/authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"

"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/util"

"github.com/beeper/bridge-manager/log"
)

var envs = map[string]string{
Expand Down Expand Up @@ -78,6 +81,27 @@ func init() {
}
}

func migrateOldConfig(currentPath string) error {
baseConfigDir, err := os.UserConfigDir()
if err != nil {
panic(err)
}
newDefault := path.Join(baseConfigDir, "bbctl", "bbctl.json")
oldDefault := path.Join(baseConfigDir, "bbctl.json")
if currentPath != newDefault {
return nil
} else if _, err = os.Stat(oldDefault); err != nil {
return nil
} else if err = os.MkdirAll(filepath.Dir(newDefault), 0700); err != nil {
return err
} else if err = os.Rename(oldDefault, newDefault); err != nil {
return err
} else {
log.Printf("Moved config to new path (from %s to %s)", oldDefault, newDefault)
return nil
}
}

func loadConfig(path string) (ret *Config, err error) {
defer func() {
if ret == nil {
Expand Down Expand Up @@ -105,6 +129,10 @@ func loadConfig(path string) (ret *Config, err error) {
}
}()

err = migrateOldConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to move config to new path: %w", err)
}
file, err := os.Open(path)
if errors.Is(err, os.ErrNotExist) {
return &Config{}, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/bbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func getDefaultConfigPath() string {
if err != nil {
panic(err)
}
return path.Join(baseConfigDir, "bbctl.json")
return path.Join(baseConfigDir, "bbctl", "bbctl.json")
}

func prepareApp(ctx *cli.Context) error {
Expand Down

0 comments on commit 928e941

Please sign in to comment.