Skip to content

Commit 9748046

Browse files
committed
readd test; handle non-tty in bubble tea
Signed-off-by: Darkhood148 <ujjwal.sharma9999999@gmail.com>
1 parent 844a9ae commit 9748046

4 files changed

Lines changed: 35 additions & 18 deletions

File tree

cmd/harbor/root/context/list.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/goharbor/harbor-cli/pkg/views/context/list"
2121
"github.com/sirupsen/logrus"
2222
"github.com/spf13/cobra"
23+
"github.com/spf13/viper"
2324
)
2425

2526
func ListContextCommand() *cobra.Command {
@@ -34,12 +35,23 @@ func ListContextCommand() *cobra.Command {
3435
logrus.Errorf("Error occurred: %v", err)
3536
return
3637
}
37-
var cxlist []api.ContextListView
38-
for _, cred := range config.Credentials {
39-
cx := api.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress}
40-
cxlist = append(cxlist, cx)
38+
39+
formatFlag := viper.GetString("output-format")
40+
if formatFlag != "" {
41+
// Use utils.PrintFormat if available
42+
err = utils.PrintFormat(config, formatFlag)
43+
if err != nil {
44+
logrus.Errorf("Failed to print config: %v", err)
45+
return
46+
}
47+
} else {
48+
var cxlist []api.ContextListView
49+
for _, cred := range config.Credentials {
50+
cx := api.ContextListView{Name: cred.Name, Username: cred.Username, Server: cred.ServerAddress}
51+
cxlist = append(cxlist, cx)
52+
}
53+
list.ListContexts(cxlist)
4154
}
42-
list.ListContexts(cxlist)
4355
},
4456
}
4557
return cmd

harbor-cli

17.6 MB
Binary file not shown.

pkg/views/context/list/view.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"os"
2020

21+
"golang.org/x/term"
22+
2123
"github.com/charmbracelet/bubbles/table"
2224
tea "github.com/charmbracelet/bubbletea"
2325
"github.com/goharbor/harbor-cli/pkg/api"
@@ -41,9 +43,14 @@ func ListContexts(contexts []api.ContextListView) {
4143
})
4244
}
4345

46+
var opts []tea.ProgramOption
47+
if !term.IsTerminal(int(os.Stdout.Fd())) {
48+
opts = append(opts, tea.WithoutRenderer(), tea.WithInput(nil))
49+
}
50+
4451
m := tablelist.NewModel(columns, rows, len(rows))
4552

46-
if _, err := tea.NewProgram(m).Run(); err != nil {
53+
if _, err := tea.NewProgram(m, opts...).Run(); err != nil {
4754
fmt.Println("Error running program:", err)
4855
os.Exit(1)
4956
}

test/e2e/config_cmd_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ func Test_ContextCmd(t *testing.T) {
3232
assert.Nil(t, err)
3333
}
3434

35-
// TODO: This test fails because it tries to open a TTY terminal which fails in GitHub workflows and dagger but works if you run it locally.
36-
// The underlying module bubbletea requires TTY to run.
37-
//func Test_ContextListCmd(t *testing.T) {
38-
// tempDir := t.TempDir()
39-
// data := Initialize(t, tempDir)
40-
// defer ConfigCleanup(t, data)
41-
// SetMockKeyring(t)
42-
// rootCmd := root.RootCmd()
43-
// rootCmd.SetArgs([]string{"context", "list"})
44-
// err := rootCmd.Execute()
45-
// assert.Nil(t, err)
46-
//}
35+
func Test_ContextListCmd(t *testing.T) {
36+
tempDir := t.TempDir()
37+
data := Initialize(t, tempDir)
38+
defer ConfigCleanup(t, data)
39+
SetMockKeyring(t)
40+
rootCmd := root.RootCmd()
41+
rootCmd.SetArgs([]string{"context", "list"})
42+
err := rootCmd.Execute()
43+
assert.Nil(t, err)
44+
}
4745

4846
func Test_ContextGetCmd_Success(t *testing.T) {
4947
tempDir := t.TempDir()

0 commit comments

Comments
 (0)