Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions internal/cmd/server/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func NewCmd(params *params.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)
Expand Down Expand Up @@ -131,9 +131,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return apiClient.GetServerLog(ctx, model.ProjectId, 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
}
7 changes: 3 additions & 4 deletions internal/cmd/server/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,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
Expand All @@ -197,7 +196,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&params.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)
}
})
Expand Down
Loading