@@ -20,10 +20,14 @@ import (
20
20
"fmt"
21
21
"os"
22
22
"strings"
23
-
23
+ "text/tabwriter"
24
+ "time"
25
+ "strconv"
24
26
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
25
27
"github.com/litmuschaos/litmusctl/pkg/utils"
28
+ "github.com/manifoldco/promptui"
26
29
"github.com/spf13/cobra"
30
+
27
31
)
28
32
29
33
@@ -58,7 +62,57 @@ var ListChaosEnvironmentCmd = &cobra.Command{
58
62
os .Exit (1 )
59
63
}
60
64
}
61
- utils .PrintInJsonFormat (environmentList .Data )
65
+ environmentListData := environmentList .Data .ListEnvironmentDetails .Environments
66
+
67
+ itemsPerPage := 5
68
+ page := 1
69
+ totalEnvironments := len (environmentListData )
70
+
71
+ writer := tabwriter .NewWriter (os .Stdout , 30 , 8 , 0 ,'\t' , tabwriter .AlignRight )
72
+ utils .White_B .Fprintln (writer , "CHAOS ENVIRONMENT ID\t CHAOS ENVIRONMENT NAME\t CREATED AT\t CREATED BY" )
73
+ for {
74
+ writer .Flush ()
75
+ // calculating the start and end indices for the current page
76
+ start := (page - 1 ) * itemsPerPage
77
+ if start >= totalEnvironments {
78
+ writer .Flush ()
79
+ utils .Red .Println ("No more environments to display" )
80
+ break
81
+ }
82
+ end := start + itemsPerPage
83
+ if end > totalEnvironments {
84
+ end = totalEnvironments
85
+
86
+ }
87
+ for _ , environment := range environmentListData [start :end ] {
88
+ intTime , err := strconv .ParseInt (environment .CreatedAt , 10 , 64 )
89
+ if err != nil {
90
+ fmt .Println ("Error converting CreatedAt to int64:" , err )
91
+ continue
92
+ }
93
+ humanTime := time .Unix (intTime , 0 )
94
+ utils .White .Fprintln (
95
+ writer ,
96
+ environment .EnvironmentID + "\t " + environment .Name + "\t " + humanTime .String ()+ "\t " + environment .CreatedBy .Username ,
97
+ )
98
+ }
99
+ writer .Flush ()
100
+ // Check if it's the last item or if user wants to see more
101
+ paginationPrompt := promptui.Prompt {
102
+ Label : "Press Enter to show more environments (or type 'q' to quit)" ,
103
+ AllowEdit : true ,
104
+ Default : "" ,
105
+ }
106
+
107
+ userInput , err := paginationPrompt .Run ()
108
+ utils .PrintError (err )
109
+
110
+ if userInput == "q" {
111
+ break
112
+ }
113
+ // Move to the next page
114
+ page ++
115
+ }
62
116
},
63
117
64
118
0 commit comments