From 93fae027585cc0022eadd0d1ba3905a34dacc3a0 Mon Sep 17 00:00:00 2001 From: shivam Date: Tue, 10 Sep 2024 17:49:10 +0530 Subject: [PATCH 1/3] get-project:fix for new schema Signed-off-by: shivam --- pkg/apis/project.go | 31 ++++++++++++++++++++++++------- pkg/cmd/get/projects.go | 8 ++++---- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/apis/project.go b/pkg/apis/project.go index 58b85ee7..7dd4539e 100644 --- a/pkg/apis/project.go +++ b/pkg/apis/project.go @@ -81,11 +81,26 @@ func CreateProjectRequest(projectName string, cred types.Credentials) (CreatePro } } +// type listProjectResponse struct { +// Data []struct { +// ID string `json:"ProjectID"` +// Name string `json:"Name"` +// CreatedAt int64 `json:"CreatedAt"` +// } `json:"data"` +// Errors []struct { +// Message string `json:"message"` +// Path []string `json:"path"` +// } `json:"errors"` +// } + type listProjectResponse struct { - Data []struct { - ID string `json:"ProjectID"` - Name string `json:"Name"` - CreatedAt int64 `json:"CreatedAt"` + Data struct { + Projects []struct { + ID string `json:"projectID"` // Adjusted field name + Name string `json:"name"` + CreatedAt int64 `json:"createdAt"` + } `json:"projects"` + TotalNumberOfProjects int `json:"totalNumberOfProjects"` } `json:"data"` Errors []struct { Message string `json:"message"` @@ -104,20 +119,22 @@ func ListProject(cred types.Credentials) (listProjectResponse, error) { if err != nil { return listProjectResponse{}, err } - defer resp.Body.Close() if resp.StatusCode == http.StatusOK { var data listProjectResponse err = json.Unmarshal(bodyBytes, &data) if err != nil { + utils.Red.Println("here1") return listProjectResponse{}, err + } - + if len(data.Errors) > 0 { + utils.Red.Println("here12") return listProjectResponse{}, errors.New(data.Errors[0].Message) } - + return data, nil } else { return listProjectResponse{}, errors.New("Unmatched status code:" + string(bodyBytes)) diff --git a/pkg/cmd/get/projects.go b/pkg/cmd/get/projects.go index 6f98353c..ecd528a6 100644 --- a/pkg/cmd/get/projects.go +++ b/pkg/cmd/get/projects.go @@ -45,12 +45,12 @@ var projectsCmd = &cobra.Command{ utils.PrintInJsonFormat(projects.Data) case "yaml": - utils.PrintInYamlFormat(projects.Data) + utils.PrintInYamlFormat(projects.Data.Projects) case "": itemsPerPage := 5 page := 1 - totalProjects := len(projects.Data) + totalProjects := len(projects.Data.Projects) for { // calculating the start and end indices for the current page @@ -69,9 +69,9 @@ var projectsCmd = &cobra.Command{ // displaying the projects for the current page writer := tabwriter.NewWriter(os.Stdout, 8, 8, 8, '\t', tabwriter.AlignRight) utils.White_B.Fprintln(writer, "PROJECT ID\tPROJECT NAME\tCREATED AT") - for _, project := range projects.Data[start:end] { + for _, project := range projects.Data.Projects[start:end] { intTime := project.CreatedAt - humanTime := time.Unix(intTime, 0) + humanTime := time.Unix(intTime/1000, 0) // Convert milliseconds to second utils.White.Fprintln(writer, project.ID+"\t"+project.Name+"\t"+humanTime.String()+"\t") } writer.Flush() From c870dabd62faa54101d6da1f52c617e1a22e49b7 Mon Sep 17 00:00:00 2001 From: shivam Date: Tue, 10 Sep 2024 18:02:37 +0530 Subject: [PATCH 2/3] remove comments and unnecessary code Signed-off-by: shivam --- pkg/apis/project.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pkg/apis/project.go b/pkg/apis/project.go index 7dd4539e..5226fad0 100644 --- a/pkg/apis/project.go +++ b/pkg/apis/project.go @@ -81,18 +81,6 @@ func CreateProjectRequest(projectName string, cred types.Credentials) (CreatePro } } -// type listProjectResponse struct { -// Data []struct { -// ID string `json:"ProjectID"` -// Name string `json:"Name"` -// CreatedAt int64 `json:"CreatedAt"` -// } `json:"data"` -// Errors []struct { -// Message string `json:"message"` -// Path []string `json:"path"` -// } `json:"errors"` -// } - type listProjectResponse struct { Data struct { Projects []struct { @@ -125,13 +113,11 @@ func ListProject(cred types.Credentials) (listProjectResponse, error) { var data listProjectResponse err = json.Unmarshal(bodyBytes, &data) if err != nil { - utils.Red.Println("here1") return listProjectResponse{}, err } if len(data.Errors) > 0 { - utils.Red.Println("here12") return listProjectResponse{}, errors.New(data.Errors[0].Message) } From 7966ff96d6450ffe78a949701a3bab07cce59e11 Mon Sep 17 00:00:00 2001 From: shivam Date: Tue, 10 Sep 2024 18:24:40 +0530 Subject: [PATCH 3/3] gofmt fix Signed-off-by: shivam --- pkg/apis/project.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/apis/project.go b/pkg/apis/project.go index 5226fad0..de1f838c 100644 --- a/pkg/apis/project.go +++ b/pkg/apis/project.go @@ -84,7 +84,7 @@ func CreateProjectRequest(projectName string, cred types.Credentials) (CreatePro type listProjectResponse struct { Data struct { Projects []struct { - ID string `json:"projectID"` // Adjusted field name + ID string `json:"projectID"` // Adjusted field name Name string `json:"name"` CreatedAt int64 `json:"createdAt"` } `json:"projects"` @@ -114,13 +114,13 @@ func ListProject(cred types.Credentials) (listProjectResponse, error) { err = json.Unmarshal(bodyBytes, &data) if err != nil { return listProjectResponse{}, err - + } - + if len(data.Errors) > 0 { return listProjectResponse{}, errors.New(data.Errors[0].Message) } - + return data, nil } else { return listProjectResponse{}, errors.New("Unmatched status code:" + string(bodyBytes))