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

add egctl edit, egctl logs, update egctl create cmd #1067

Merged
merged 15 commits into from
Aug 31, 2023
Prev Previous commit
Next Next commit
update based on github action
  • Loading branch information
suchen-sci committed Aug 25, 2023
commit 0d9548f4440c202e4617dc01cb2fe3317bbf739b
4 changes: 2 additions & 2 deletions cmd/client/commandv2/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// CreateCmd returns create command.
func CreateCmd() *cobra.Command {
cmd := create.CreateCmd()
cmd.AddCommand(create.CreateHTTPProxyCmd())
cmd := create.Cmd()
cmd.AddCommand(create.HTTPProxyCmd())
return cmd
}
4 changes: 2 additions & 2 deletions cmd/client/commandv2/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/spf13/cobra"
)

// CreateCmd returns create command.
func CreateCmd() *cobra.Command {
// Cmd returns create command.
func Cmd() *cobra.Command {
examples := []general.Example{
{Desc: "Create a resource from a file", Command: "egctl create -f <filename>.yaml"},
{Desc: "Create a resource from stdin", Command: "cat <filename>.yaml | egctl create -f -"},
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/commandv2/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCreate(t *testing.T) {
cmd := CreateCmd()
func TestCmd(t *testing.T) {
cmd := Cmd()
assert.NotNil(t, cmd)
}
54 changes: 27 additions & 27 deletions cmd/client/commandv2/create/createhttpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
"github.com/spf13/cobra"
)

// CreateHTTPProxyOptions are the options to create a HTTPProxy.
type CreateHTTPProxyOptions struct {
// HTTPProxyOptions are the options to create a HTTPProxy.
type HTTPProxyOptions struct {
Name string
Port int
Rules []string
Expand All @@ -51,12 +51,12 @@ type CreateHTTPProxyOptions struct {
caCert string
certs []string
keys []string
rules []*CreateHTTPProxyRule
rules []*HTTPProxyRule
}

var createHTTPProxyOptions = &CreateHTTPProxyOptions{}
var httpProxyOptions = &HTTPProxyOptions{}

var createHTTPProxyExamples = `# General case
var httpProxyExamples = `# General case
egctl create httpproxy NAME --port PORT \
--rule HOST/PATH=ENDPOINT1,ENDPOINT2 \
[--rule HOST/PATH=ENDPOINT1,ENDPOINT2] \
Expand All @@ -79,17 +79,17 @@ egctl create httpproxy demo2 --port 10081 \
--rule="foo.com/prefix*=http:https://127.0.0.1:8083"
`

// CreateHTTPProxyCmd returns create command of HTTPProxy.
func CreateHTTPProxyCmd() *cobra.Command {
o := createHTTPProxyOptions
// HTTPProxyCmd returns create command of HTTPProxy.
func HTTPProxyCmd() *cobra.Command {
o := httpProxyOptions

cmd := &cobra.Command{
Use: "httpproxy NAME",
Short: "Create a HTTPServer and corresponding Pipelines with a specific name",
Args: createHTTPProxyArgs,
Example: general.CreateMultiLineExample(createHTTPProxyExamples),
Args: httpProxyArgs,
Example: general.CreateMultiLineExample(httpProxyExamples),
Run: func(cmd *cobra.Command, args []string) {
err := createHTTPProxyRun(cmd, args)
err := httpProxyRun(cmd, args)
if err != nil {
general.ExitWithError(err)
}
Expand All @@ -107,8 +107,8 @@ func CreateHTTPProxyCmd() *cobra.Command {
return cmd
}

func createHTTPProxyArgs(_ *cobra.Command, args []string) error {
o := createHTTPProxyOptions
func httpProxyArgs(_ *cobra.Command, args []string) error {
o := httpProxyOptions
if len(args) != 1 {
return fmt.Errorf("create httpproxy requires a name")
}
Expand All @@ -124,8 +124,8 @@ func createHTTPProxyArgs(_ *cobra.Command, args []string) error {
return nil
}

func createHTTPProxyRun(cmd *cobra.Command, args []string) error {
o := createHTTPProxyOptions
func httpProxyRun(cmd *cobra.Command, args []string) error {
o := httpProxyOptions
o.Complete(args)
if err := o.Parse(); err != nil {
return err
Expand Down Expand Up @@ -164,14 +164,14 @@ type PipelineSpec struct {
}

// Complete completes all the required options.
func (o *CreateHTTPProxyOptions) Complete(args []string) {
func (o *HTTPProxyOptions) Complete(args []string) {
o.Name = args[0]
}

// Parse parses all the optional options.
func (o *CreateHTTPProxyOptions) Parse() error {
func (o *HTTPProxyOptions) Parse() error {
// parse rules
rules := []*CreateHTTPProxyRule{}
rules := []*HTTPProxyRule{}
for _, rule := range o.Rules {
r, err := parseRule(rule)
if err != nil {
Expand Down Expand Up @@ -214,16 +214,16 @@ func (o *CreateHTTPProxyOptions) Parse() error {
return nil
}

func (o *CreateHTTPProxyOptions) getServerName() string {
func (o *HTTPProxyOptions) getServerName() string {
return o.Name + "-server"
}

func (o *CreateHTTPProxyOptions) getPipelineName(id int) string {
func (o *HTTPProxyOptions) getPipelineName(id int) string {
return fmt.Sprintf("%s-pipeline-%d", o.Name, id)
}

// Translate translates CreateHTTPProxyOptions to HTTPServerSpec and PipelineSpec.
func (o *CreateHTTPProxyOptions) Translate() (*HTTPServerSpec, []*PipelineSpec) {
// Translate translates HTTPProxyOptions to HTTPServerSpec and PipelineSpec.
func (o *HTTPProxyOptions) Translate() (*HTTPServerSpec, []*PipelineSpec) {
hs := &HTTPServerSpec{
Name: o.getServerName(),
Kind: httpserver.Kind,
Expand All @@ -247,7 +247,7 @@ func (o *CreateHTTPProxyOptions) Translate() (*HTTPServerSpec, []*PipelineSpec)
return hs, pipelines
}

func (o *CreateHTTPProxyOptions) translateRules() (routers.Rules, []*PipelineSpec) {
func (o *HTTPProxyOptions) translateRules() (routers.Rules, []*PipelineSpec) {
var rules routers.Rules
var pipelines []*PipelineSpec
pipelineID := 0
Expand Down Expand Up @@ -338,7 +338,7 @@ func translateToProxyFilter(endpoints []string) *httpproxy.Spec {
return spec
}

func parseRule(rule string) (*CreateHTTPProxyRule, error) {
func parseRule(rule string) (*HTTPProxyRule, error) {
parts := strings.Split(rule, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("rule %s should in format 'host/path=endpoint1,endpoint2', invalid format", rule)
Expand Down Expand Up @@ -366,16 +366,16 @@ func parseRule(rule string) (*CreateHTTPProxyRule, error) {
return nil, fmt.Errorf("endpoints in rule %s is empty", rule)
}

return &CreateHTTPProxyRule{
return &HTTPProxyRule{
Host: host,
Path: path,
PathPrefix: pathPrefix,
Endpoints: endpoints,
}, nil
}

// CreateHTTPProxyRule is the rule of HTTPProxy.
type CreateHTTPProxyRule struct {
// HTTPProxyRule is the rule of HTTPProxy.
type HTTPProxyRule struct {
Host string
Path string
PathPrefix string
Expand Down
30 changes: 15 additions & 15 deletions cmd/client/commandv2/create/createhttpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestCreateHTTPProxyOptions(t *testing.T) {
}
certBase64 := "aGVsbG8="

o := &CreateHTTPProxyOptions{
o := &HTTPProxyOptions{
Port: 10080,
Rules: []string{
"foo.com/barz=http:https://127.0.0.1:9095",
Expand Down Expand Up @@ -335,50 +335,50 @@ filters:
}

func TestCreateHTTPProxyCmd(t *testing.T) {
cmd := CreateHTTPProxyCmd()
cmd := HTTPProxyCmd()
assert.NotNil(t, cmd)

resetOption := func() {
createHTTPProxyOptions = &CreateHTTPProxyOptions{
httpProxyOptions = &HTTPProxyOptions{
Port: 10080,
Rules: []string{
"foo.com/bar=http:https://127.0.0.1:9096",
},
}
}
resetOption()
err := createHTTPProxyArgs(cmd, []string{"demo"})
err := httpProxyArgs(cmd, []string{"demo"})
assert.Nil(t, err)

// test arg len
err = createHTTPProxyArgs(cmd, []string{})
err = httpProxyArgs(cmd, []string{})
assert.NotNil(t, err)
err = createHTTPProxyArgs(cmd, []string{"demo", "123"})
err = httpProxyArgs(cmd, []string{"demo", "123"})
assert.NotNil(t, err)

// test port
createHTTPProxyOptions.Port = -1
err = createHTTPProxyArgs(cmd, []string{"demo"})
httpProxyOptions.Port = -1
err = httpProxyArgs(cmd, []string{"demo"})
assert.NotNil(t, err)

createHTTPProxyOptions.Port = 65536
err = createHTTPProxyArgs(cmd, []string{"demo"})
httpProxyOptions.Port = 65536
err = httpProxyArgs(cmd, []string{"demo"})
assert.NotNil(t, err)
resetOption()

// test rule
createHTTPProxyOptions.Rules = []string{}
err = createHTTPProxyArgs(cmd, []string{"demo"})
httpProxyOptions.Rules = []string{}
err = httpProxyArgs(cmd, []string{"demo"})
assert.NotNil(t, err)
resetOption()

// test cert files
createHTTPProxyOptions.CertFiles = []string{"not-exist-file.cert"}
err = createHTTPProxyArgs(cmd, []string{"demo"})
httpProxyOptions.CertFiles = []string{"not-exist-file.cert"}
err = httpProxyArgs(cmd, []string{"demo"})
assert.NotNil(t, err)
resetOption()

// test run
err = createHTTPProxyRun(cmd, []string{"demo"})
err = httpProxyRun(cmd, []string{"demo"})
assert.NotNil(t, err)
}
Loading