Skip to content

Commit

Permalink
Refactor updating cached info from whoami
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 6, 2023
1 parent bd0bc2c commit 0e62068
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
1 change: 0 additions & 1 deletion cmd/bbctl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func deleteBridge(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to get whoami: %w", err)
}
SaveHungryURL(ctx, whoami.UserInfo.HungryURL)
bridgeInfo, ok := whoami.User.Bridges[bridge]
if !ok {
return UserError{fmt.Sprintf("You don't have a %s bridge.", color.CyanString(bridge))}
Expand Down
1 change: 0 additions & 1 deletion cmd/bbctl/login-email.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func doMatrixLogin(ctx *cli.Context, req *mautrix.ReqLogin, whoami *beeperapi.Re
return fmt.Errorf("failed to get user details: %w", err)
}
}
fmt.Printf("Found own bridge cluster ID: %s\n", whoami.UserInfo.BridgeClusterID)
envCfg := GetEnvConfig(ctx)
envCfg.ClusterID = whoami.UserInfo.BridgeClusterID
envCfg.Username = whoami.UserInfo.Username
Expand Down
9 changes: 1 addition & 8 deletions cmd/bbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,10 @@ func prepareApp(ctx *cli.Context) error {
if envConfig.HasCredentials() {
if envConfig.HungryAddress == "" || envConfig.ClusterID == "" || envConfig.Username == "" {
log.Printf("Fetching whoami to fill missing env config details")
whoami, err := getCachedWhoami(ctx)
_, err = getCachedWhoami(ctx)
if err != nil {
return fmt.Errorf("failed to get whoami: %w", err)
}
envConfig.Username = whoami.UserInfo.Username
envConfig.ClusterID = whoami.UserInfo.BridgeClusterID
envConfig.HungryAddress = whoami.UserInfo.HungryURL
err = cfg.Save()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, color.RedString("Failed to save config: "+err.Error()))
}
}
homeserver := ctx.String("homeserver")
ctx.Context = context.WithValue(ctx.Context, contextKeyMatrixClient, NewMatrixAPI(homeserver, envConfig.Username, envConfig.AccessToken))
Expand Down
5 changes: 1 addition & 4 deletions cmd/bbctl/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ type RegisterJSON struct {
}

func doRegisterBridge(ctx *cli.Context, bridge, bridgeType string, onlyGet bool) (*RegisterJSON, error) {
homeserver := ctx.String("homeserver")
envConfig := GetEnvConfig(ctx)
whoami, err := beeperapi.Whoami(homeserver, envConfig.AccessToken)
whoami, err := getCachedWhoami(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get whoami: %w", err)
}
SaveHungryURL(ctx, whoami.UserInfo.HungryURL)
bridgeInfo, ok := whoami.User.Bridges[bridge]
if ok && !bridgeInfo.BridgeState.IsSelfHosted && !ctx.Bool("force") {
return nil, UserError{fmt.Sprintf("Your %s bridge is not self-hosted.", color.CyanString(bridge))}
Expand Down
24 changes: 22 additions & 2 deletions cmd/bbctl/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/beeper/bridge-manager/api/beeperapi"
"github.com/beeper/bridge-manager/cli/hyper"
"github.com/beeper/bridge-manager/log"
)

var whoamiCommand = &cli.Command{
Expand Down Expand Up @@ -154,10 +155,30 @@ func getCachedWhoami(ctx *cli.Context) (*beeperapi.RespWhoami, error) {
if cachedWhoami != nil {
return cachedWhoami, nil
}
resp, err := beeperapi.Whoami(ctx.String("homeserver"), GetEnvConfig(ctx).AccessToken)
ec := GetEnvConfig(ctx)
resp, err := beeperapi.Whoami(ctx.String("homeserver"), ec.AccessToken)
if err != nil {
return nil, err
}
changed := false
if ec.Username != resp.UserInfo.Username {
ec.Username = resp.UserInfo.Username
changed = true
}
if ec.ClusterID != resp.UserInfo.BridgeClusterID {
ec.ClusterID = resp.UserInfo.BridgeClusterID
changed = true
}
if ec.HungryAddress != resp.UserInfo.HungryURL {
ec.HungryAddress = resp.UserInfo.HungryURL
changed = true
}
if changed {
err = GetConfig(ctx).Save()
if err != nil {
log.Printf("Failed to save config after updating: %v", err)
}
}
cachedWhoami = resp
return resp, nil
}
Expand All @@ -167,7 +188,6 @@ func whoamiFunction(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to get whoami: %w", err)
}
SaveHungryURL(ctx, whoami.UserInfo.HungryURL)
if ctx.Bool("raw") {
data, err := json.MarshalIndent(whoami, "", " ")
if err != nil {
Expand Down

0 comments on commit 0e62068

Please sign in to comment.