Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance egctl #1020

Merged
merged 31 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a755223
finish get command and refactor code
suchen-sci Jun 16, 2023
a118ed2
finish describe object
suchen-sci Jun 16, 2023
4c96e30
finish all describe
suchen-sci Jun 19, 2023
84ab4c6
finish create, delete, update
suchen-sci Jun 20, 2023
5f9815a
finish other command
suchen-sci Jun 21, 2023
6a807fe
add example framework
suchen-sci Jun 21, 2023
587e948
add more examples, add other missing commands
suchen-sci Jun 25, 2023
75219f6
add command for object status
suchen-sci Jun 26, 2023
9a74cf9
udpate egctl command
suchen-sci Jun 26, 2023
7d1f484
update
suchen-sci Jun 26, 2023
9f75742
add comments
suchen-sci Jun 26, 2023
7bc5634
add more comments, add egctl cheat sheet
suchen-sci Jun 26, 2023
9d8063a
update egctl cheat sheet
suchen-sci Jun 26, 2023
a496d83
fix random test bug
suchen-sci Jun 26, 2023
86baec0
update based on comment
suchen-sci Jun 26, 2023
599cd37
rewrite apply create get command
suchen-sci Jun 29, 2023
bf84ef6
finish describe, delete and refactor code
suchen-sci Jun 30, 2023
66d0089
update examples
suchen-sci Jul 3, 2023
e3169b9
Merge branch 'main' into enhance-egctl
suchen-sci Jul 3, 2023
f4a1e84
fix typo
suchen-sci Jul 3, 2023
d62f266
add security for egctl
suchen-sci Jul 5, 2023
d65c077
update doc
suchen-sci Jul 5, 2023
cb58b43
refactor code structure
suchen-sci Jul 6, 2023
f69ec13
update based on code review comments
suchen-sci Jul 6, 2023
14ebb75
update based on github action
suchen-sci Jul 6, 2023
7dfd1cc
update based on github action
suchen-sci Jul 6, 2023
83df91b
update based on github action
suchen-sci Jul 6, 2023
92f54a8
update based on github action
suchen-sci Jul 6, 2023
b8593fc
update based on github action
suchen-sci Jul 6, 2023
c4e92c4
update based on github action
suchen-sci Jul 6, 2023
6e81f67
support egctl create/apply -f -
suchen-sci Jul 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add more examples, add other missing commands
  • Loading branch information
suchen-sci committed Jun 25, 2023
commit 587e948c5590b8304e059761857c37a3da2e50d2
56 changes: 56 additions & 0 deletions cmd/client/commandv2/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"os"
"sort"
"strings"

"github.com/megaease/easegress/cmd/client/general"
"github.com/megaease/easegress/pkg/api"
Expand Down Expand Up @@ -106,3 +107,58 @@ func APIsCmd() *cobra.Command {

return cmd
}

func APIResourcesCmd() *cobra.Command {
resourceCmds := []*cobra.Command{
GetCmd(),
ApplyCmd(),
CreateCmd(),
DeleteCmd(),
DescribeCmd(),
}

actionMap := map[string][]string{}
aliasMap := map[string]string{}
for _, cmd := range resourceCmds {
action, resources, resourceAlias := getApiResource(cmd)
for i, r := range resources {
actionMap[r] = append(actionMap[r], action)
aliasMap[r] = resourceAlias[i]
}
}

for _, v := range actionMap {
sort.Strings(v)
}

cmd := &cobra.Command{
Use: "api-resources",
Short: "View all API resources",
Run: func(cmd *cobra.Command, args []string) {
tables := [][]string{}
for resource, actions := range actionMap {
tables = append(tables, []string{resource, aliasMap[resource], strings.Join(actions, ",")})
}
sort.Slice(tables, func(i, j int) bool {
return tables[i][0] < tables[j][0]
})
tables = append([][]string{{"RESOURCE", "ALIAS", "ACTIONS"}}, tables...)
general.PrintTable(tables)
},
}
return cmd
}

func getApiResource(actionCmd *cobra.Command) (string, []string, []string) {
resources := []string{}
resourceAlias := []string{}
for _, cmd := range actionCmd.Commands() {
resources = append(resources, cmd.Name())
alias := cmd.Aliases
sort.Slice(alias, func(i, j int) bool {
return len(alias[i]) < len(alias[j])
})
resourceAlias = append(resourceAlias, strings.Join(alias, ","))
}
return actionCmd.Name(), resources, resourceAlias
}
5 changes: 3 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func main() {

addCommandWithGroup(
basicGroup,
commandv2.GetCmd(),
commandv2.DescribeCmd(),
commandv2.CreateCmd(),
commandv2.DeleteCmd(),
commandv2.GetCmd(),
commandv2.DescribeCmd(),
commandv2.ApplyCmd(),
)

Expand All @@ -82,6 +82,7 @@ func main() {
commandv2.CompletionCmd(),
commandv2.HealthCmd(),
commandv2.ProfileCmd(),
commandv2.APIResourcesCmd(),
)

addCommandWithGroup(
Expand Down
1 change: 1 addition & 0 deletions cmd/client/resources/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ import "github.com/megaease/easegress/cmd/client/general"
var makeURL = general.MakeURL
var handleReq = general.HandleRequest
var createExample = general.CreateExample
var createMultiExample = general.CreateMultiExample
36 changes: 35 additions & 1 deletion cmd/client/resources/customdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func customDataKindApplyCmd() []*cobra.Command {
}

func describeCustomDataKinds() *cobra.Command {
examples := []general.Example{
{Desc: "Describe all custom data kinds", Command: "egctl describe customdatakind"},
{Desc: "Describe certain custom data kind", Command: "egctl describe customdatakind <custom-data-kind>"},
}

cmd := &cobra.Command{
Use: CustomDataKindName,
Short: "Describe one or many custom data kinds",
Expand All @@ -80,6 +85,7 @@ func describeCustomDataKinds() *cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := httpGetCustomDataKind(cmd, args)
if err != nil {
Expand Down Expand Up @@ -113,6 +119,11 @@ func httpGetCustomDataKind(cmd *cobra.Command, args []string) ([]byte, error) {
}

func getCustomDataKinds() *cobra.Command {
examples := []general.Example{
{Desc: "Get all custom data kinds", Command: "egctl get customdatakind"},
{Desc: "Get certain custom data kind", Command: "egctl get customdatakind <custom-data-kind>"},
}

cmd := &cobra.Command{
Use: CustomDataKindName,
Short: "Display one or many custom data kinds",
Expand All @@ -123,6 +134,7 @@ func getCustomDataKinds() *cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := httpGetCustomDataKind(cmd, args)
if err != nil {
Expand Down Expand Up @@ -187,6 +199,7 @@ func createCustomDataKind() *cobra.Command {
Use: CustomDataKindName,
Short: "Create a custom data kind from a yaml file or stdin",
Aliases: CustomDataKindAlias(),
Example: createExample("Create a custom data kind from a yaml file", "egctl create -f <custom-data-kind>.yaml"),
Run: func(cmd *cobra.Command, args []string) {
visitor := buildYAMLVisitor(specFile, cmd)
visitor.Visit(func(yamlDoc []byte) error {
Expand All @@ -203,7 +216,6 @@ func createCustomDataKind() *cobra.Command {
}

cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file containing custom data kind spec")

return cmd
}

Expand All @@ -218,6 +230,7 @@ func deleteCustomDataKind() *cobra.Command {
}
return nil
},
Example: createExample("Delete a custom data kind", "egctl delete customdatakind <custom-data-kind>"),
Run: func(cmd *cobra.Command, args []string) {
_, err := handleReq(http.MethodDelete, makeURL(general.CustomDataKindItemURL, args[0]), nil, cmd)
if err != nil {
Expand Down Expand Up @@ -257,6 +270,7 @@ func applyCustomDataKind() *cobra.Command {
Use: CustomDataKindName,
Short: "Apply a custom data kind from a yaml file or stdin",
Aliases: CustomDataKindAlias(),
Example: createExample("Apply a custom data kind from a yaml file", "egctl apply -f <custom-data-kind>.yaml"),
Run: func(cmd *cobra.Command, args []string) {
visitor := buildYAMLVisitor(specFile, cmd)
visitor.Visit(func(yamlDoc []byte) error {
Expand Down Expand Up @@ -354,6 +368,11 @@ func getCertainCustomDataKind(cmd *cobra.Command, kindName string) (*customdata.
}

func getCustomData() *cobra.Command {
examples := []general.Example{
{Desc: "Get all custom data of certain kind", Command: "egctl get customdata <custom-data-kind>"},
{Desc: "Get a certain custom data of certain kind", Command: "egctl get customdata <custom-data-kind> <custom-data-id>"},
}

cmd := &cobra.Command{
Use: CustomDataName,
Short: "Display one or many custom data of given kind",
Expand All @@ -364,6 +383,7 @@ func getCustomData() *cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := httpGetCustomData(cmd, args)
if err != nil {
Expand Down Expand Up @@ -392,6 +412,11 @@ func getCustomData() *cobra.Command {
}

func describeCustomData() *cobra.Command {
examples := []general.Example{
{Desc: "Describe all custom data of certain kind", Command: "egctl describe customdata <custom-data-kind>"},
{Desc: "Describe a certain custom data of certain kind", Command: "egctl describe customdata <custom-data-kind> <custom-data-id>"},
}

cmd := &cobra.Command{
Use: CustomDataName,
Short: "Describe one or many custom data of given kind",
Expand All @@ -402,6 +427,7 @@ func describeCustomData() *cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := httpGetCustomData(cmd, args)
if err != nil {
Expand Down Expand Up @@ -462,6 +488,7 @@ func createCustomData() *cobra.Command {
}
return nil
},
Example: createExample("Create a custom data from a yaml file", "egctl create customdata <custom-data-kind> -f <custom-data>.yaml"),
Run: func(cmd *cobra.Command, args []string) {
visitor := buildYAMLVisitor(specFile, cmd)
visitor.Visit(func(yamlDoc []byte) error {
Expand Down Expand Up @@ -492,6 +519,7 @@ func deleteCustomData() *cobra.Command {
}
return nil
},
Example: createExample("Delete a custom data item", "egctl delete customdata <custom-data-kind> <custom-data-id>"),
Run: func(cmd *cobra.Command, args []string) {
_, err := handleReq(http.MethodDelete, makeURL(general.CustomDataItemURL, args[0], args[1]), nil, cmd)
if err != nil {
Expand All @@ -506,6 +534,11 @@ func deleteCustomData() *cobra.Command {
}

func applyCustomData() *cobra.Command {
examples := []general.Example{
{Desc: "Apply a custom data from a yaml file", Command: "egctl apply customdata <custom-data-kind> -f <custom-data>.yaml"},
{Desc: "Apply a batch update for custom data", Command: "egctl apply customdata <custom-data-kind> -f <custom-data>.yaml --batch-update"},
}

var specFile string
var batchUpdate bool

Expand Down Expand Up @@ -585,6 +618,7 @@ func applyCustomData() *cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
if batchUpdate {
applyBatch(cmd, args)
Expand Down
38 changes: 38 additions & 0 deletions cmd/client/resources/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package resources

import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
Expand All @@ -42,12 +43,19 @@ func memberCmd(cmdType general.CmdType) []*cobra.Command {
return memberGetCmd()
case general.DescribeCmd:
return memberDescribeCmd()
case general.DeleteCmd:
return memberDeleteCmd()
default:
return nil
}
}

func memberDescribeCmd() []*cobra.Command {
examples := []general.Example{
{Desc: "Describe all members", Command: "egctl describe member"},
{Desc: "Describe one member", Command: "egctl describe member <member-name>"},
}

cmd := &cobra.Command{
Use: MemberName,
Short: "Describe one or many members",
Expand All @@ -58,6 +66,7 @@ func memberDescribeCmd() []*cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := handleReq(http.MethodGet, makeURL(general.MembersURL), nil, cmd)
if err != nil {
Expand Down Expand Up @@ -118,7 +127,35 @@ func printMemberStatusDescription(memberStatus []*cluster.MemberStatus) {
})
}

func memberDeleteCmd() []*cobra.Command {
cmd := &cobra.Command{
Use: MemberName,
Aliases: MemberAlias(),
Short: "Purge a Easegress member. This command should be run after the easegress node uninstalled",
Example: createExample("Purge a Easegress member. This command should be run after the easegress node uninstalled", "egctl delete member <member-name>"),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("requires one member name to be deleted")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
_, err := handleReq(http.MethodDelete, makeURL(general.MemberItemURL, args[0]), nil, cmd)
if err != nil {
general.ExitWithErrorf("purge member failed: %v", err)
}
fmt.Printf("purge member %s successfully\n", args[0])
},
}
return []*cobra.Command{cmd}
}

func memberGetCmd() []*cobra.Command {
examples := []general.Example{
{Desc: "Get all members", Command: "egctl get member"},
{Desc: "Get one member", Command: "egctl get member <member-name>"},
}

cmd := &cobra.Command{
Use: MemberName,
Short: "Display one or many members",
Expand All @@ -129,6 +166,7 @@ func memberGetCmd() []*cobra.Command {
}
return nil
},
Example: createMultiExample(examples),
Run: func(cmd *cobra.Command, args []string) {
body, err := handleReq(http.MethodGet, makeURL(general.MembersURL), nil, cmd)
if err != nil {
Expand Down
Loading