@@ -3,14 +3,15 @@ package main
33
44import (
55 "fmt"
6+ "log/slog"
67 "os"
78 "reflect"
89
910 grpccodes "google.golang.org/grpc/codes"
1011 grpcstatus "google.golang.org/grpc/status"
1112
13+ "gitlab.com/gitlab-org/labkit/fields"
1214 "gitlab.com/gitlab-org/labkit/fips"
13- "gitlab.com/gitlab-org/labkit/log"
1415
1516 shellCmd "gitlab.com/gitlab-org/gitlab-shell/v14/cmd/gitlab-shell/command"
1617 "gitlab.com/gitlab-org/gitlab-shell/v14/internal/command"
2930 BuildTime = "19700101.000000" // Set at build time in the Makefile
3031)
3132
32- func main () {
33+ func run () int {
3334 command .CheckForVersionFlag (os .Args , Version , BuildTime )
3435
3536 readWriter := & readwriter.ReadWriter {
@@ -41,47 +42,51 @@ func main() {
4142 executable , err := executable .New (executable .GitlabShell )
4243 if err != nil {
4344 _ , _ = fmt .Fprintln (readWriter .ErrOut , "Failed to determine executable, exiting" )
44- os . Exit ( 1 )
45+ return 1
4546 }
4647
4748 config , err := config .NewFromDirExternal (executable .RootDir )
4849 if err != nil {
4950 _ , _ = fmt .Fprintln (readWriter .ErrOut , "Failed to read config, exiting:" , err )
50- os . Exit ( 1 )
51+ return 1
5152 }
5253
53- logCloser := logger .Configure (config )
54+ logCloser := logger .ConfigureLogger (config )
55+ if logCloser != nil {
56+ defer logCloser .Close () //nolint:errcheck
57+ }
5458
5559 env := sshenv .NewFromEnv ()
5660 cmd , err := shellCmd .New (os .Args [1 :], env , config , readWriter )
5761 if err != nil {
5862 // For now this could happen if `SSH_CONNECTION` is not set on
5963 // the environment
6064 _ , _ = fmt .Fprintf (readWriter .ErrOut , "%v\n " , err )
61- _ = logCloser .Close ()
62- os .Exit (1 )
65+ return 1
6366 }
6467
6568 ctx , finished := command .Setup (executable .Name , config )
6669
6770 config .GitalyClient .InitSidechannelRegistry (ctx )
6871
6972 cmdName := reflect .TypeOf (cmd ).String ()
70- ctxlog := log .ContextLogger (ctx )
71- ctxlog .WithFields (log.Fields {"env" : env , "command" : cmdName }).Info ("gitlab-shell: main: executing command" )
73+ slog .InfoContext (ctx , "gitlab-shell: main: executing command" , slog .Any ("env" , env ), slog .String ("command" , cmdName ))
7274 fips .Check ()
7375
7476 if _ , err := cmd .Execute (ctx ); err != nil {
75- ctxlog . WithError ( err ). Warn ( "gitlab-shell: main: command execution failed" )
77+ slog . WarnContext ( ctx , "gitlab-shell: main: command execution failed" , slog . String ( fields . ErrorMessage , err . Error ()) )
7678 if grpcstatus .Convert (err ).Code () != grpccodes .Internal {
7779 console .DisplayWarningMessage (err .Error (), readWriter .ErrOut )
7880 }
7981 finished ()
80- _ = logCloser .Close ()
81- os .Exit (1 )
82+ return 1
8283 }
8384
84- ctxlog . Info ( "gitlab-shell: main: command executed successfully" )
85+ slog . InfoContext ( ctx , "gitlab-shell: main: command executed successfully" )
8586 finished ()
86- _ = logCloser .Close ()
87+ return 0
88+ }
89+
90+ func main () {
91+ os .Exit (run ())
8792}
0 commit comments