Skip to content

Commit c2fe7ea

Browse files
committed
feat(logs): improve output for server logs
1 parent fc25893 commit c2fe7ea

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

internal/cmd/server/log/log.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8484
log := resp.GetOutput()
8585
lines := strings.Split(log, "\n")
8686

87-
if len(lines) > int(*model.Length) {
88-
// Truncate output and show most recent logs
89-
start := len(lines) - int(*model.Length)
90-
return outputResult(params.Printer, model.OutputFormat, serverLabel, strings.Join(lines[start:], "\n"))
87+
maxLines := int(*model.Length)
88+
if len(lines) <= maxLines {
89+
return outputResult(params.Printer, serverLabel, lines)
9190
}
9291

93-
return outputResult(params.Printer, model.OutputFormat, serverLabel, log)
92+
recentLogs := lines[len(lines)-maxLines:]
93+
return outputResult(params.Printer, serverLabel, recentLogs)
9494
},
9595
}
9696
configureFlags(cmd)
@@ -131,9 +131,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
131131
return apiClient.GetServerLog(ctx, model.ProjectId, model.ServerId)
132132
}
133133

134-
func outputResult(p *print.Printer, outputFormat, serverLabel, log string) error {
135-
return p.OutputResult(outputFormat, log, func() error {
136-
p.Outputf("Log for server %q\n%s", serverLabel, log)
137-
return nil
138-
})
134+
func outputResult(p *print.Printer, serverLabel string, logLines []string) error {
135+
p.Outputf("Log for server %q\n", serverLabel)
136+
for _, line := range logLines {
137+
// Skip empty lines
138+
if strings.TrimSpace(line) == "" {
139+
continue
140+
}
141+
p.Outputln(line)
142+
}
143+
144+
return nil
139145
}

internal/cmd/server/log/log_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ func TestBuildRequest(t *testing.T) {
178178

179179
func TestOutputResult(t *testing.T) {
180180
type args struct {
181-
outputFormat string
182-
serverLabel string
183-
log string
181+
serverLabel string
182+
logLines []string
184183
}
185184
tests := []struct {
186185
name string
@@ -197,7 +196,7 @@ func TestOutputResult(t *testing.T) {
197196
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
198197
for _, tt := range tests {
199198
t.Run(tt.name, func(t *testing.T) {
200-
if err := outputResult(p, tt.args.outputFormat, tt.args.serverLabel, tt.args.log); (err != nil) != tt.wantErr {
199+
if err := outputResult(p, tt.args.serverLabel, tt.args.logLines); (err != nil) != tt.wantErr {
201200
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
202201
}
203202
})

0 commit comments

Comments
 (0)