Skip to content

Commit 0958c3b

Browse files
committed
refactor: rework cli logging
1 parent e214d6d commit 0958c3b

5 files changed

Lines changed: 32 additions & 27 deletions

File tree

cmd/tinyauth/create_user.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"strings"
77

88
"charm.land/huh/v2"
9-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
109
"github.com/tinyauthapp/paerser/cli"
10+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
1111
"golang.org/x/crypto/bcrypt"
1212
)
1313

@@ -40,7 +40,8 @@ func createUserCmd() *cli.Command {
4040
Configuration: tCfg,
4141
Resources: loaders,
4242
Run: func(_ []string) error {
43-
tlog.NewSimpleLogger().Init()
43+
log := logger.NewLogger().WithSimpleConfig()
44+
log.Init()
4445

4546
if tCfg.Interactive {
4647
form := huh.NewForm(
@@ -73,7 +74,7 @@ func createUserCmd() *cli.Command {
7374
return errors.New("username and password cannot be empty")
7475
}
7576

76-
tlog.App.Info().Str("username", tCfg.Username).Msg("Creating user")
77+
log.App.Info().Str("username", tCfg.Username).Msg("Creating user")
7778

7879
passwd, err := bcrypt.GenerateFromPassword([]byte(tCfg.Password), bcrypt.DefaultCost)
7980
if err != nil {
@@ -86,7 +87,7 @@ func createUserCmd() *cli.Command {
8687
passwdStr = strings.ReplaceAll(passwdStr, "$", "$$")
8788
}
8889

89-
tlog.App.Info().Str("user", fmt.Sprintf("%s:%s", tCfg.Username, passwdStr)).Msg("User created")
90+
log.App.Info().Str("user", fmt.Sprintf("%s:%s", tCfg.Username, passwdStr)).Msg("User created")
9091

9192
return nil
9293
},

cmd/tinyauth/generate_totp.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88

99
"github.com/tinyauthapp/tinyauth/internal/utils"
10-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
10+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
1111

1212
"charm.land/huh/v2"
1313
"github.com/mdp/qrterminal/v3"
@@ -40,7 +40,8 @@ func generateTotpCmd() *cli.Command {
4040
Configuration: tCfg,
4141
Resources: loaders,
4242
Run: func(_ []string) error {
43-
tlog.NewSimpleLogger().Init()
43+
log := logger.NewLogger().WithSimpleConfig()
44+
log.Init()
4445

4546
if tCfg.Interactive {
4647
form := huh.NewForm(
@@ -88,9 +89,9 @@ func generateTotpCmd() *cli.Command {
8889

8990
secret := key.Secret()
9091

91-
tlog.App.Info().Str("secret", secret).Msg("Generated TOTP secret")
92+
log.App.Info().Str("secret", secret).Msg("Generated TOTP secret")
9293

93-
tlog.App.Info().Msg("Generated QR code")
94+
log.App.Info().Msg("Generated QR code")
9495

9596
config := qrterminal.Config{
9697
Level: qrterminal.L,
@@ -109,7 +110,7 @@ func generateTotpCmd() *cli.Command {
109110
user.Password = strings.ReplaceAll(user.Password, "$", "$$")
110111
}
111112

112-
tlog.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TOTPSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.")
113+
log.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TOTPSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.")
113114

114115
return nil
115116
},

cmd/tinyauth/healthcheck.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"os"
1010
"time"
1111

12-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
1312
"github.com/tinyauthapp/paerser/cli"
13+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
1414
)
1515

1616
type healthzResponse struct {
@@ -26,7 +26,8 @@ func healthcheckCmd() *cli.Command {
2626
Resources: nil,
2727
AllowArg: true,
2828
Run: func(args []string) error {
29-
tlog.NewSimpleLogger().Init()
29+
log := logger.NewLogger().WithSimpleConfig()
30+
log.Init()
3031

3132
srvAddr := os.Getenv("TINYAUTH_SERVER_ADDRESS")
3233
if srvAddr == "" {
@@ -48,7 +49,7 @@ func healthcheckCmd() *cli.Command {
4849
return errors.New("Could not determine app URL")
4950
}
5051

51-
tlog.App.Info().Str("app_url", appUrl).Msg("Performing health check")
52+
log.App.Info().Str("app_url", appUrl).Msg("Performing health check")
5253

5354
client := http.Client{
5455
Timeout: 30 * time.Second,
@@ -86,7 +87,7 @@ func healthcheckCmd() *cli.Command {
8687
return fmt.Errorf("failed to decode response: %w", err)
8788
}
8889

89-
tlog.App.Info().Interface("response", healthResp).Msg("Tinyauth is healthy")
90+
log.App.Info().Interface("response", healthResp).Msg("Tinyauth is healthy")
9091

9192
return nil
9293
},

cmd/tinyauth/verify_user.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66

77
"github.com/tinyauthapp/tinyauth/internal/utils"
8-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
8+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
99

1010
"charm.land/huh/v2"
1111
"github.com/pquerna/otp/totp"
@@ -44,7 +44,8 @@ func verifyUserCmd() *cli.Command {
4444
Configuration: tCfg,
4545
Resources: loaders,
4646
Run: func(_ []string) error {
47-
tlog.NewSimpleLogger().Init()
47+
log := logger.NewLogger().WithSimpleConfig()
48+
log.Init()
4849

4950
if tCfg.Interactive {
5051
form := huh.NewForm(
@@ -97,9 +98,9 @@ func verifyUserCmd() *cli.Command {
9798

9899
if user.TOTPSecret == "" {
99100
if tCfg.Totp != "" {
100-
tlog.App.Warn().Msg("User does not have TOTP secret")
101+
log.App.Warn().Msg("User does not have TOTP secret")
101102
}
102-
tlog.App.Info().Msg("User verified")
103+
log.App.Info().Msg("User verified")
103104
return nil
104105
}
105106

@@ -109,7 +110,7 @@ func verifyUserCmd() *cli.Command {
109110
return fmt.Errorf("TOTP code incorrect")
110111
}
111112

112-
tlog.App.Info().Msg("User verified")
113+
log.App.Info().Msg("User verified")
113114

114115
return nil
115116
},

internal/bootstrap/app_bootstrap.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,18 @@ func (app *BootstrapApp) Setup() error {
249249
}()
250250

251251
// monitor cancellation and server errors
252-
select {
253-
case <-app.ctx.Done():
254-
app.log.App.Debug().Msg("Shutting down application")
255-
return nil
256-
case err := <-errChan:
257-
if err != nil {
258-
return fmt.Errorf("server error: %w", err)
252+
for {
253+
select {
254+
case <-app.ctx.Done():
255+
app.log.App.Debug().Msg("Oh, seems like I got to shutdown, bye!")
256+
app.db.Close()
257+
return nil
258+
case err := <-errChan:
259+
if err != nil {
260+
return fmt.Errorf("server error: %w", err)
261+
}
259262
}
260263
}
261-
262-
return nil
263264
}
264265

265266
func (app *BootstrapApp) serveHTTP() error {

0 commit comments

Comments
 (0)