Skip to content

Commit

Permalink
use structured logs consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
sclevine committed Nov 22, 2024
1 parent fa19c18 commit ab388e0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions tool/teleport-update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const (
var plog = logutils.NewPackageLogger(teleport.ComponentKey, teleport.ComponentUpdater)

func main() {
if err := Run(os.Args[1:]); err != nil {
libutils.FatalError(err)
if code := Run(os.Args[1:]); code != 0 {
os.Exit(code)
}
}

Expand All @@ -88,7 +88,7 @@ type cliConfig struct {
SelfSetup bool
}

func Run(args []string) error {
func Run(args []string) int {
var ccfg cliConfig
ctx := context.Background()
ctx, _ = signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
Expand Down Expand Up @@ -155,12 +155,13 @@ func Run(args []string) error {
command, err := app.Parse(args)
if err != nil {
app.Usage(args)
return trace.Wrap(err)
libutils.FatalError(err)
}
// Logging must be configured as early as possible to ensure all log
// message are formatted correctly.
if err := setupLogger(ccfg.Debug, ccfg.LogFormat); err != nil {
return trace.Errorf("failed to set up logger")
plog.ErrorContext(ctx, "Failed to set up logger.", "error", err)
return 1
}

switch command {
Expand Down Expand Up @@ -192,8 +193,14 @@ func Run(args []string) error {
// This should only happen when there's a missing switch case above.
err = trace.Errorf("command %q not configured", command)
}

return err
if errors.Is(err, autoupdate.ErrNotSupported) {
return autoupdate.CodeNotSupported
}
if err != nil {
plog.ErrorContext(ctx, "Command failed.", "error", err)
return 1
}
return 0
}

func setupLogger(debug bool, format string) error {
Expand Down Expand Up @@ -390,7 +397,7 @@ func cmdSetup(ctx context.Context, ccfg *cliConfig) error {
err = cluster.Setup(ctx, plog, ccfg.LinkDir, ccfg.DataDir)
if errors.Is(err, autoupdate.ErrNotSupported) {
plog.WarnContext(ctx, "Not enabling systemd service because systemd is not running.")
os.Exit(autoupdate.CodeNotSupported)
return autoupdate.ErrNotSupported
}
if err != nil {
return trace.Errorf("failed to setup teleport-update service: %w", err)
Expand Down

0 comments on commit ab388e0

Please sign in to comment.