diff --git a/internal/cmd/server/log/log.go b/internal/cmd/server/log/log.go index a211a44c1..0dc0b8bfa 100644 --- a/internal/cmd/server/log/log.go +++ b/internal/cmd/server/log/log.go @@ -85,13 +85,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command { log := resp.GetOutput() lines := strings.Split(log, "\n") - if len(lines) > int(*model.Length) { - // Truncate output and show most recent logs - start := len(lines) - int(*model.Length) - return outputResult(params.Printer, model.OutputFormat, serverLabel, strings.Join(lines[start:], "\n")) + maxLines := int(*model.Length) + if len(lines) <= maxLines { + return outputResult(params.Printer, serverLabel, lines) } - return outputResult(params.Printer, model.OutputFormat, serverLabel, log) + recentLogs := lines[len(lines)-maxLines:] + return outputResult(params.Printer, serverLabel, recentLogs) }, } configureFlags(cmd) @@ -132,9 +132,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli return apiClient.GetServerLog(ctx, model.ProjectId, model.Region, model.ServerId) } -func outputResult(p *print.Printer, outputFormat, serverLabel, log string) error { - return p.OutputResult(outputFormat, log, func() error { - p.Outputf("Log for server %q\n%s", serverLabel, log) - return nil - }) +func outputResult(p *print.Printer, serverLabel string, logLines []string) error { + p.Outputf("Log for server %q\n", serverLabel) + for _, line := range logLines { + // Skip empty lines + if strings.TrimSpace(line) == "" { + continue + } + p.Outputln(line) + } + + return nil } diff --git a/internal/cmd/server/log/log_test.go b/internal/cmd/server/log/log_test.go index 87375530f..d1a2d0d1f 100644 --- a/internal/cmd/server/log/log_test.go +++ b/internal/cmd/server/log/log_test.go @@ -184,9 +184,8 @@ func TestBuildRequest(t *testing.T) { func TestOutputResult(t *testing.T) { type args struct { - outputFormat string - serverLabel string - log string + serverLabel string + logLines []string } tests := []struct { name string @@ -203,7 +202,7 @@ func TestOutputResult(t *testing.T) { p.Cmd = NewCmd(&types.CmdParams{Printer: p}) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.log); (err != nil) != tt.wantErr { + if err := outputResult(p, tt.args.serverLabel, tt.args.logLines); (err != nil) != tt.wantErr { t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) } })