Skip to content

Commit fbddf0e

Browse files
committed
Add log format flag
1 parent 3d2274e commit fbddf0e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pkg/cmd/cmd.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package cmd
22

33
import (
44
"context"
5+
"log/slog"
6+
"os"
57

68
"github.com/alecthomas/kong"
79
"github.com/cedws/sshgate/pkg/sshgate"
810
)
911

1012
type cli struct {
1113
ListenAddr string `help:"Address to listen on" default:":2222"`
14+
LogFormat string `help:"Log format"`
1215
Config string `help:"Path to JSON config file"`
1316

1417
Serve serveCmd `cmd:"" default:"1" help:"Start the server"`
@@ -18,7 +21,20 @@ type cli struct {
1821
type serveCmd struct{}
1922

2023
func (s *serveCmd) Run(c *cli) error {
21-
config, err := sshgate.Open(c.Config)
24+
var handler slog.Handler
25+
26+
switch c.LogFormat {
27+
case "json":
28+
handler = slog.NewJSONHandler(os.Stderr, nil)
29+
case "text":
30+
fallthrough
31+
default:
32+
handler = slog.NewTextHandler(os.Stderr, nil)
33+
}
34+
35+
slog.SetDefault(slog.New(handler))
36+
37+
config, err := sshgate.ReadConfig(c.Config)
2238
if err != nil {
2339
return err
2440
}

pkg/sshgate/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c Config) Validate() error {
4848
return nil
4949
}
5050

51-
func Open(path string) (*Config, error) {
51+
func ReadConfig(path string) (*Config, error) {
5252
data, err := os.ReadFile(path)
5353
if err != nil {
5454
return nil, fmt.Errorf("failed to read config file: %w", err)

0 commit comments

Comments
 (0)