-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get chaos environments command (#170)
* add get/list env commands to docs Signed-off-by: Shivam Purohit <[email protected]> * modify print for error message Signed-off-by: Shivam Purohit <[email protected]> * fix:fmt error Signed-off-by: Shivam Purohit <[email protected]> --------- Signed-off-by: Shivam Purohit <[email protected]>
- Loading branch information
1 parent
486349b
commit cf8c33a
Showing
9 changed files
with
338 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright © 2021 The LitmusChaos Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package get | ||
|
||
import ( | ||
"fmt" | ||
"github.com/litmuschaos/litmusctl/pkg/apis/environment" | ||
"github.com/litmuschaos/litmusctl/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"text/tabwriter" | ||
"time" | ||
) | ||
|
||
var ChaosEnvironmentCmd = &cobra.Command{ | ||
Use: "chaos-environment", | ||
Short: "Get Chaos Environment within the project", | ||
Long: `Display the Chaos Environments within the project with the targeted id `, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
credentials, err := utils.GetCredentials(cmd) | ||
utils.PrintError(err) | ||
|
||
projectID, err := cmd.Flags().GetString("project-id") | ||
utils.PrintError(err) | ||
|
||
if projectID == "" { | ||
utils.White_B.Print("\nEnter the Project ID: ") | ||
fmt.Scanln(&projectID) | ||
|
||
if projectID == "" { | ||
utils.Red.Println("⛔ Project ID can't be empty!!") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
environmentID, err := cmd.Flags().GetString("environment-id") | ||
utils.PrintError(err) | ||
|
||
if environmentID == "" { | ||
utils.White_B.Print("\nEnter the Environment ID: ") | ||
fmt.Scanln(&environmentID) | ||
|
||
if environmentID == "" { | ||
utils.Red.Println("⛔ Environment ID can't be empty!!") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
environmentList, err := environment.GetEnvironmentList(projectID, credentials) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "permission_denied") { | ||
utils.Red.Println("❌ You don't have enough permissions to access this resource.") | ||
os.Exit(1) | ||
} else { | ||
utils.PrintError(err) | ||
os.Exit(1) | ||
} | ||
} | ||
environmentListData := environmentList.Data.ListEnvironmentDetails.Environments | ||
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 0, '\t', tabwriter.AlignRight) | ||
writer.Flush() | ||
for i := 0; i < len(environmentListData); i++ { | ||
if environmentListData[i].EnvironmentID == environmentID { | ||
intUpdateTime, err := strconv.ParseInt(environmentListData[i].UpdatedAt, 10, 64) | ||
if err != nil { | ||
utils.Red.Println("Error converting UpdatedAt to int64:", err) | ||
continue | ||
} | ||
updatedTime := time.Unix(intUpdateTime, 0).String() | ||
intCreatedTime, err := strconv.ParseInt(environmentListData[i].CreatedAt, 10, 64) | ||
if err != nil { | ||
utils.Red.Println("Error converting CreatedAt to int64:", err) | ||
continue | ||
} | ||
createdTime := time.Unix(intCreatedTime, 0).String() | ||
writer.Flush() | ||
utils.White_B.Fprintln(writer, "CHAOS ENVIRONMENT DETAILS") | ||
utils.White.Fprintln(writer, "CHAOS ENVIRONMENT ID\t", environmentListData[i].EnvironmentID) | ||
utils.White.Fprintln(writer, "CHAOS ENVIRONMENT NAME\t", environmentListData[i].Name) | ||
utils.White.Fprintln(writer, "CHAOS ENVIRONMENT Type\t", environmentListData[i].Type) | ||
utils.White.Fprintln(writer, "CREATED AT\t", createdTime) | ||
utils.White.Fprintln(writer, "CREATED BY\t", environmentListData[i].CreatedBy.Username) | ||
utils.White.Fprintln(writer, "UPDATED AT\t", updatedTime) | ||
utils.White.Fprintln(writer, "UPDATED BY\t", environmentListData[i].UpdatedBy.Username) | ||
utils.White.Fprintln(writer, "CHAOS INFRA IDs\t", strings.Join(environmentListData[i].InfraIDs, ", ")) | ||
break | ||
} | ||
} | ||
writer.Flush() | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
GetCmd.AddCommand(ChaosEnvironmentCmd) | ||
ChaosEnvironmentCmd.Flags().String("project-id", "", "Set the project-id to get Chaos Environment from a particular project.") | ||
ChaosEnvironmentCmd.Flags().String("environment-id", "", "Set the environment-id to get Chaos Environment") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright © 2021 The LitmusChaos Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package list | ||
|
||
import ( | ||
"fmt" | ||
"github.com/litmuschaos/litmusctl/pkg/apis/environment" | ||
"github.com/litmuschaos/litmusctl/pkg/utils" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"text/tabwriter" | ||
"time" | ||
) | ||
|
||
var ListChaosEnvironmentCmd = &cobra.Command{ | ||
Use: "chaos-environments", | ||
Short: "Get Chaos Environments within the project", | ||
Long: `Display the Chaos Environments within the project with the targeted id `, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
credentials, err := utils.GetCredentials(cmd) | ||
utils.PrintError(err) | ||
|
||
projectID, err := cmd.Flags().GetString("project-id") | ||
utils.PrintError(err) | ||
|
||
if projectID == "" { | ||
utils.White_B.Print("\nEnter the Project ID: ") | ||
fmt.Scanln(&projectID) | ||
|
||
for projectID == "" { | ||
utils.Red.Println("⛔ Project ID can't be empty!!") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
environmentList, err := environment.GetEnvironmentList(projectID, credentials) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "permission_denied") { | ||
utils.Red.Println("❌ You don't have enough permissions to access this resource.") | ||
os.Exit(1) | ||
} else { | ||
utils.PrintError(err) | ||
os.Exit(1) | ||
} | ||
} | ||
environmentListData := environmentList.Data.ListEnvironmentDetails.Environments | ||
|
||
itemsPerPage := 5 | ||
page := 1 | ||
totalEnvironments := len(environmentListData) | ||
|
||
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 0, '\t', tabwriter.AlignRight) | ||
utils.White_B.Fprintln(writer, "CHAOS ENVIRONMENT ID\tCHAOS ENVIRONMENT NAME\tCREATED AT\tCREATED BY") | ||
for { | ||
writer.Flush() | ||
// calculating the start and end indices for the current page | ||
start := (page - 1) * itemsPerPage | ||
if start >= totalEnvironments { | ||
writer.Flush() | ||
utils.Red.Println("No more environments to display") | ||
break | ||
} | ||
end := start + itemsPerPage | ||
if end > totalEnvironments { | ||
end = totalEnvironments | ||
|
||
} | ||
for _, environment := range environmentListData[start:end] { | ||
intTime, err := strconv.ParseInt(environment.CreatedAt, 10, 64) | ||
if err != nil { | ||
fmt.Println("Error converting CreatedAt to int64:", err) | ||
continue | ||
} | ||
humanTime := time.Unix(intTime, 0) | ||
utils.White.Fprintln( | ||
writer, | ||
environment.EnvironmentID+"\t"+environment.Name+"\t"+humanTime.String()+"\t"+environment.CreatedBy.Username, | ||
) | ||
} | ||
writer.Flush() | ||
// Check if it's the last item or if user wants to see more | ||
paginationPrompt := promptui.Prompt{ | ||
Label: "Press Enter to show more environments (or type 'q' to quit)", | ||
AllowEdit: true, | ||
Default: "", | ||
} | ||
|
||
userInput, err := paginationPrompt.Run() | ||
utils.PrintError(err) | ||
|
||
if userInput == "q" { | ||
break | ||
} | ||
// Move to the next page | ||
page++ | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
ListCmd.AddCommand(ListChaosEnvironmentCmd) | ||
ListChaosEnvironmentCmd.Flags().String("project-id", "", "Set the project-id to list Chaos Environments from a particular project.") | ||
} |
Oops, something went wrong.