mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 08:10:34 +00:00
feat(internal/client): support dynamic log level
This commit is contained in:
parent
fab4912a0d
commit
27b6446893
@ -10,6 +10,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
"koti.casa/numenor-labs/dsfx/internal/lib/crypto/identity"
|
"koti.casa/numenor-labs/dsfx/internal/lib/crypto/identity"
|
||||||
"koti.casa/numenor-labs/dsfx/internal/lib/disk"
|
"koti.casa/numenor-labs/dsfx/internal/lib/disk"
|
||||||
@ -28,6 +29,23 @@ const (
|
|||||||
type Conf struct {
|
type Conf struct {
|
||||||
// Directories
|
// Directories
|
||||||
ConfigDir string
|
ConfigDir string
|
||||||
|
LogLevel string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SlogLevel returns the appropriate slog.Level based on the LogLevel string.
|
||||||
|
func (c Conf) SlogLevel() slog.Level {
|
||||||
|
switch c.LogLevel {
|
||||||
|
case "debug":
|
||||||
|
return slog.LevelDebug
|
||||||
|
case "info":
|
||||||
|
return slog.LevelInfo
|
||||||
|
case "warn":
|
||||||
|
return slog.LevelWarn
|
||||||
|
case "error":
|
||||||
|
return slog.LevelError
|
||||||
|
default:
|
||||||
|
return slog.LevelInfo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfigFromSystem(sys system.System) Conf {
|
func loadConfigFromSystem(sys system.System) Conf {
|
||||||
@ -38,6 +56,9 @@ func loadConfigFromSystem(sys system.System) Conf {
|
|||||||
c.ConfigDir = DefaultConfigDir
|
c.ConfigDir = DefaultConfigDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// defaults are handled by Conf.SlogLevel.
|
||||||
|
c.LogLevel = sys.GetEnv("DSFXCTL_LOG_LEVEL")
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +96,7 @@ func (a *Client) Run(ctx context.Context) error {
|
|||||||
|
|
||||||
opts := &slog.HandlerOptions{
|
opts := &slog.HandlerOptions{
|
||||||
AddSource: false,
|
AddSource: false,
|
||||||
Level: slog.LevelInfo,
|
Level: a.conf.SlogLevel(),
|
||||||
}
|
}
|
||||||
logger := slog.New(slog.NewTextHandler(a.system.Stdout(), opts))
|
logger := slog.New(slog.NewTextHandler(a.system.Stdout(), opts))
|
||||||
|
|
||||||
@ -114,13 +135,16 @@ func (a *Client) Run(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
testConnection(ctx, id, laddr, raddrRaw)
|
testConnection(ctx, id, laddr, raddrRaw)
|
||||||
|
return nil
|
||||||
|
case "identity":
|
||||||
|
pubKey := identity.ToPublicKey(id)
|
||||||
|
logger.InfoContext(ctx, "identity", slog.String("publicKey", base64.StdEncoding.EncodeToString(pubKey)))
|
||||||
|
return nil
|
||||||
case "":
|
case "":
|
||||||
return errors.New("no command provided")
|
return errors.New("no command provided")
|
||||||
default:
|
default:
|
||||||
return errors.New("unknown command: " + a.system.Arg(0))
|
return errors.New("unknown command: " + a.system.Arg(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testConnection(ctx context.Context, id ed25519.PrivateKey, laddr *network.Addr, raddrRaw string) {
|
func testConnection(ctx context.Context, id ed25519.PrivateKey, laddr *network.Addr, raddrRaw string) {
|
||||||
@ -138,6 +162,8 @@ func testConnection(ctx context.Context, id ed25519.PrivateKey, laddr *network.A
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
logger.InfoContext(ctx, "connected to server", slog.String("remote", raddr.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadIdentity ...
|
// loadIdentity ...
|
||||||
@ -159,6 +185,9 @@ func (c *Client) loadIdentity() (ed25519.PrivateKey, error) {
|
|||||||
// hasKeyFile ...
|
// hasKeyFile ...
|
||||||
func (c *Client) hasKeyFile() (bool, error) {
|
func (c *Client) hasKeyFile() (bool, error) {
|
||||||
f, err := c.configScope.Open("ed25519.key")
|
f, err := c.configScope.Open("ed25519.key")
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return false, nil // Key file does not exist
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user