Skip to content

Commit 1be4745

Browse files
refactor command get/list environment
Signed-off-by: Shivam Purohit <[email protected]>
1 parent 83fd657 commit 1be4745

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

pkg/apis/environment/environment.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"io/ioutil"
77
"net/http"
8-
98
models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
109
"github.com/litmuschaos/litmusctl/pkg/apis"
1110
"github.com/litmuschaos/litmusctl/pkg/types"
@@ -58,7 +57,6 @@ func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData
5857
if err != nil {
5958
return ListEnvironmentData{}, err
6059
}
61-
6260
resp, err := apis.SendRequest(
6361
apis.SendRequestParams{
6462
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
@@ -67,11 +65,9 @@ func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData
6765
query,
6866
string(types.Post),
6967
)
70-
7168
if err != nil {
7269
return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error())
7370
}
74-
7571
bodyBytes, err := ioutil.ReadAll(resp.Body)
7672
defer resp.Body.Close()
7773
if err != nil {

pkg/apis/environment/query.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const (
1515
listEnvironments(projectID: $projectID,request: $request){
1616
environments {
1717
environmentID
18+
name
19+
createdAt
20+
updatedAt
21+
createdBy{
22+
username
23+
}
1824
}
1925
}
2026
}`

pkg/cmd/get/environment.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"fmt"
2121
"os"
2222
"strings"
23+
"time"
24+
"text/tabwriter"
25+
"strconv"
2326
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
2427
"github.com/litmuschaos/litmusctl/pkg/utils"
2528
"github.com/spf13/cobra"
@@ -70,18 +73,33 @@ var ChaosEnvironmentCmd = &cobra.Command{
7073
os.Exit(1)
7174
}
7275
}
73-
environmentListData := environmentList.Data.ListEnvironmentDetails.Environments;
74-
75-
for i :=0; i <= len(environmentListData); i++ {
76+
environmentListData := environmentList.Data.ListEnvironmentDetails.Environments
77+
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 0,'\t', tabwriter.AlignRight)
78+
writer.Flush()
79+
utils.White_B.Fprintln(writer, "CHAOS ENVIRONMENT ID\tCHAOS ENVIRONMENT NAME\tCREATED AT\tCREATED BY")
80+
for i :=0; i < len(environmentListData); i++ {
7681
if(environmentListData[i].EnvironmentID == environmentID){
77-
utils.PrintInJsonFormat(environmentListData[i]);
78-
os.Exit(1)
82+
intTime, err := strconv.ParseInt(environmentListData[i].CreatedAt, 10, 64)
83+
if err != nil {
84+
fmt.Println("Error converting CreatedAt to int64:", err)
85+
continue
86+
}
87+
humanTime := time.Unix(intTime, 0)
88+
writer.Flush()
89+
utils.White.Fprintln(
90+
writer,
91+
environmentListData[i].EnvironmentID+"\t"+environmentListData[i].Name+"\t"+humanTime.String()+"\t"+environmentListData[i].CreatedBy.Username+"\t")
92+
break
7993
}
8094
}
95+
writer.Flush()
96+
97+
8198
},
8299

83100

84101
}
102+
85103
func init() {
86104
GetCmd.AddCommand(ChaosEnvironmentCmd);
87105
ChaosEnvironmentCmd.Flags().String("project-id", "", "Set the project-id to get Chaos Environment from a particular project.")

pkg/cmd/get/projects.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"os"
2020
"text/tabwriter"
2121
"time"
22-
2322
"github.com/litmuschaos/litmusctl/pkg/apis"
2423
"github.com/litmuschaos/litmusctl/pkg/utils"
2524
"github.com/manifoldco/promptui"
@@ -49,7 +48,6 @@ var projectsCmd = &cobra.Command{
4948
utils.PrintInYamlFormat(projects.Data)
5049

5150
case "":
52-
5351
itemsPerPage := 5
5452
page := 1
5553
totalProjects := len(projects.Data)

pkg/cmd/list/environment.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import (
2020
"fmt"
2121
"os"
2222
"strings"
23-
23+
"text/tabwriter"
24+
"time"
25+
"strconv"
2426
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
2527
"github.com/litmuschaos/litmusctl/pkg/utils"
28+
"github.com/manifoldco/promptui"
2629
"github.com/spf13/cobra"
30+
2731
)
2832

2933

@@ -58,7 +62,57 @@ var ListChaosEnvironmentCmd = &cobra.Command{
5862
os.Exit(1)
5963
}
6064
}
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\tCHAOS ENVIRONMENT NAME\tCREATED AT\tCREATED 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+
}
62116
},
63117

64118

0 commit comments

Comments
 (0)