Skip to content

Commit 4f13f22

Browse files
committed
fix: format output for branches list
1 parent 07f6bde commit 4f13f22

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

internal/branches/list/list.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package list
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"strings"
78

89
"github.com/go-errors/errors"
@@ -19,27 +20,39 @@ func Run(ctx context.Context, fsys afero.Fs) error {
1920
return err
2021
}
2122

22-
table := `|ID|NAME|DEFAULT|GIT BRANCH|STATUS|CREATED AT (UTC)|UPDATED AT (UTC)|
23+
switch utils.OutputFormat.Value {
24+
case utils.OutputPretty:
25+
table := `|ID|NAME|DEFAULT|GIT BRANCH|STATUS|CREATED AT (UTC)|UPDATED AT (UTC)|
2326
|-|-|-|-|-|-|-|
2427
`
25-
for _, branch := range branches {
26-
gitBranch := " "
27-
if branch.GitBranch != nil {
28-
gitBranch = *branch.GitBranch
28+
for _, branch := range branches {
29+
gitBranch := " "
30+
if branch.GitBranch != nil {
31+
gitBranch = *branch.GitBranch
32+
}
33+
table += fmt.Sprintf(
34+
"|`%s`|`%s`|`%t`|`%s`|`%s`|`%s`|`%s`|\n",
35+
branch.Id,
36+
strings.ReplaceAll(branch.Name, "|", "\\|"),
37+
branch.IsDefault,
38+
strings.ReplaceAll(gitBranch, "|", "\\|"),
39+
branch.Status,
40+
utils.FormatTimestamp(branch.CreatedAt),
41+
utils.FormatTimestamp(branch.UpdatedAt),
42+
)
2943
}
30-
table += fmt.Sprintf(
31-
"|`%s`|`%s`|`%t`|`%s`|`%s`|`%s`|`%s`|\n",
32-
branch.Id,
33-
strings.ReplaceAll(branch.Name, "|", "\\|"),
34-
branch.IsDefault,
35-
strings.ReplaceAll(gitBranch, "|", "\\|"),
36-
branch.Status,
37-
utils.FormatTimestamp(branch.CreatedAt),
38-
utils.FormatTimestamp(branch.UpdatedAt),
39-
)
44+
return list.RenderTable(table)
45+
case utils.OutputToml:
46+
return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, struct {
47+
Branches []api.BranchResponse `toml:"branches"`
48+
}{
49+
Branches: branches,
50+
})
51+
case utils.OutputEnv:
52+
return errors.Errorf("--output env flag is not supported")
4053
}
4154

42-
return list.RenderTable(table)
55+
return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, branches)
4356
}
4457

4558
type BranchFilter func(api.BranchResponse) bool

0 commit comments

Comments
 (0)