From 9a1a9a15a8dbd5093db677bfba82e747c729e1c5 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Mon, 8 Feb 2021 17:09:16 +0000 Subject: [PATCH 1/5] add port prompt --- pkg/app/run_test.go | 1 + pkg/compute/init.go | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/app/run_test.go b/pkg/app/run_test.go index 9b1603fff..385a5ce04 100644 --- a/pkg/app/run_test.go +++ b/pkg/app/run_test.go @@ -285,6 +285,7 @@ COMMANDS package --backend=BACKEND A hostname, IPv4, or IPv6 address for the package backend + --port=PORT A port number for the package backend compute build [] Build a Compute@Edge package locally diff --git a/pkg/compute/init.go b/pkg/compute/init.go index b749d8e1e..d41066058 100644 --- a/pkg/compute/init.go +++ b/pkg/compute/init.go @@ -78,6 +78,7 @@ type InitCommand struct { path string domain string backend string + port uint } // NewInitCommand returns a usable command registered under the parent. @@ -96,8 +97,9 @@ func NewInitCommand(parent common.Registerer, client api.HTTPClient, globals *co c.CmdClause.Flag("branch", "Git branch name to clone from package template repository").Hidden().StringVar(&c.branch) c.CmdClause.Flag("tag", "Git tag name to clone from package template repository").Hidden().StringVar(&c.tag) c.CmdClause.Flag("path", "Destination to write the new package, defaulting to the current directory").Short('p').StringVar(&c.path) - c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.path) - c.CmdClause.Flag("backend", "A hostname, IPv4, or IPv6 address for the package backend").StringVar(&c.path) + c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.domain) + c.CmdClause.Flag("backend", "A hostname, IPv4, or IPv6 address for the package backend").StringVar(&c.backend) + c.CmdClause.Flag("port", "A port number for the package backend").UintVar(&c.port) return &c } @@ -296,6 +298,22 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { } } + if c.port == 0 { + c.port = 80 + + input, err := text.Input(out, "Backend port number: [80] ", in) + if err != nil { + return fmt.Errorf("error reading input %w", err) + } + + portnumber, err := strconv.Atoi(input) + if err != nil { + return fmt.Errorf("error converting input %w", err) + } + + c.port = uint(portnumber) + } + text.Break(out) if !c.Globals.Verbose() { @@ -370,6 +388,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { ServiceVersion: version, Name: c.backend, Address: c.backend, + Port: c.port, }) if err != nil { return fmt.Errorf("error creating backend: %w", err) From fda7a528d8c7dbf6439e6588f3e0cceb9da23636 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Mon, 8 Feb 2021 17:19:06 +0000 Subject: [PATCH 2/5] rename flag to be explicitly tied to backend --- pkg/app/run_test.go | 3 ++- pkg/compute/init.go | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/app/run_test.go b/pkg/app/run_test.go index 385a5ce04..88323e82a 100644 --- a/pkg/app/run_test.go +++ b/pkg/app/run_test.go @@ -285,7 +285,8 @@ COMMANDS package --backend=BACKEND A hostname, IPv4, or IPv6 address for the package backend - --port=PORT A port number for the package backend + --backend-port=BACKEND-PORT + A port number for the package backend compute build [] Build a Compute@Edge package locally diff --git a/pkg/compute/init.go b/pkg/compute/init.go index d41066058..12cacd03e 100644 --- a/pkg/compute/init.go +++ b/pkg/compute/init.go @@ -69,16 +69,16 @@ type StarterKit struct { // InitCommand initializes a Compute@Edge project package on the local machine. type InitCommand struct { common.Base - client api.HTTPClient - manifest manifest.Data - language string - from string - branch string - tag string - path string - domain string - backend string - port uint + client api.HTTPClient + manifest manifest.Data + language string + from string + branch string + tag string + path string + domain string + backend string + backendPort uint } // NewInitCommand returns a usable command registered under the parent. @@ -99,7 +99,7 @@ func NewInitCommand(parent common.Registerer, client api.HTTPClient, globals *co c.CmdClause.Flag("path", "Destination to write the new package, defaulting to the current directory").Short('p').StringVar(&c.path) c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.domain) c.CmdClause.Flag("backend", "A hostname, IPv4, or IPv6 address for the package backend").StringVar(&c.backend) - c.CmdClause.Flag("port", "A port number for the package backend").UintVar(&c.port) + c.CmdClause.Flag("backend-port", "A port number for the package backend").UintVar(&c.backendPort) return &c } @@ -298,8 +298,8 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { } } - if c.port == 0 { - c.port = 80 + if c.backendPort == 0 { + c.backendPort = 80 input, err := text.Input(out, "Backend port number: [80] ", in) if err != nil { @@ -311,7 +311,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { return fmt.Errorf("error converting input %w", err) } - c.port = uint(portnumber) + c.backendPort = uint(portnumber) } text.Break(out) @@ -388,7 +388,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { ServiceVersion: version, Name: c.backend, Address: c.backend, - Port: c.port, + Port: c.backendPort, }) if err != nil { return fmt.Errorf("error creating backend: %w", err) From cacd5d84db8f4ec153ee6584745b8953c3f48720 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Mon, 8 Feb 2021 17:50:52 +0000 Subject: [PATCH 3/5] add flag to prevent integration tests failing because of missing prompt --- pkg/compute/compute_integration_test.go | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pkg/compute/compute_integration_test.go b/pkg/compute/compute_integration_test.go index 1e38587ae..37771a93c 100644 --- a/pkg/compute/compute_integration_test.go +++ b/pkg/compute/compute_integration_test.go @@ -51,7 +51,7 @@ func TestInit(t *testing.T) { }, { name: "unkown repository", - args: []string{"compute", "init", "--from", "https://example.com/template"}, + args: []string{"compute", "init", "--from", "https://example.com/template", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -67,7 +67,7 @@ func TestInit(t *testing.T) { }, { name: "create service error", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -78,7 +78,7 @@ func TestInit(t *testing.T) { }, { name: "create domain error", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -91,7 +91,7 @@ func TestInit(t *testing.T) { }, { name: "create backend error", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -106,7 +106,7 @@ func TestInit(t *testing.T) { }, { name: "with name", - args: []string{"compute", "init", "--name", "test"}, + args: []string{"compute", "init", "--name", "test", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -124,7 +124,7 @@ func TestInit(t *testing.T) { }, { name: "with service", - args: []string{"compute", "init", "-s", "test"}, + args: []string{"compute", "init", "-s", "test", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -144,7 +144,7 @@ func TestInit(t *testing.T) { }, { name: "with description", - args: []string{"compute", "init", "--description", "test"}, + args: []string{"compute", "init", "--description", "test", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -162,7 +162,7 @@ func TestInit(t *testing.T) { }, { name: "with author", - args: []string{"compute", "init", "--author", "test@example.com"}, + args: []string{"compute", "init", "--author", "test@example.com", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -180,7 +180,7 @@ func TestInit(t *testing.T) { }, { name: "with multiple authors", - args: []string{"compute", "init", "--author", "test1@example.com", "--author", "test2@example.com"}, + args: []string{"compute", "init", "--author", "test1@example.com", "--author", "test2@example.com", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -199,7 +199,7 @@ func TestInit(t *testing.T) { { name: "with from repository and branch", - args: []string{"compute", "init", "--from", "https://github.com/fastly/compute-starter-kit-rust-default.git", "--branch", "main"}, + args: []string{"compute", "init", "--from", "https://github.com/fastly/compute-starter-kit-rust-default.git", "--branch", "main", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -216,7 +216,7 @@ func TestInit(t *testing.T) { }, { name: "with existing package manifest", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{Token: "123"}, manifest: strings.Join([]string{ "service_id = \"1234\"", @@ -243,7 +243,7 @@ func TestInit(t *testing.T) { }, { name: "default", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{ Token: "123", Email: "test@example.com", @@ -275,7 +275,7 @@ func TestInit(t *testing.T) { }, { name: "with default name inferred from directory", - args: []string{"compute", "init"}, + args: []string{"compute", "init", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -288,7 +288,7 @@ func TestInit(t *testing.T) { }, { name: "with AssemblyScript language", - args: []string{"compute", "init", "--language", "assemblyscript"}, + args: []string{"compute", "init", "--language", "assemblyscript", "--backend-port", "80"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, From 6669cb5a2af2b479771ade6eab4fe41a1481250a Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Tue, 9 Feb 2021 11:46:08 +0000 Subject: [PATCH 4/5] refactor code to not require flag within integration tests --- pkg/compute/compute_integration_test.go | 28 ++++++++++++------------- pkg/compute/init.go | 5 ++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pkg/compute/compute_integration_test.go b/pkg/compute/compute_integration_test.go index 37771a93c..1e38587ae 100644 --- a/pkg/compute/compute_integration_test.go +++ b/pkg/compute/compute_integration_test.go @@ -51,7 +51,7 @@ func TestInit(t *testing.T) { }, { name: "unkown repository", - args: []string{"compute", "init", "--from", "https://example.com/template", "--backend-port", "80"}, + args: []string{"compute", "init", "--from", "https://example.com/template"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -67,7 +67,7 @@ func TestInit(t *testing.T) { }, { name: "create service error", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -78,7 +78,7 @@ func TestInit(t *testing.T) { }, { name: "create domain error", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -91,7 +91,7 @@ func TestInit(t *testing.T) { }, { name: "create backend error", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -106,7 +106,7 @@ func TestInit(t *testing.T) { }, { name: "with name", - args: []string{"compute", "init", "--name", "test", "--backend-port", "80"}, + args: []string{"compute", "init", "--name", "test"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -124,7 +124,7 @@ func TestInit(t *testing.T) { }, { name: "with service", - args: []string{"compute", "init", "-s", "test", "--backend-port", "80"}, + args: []string{"compute", "init", "-s", "test"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -144,7 +144,7 @@ func TestInit(t *testing.T) { }, { name: "with description", - args: []string{"compute", "init", "--description", "test", "--backend-port", "80"}, + args: []string{"compute", "init", "--description", "test"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -162,7 +162,7 @@ func TestInit(t *testing.T) { }, { name: "with author", - args: []string{"compute", "init", "--author", "test@example.com", "--backend-port", "80"}, + args: []string{"compute", "init", "--author", "test@example.com"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -180,7 +180,7 @@ func TestInit(t *testing.T) { }, { name: "with multiple authors", - args: []string{"compute", "init", "--author", "test1@example.com", "--author", "test2@example.com", "--backend-port", "80"}, + args: []string{"compute", "init", "--author", "test1@example.com", "--author", "test2@example.com"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -199,7 +199,7 @@ func TestInit(t *testing.T) { { name: "with from repository and branch", - args: []string{"compute", "init", "--from", "https://github.com/fastly/compute-starter-kit-rust-default.git", "--branch", "main", "--backend-port", "80"}, + args: []string{"compute", "init", "--from", "https://github.com/fastly/compute-starter-kit-rust-default.git", "--branch", "main"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -216,7 +216,7 @@ func TestInit(t *testing.T) { }, { name: "with existing package manifest", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{Token: "123"}, manifest: strings.Join([]string{ "service_id = \"1234\"", @@ -243,7 +243,7 @@ func TestInit(t *testing.T) { }, { name: "default", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{ Token: "123", Email: "test@example.com", @@ -275,7 +275,7 @@ func TestInit(t *testing.T) { }, { name: "with default name inferred from directory", - args: []string{"compute", "init", "--backend-port", "80"}, + args: []string{"compute", "init"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, @@ -288,7 +288,7 @@ func TestInit(t *testing.T) { }, { name: "with AssemblyScript language", - args: []string{"compute", "init", "--language", "assemblyscript", "--backend-port", "80"}, + args: []string{"compute", "init", "--language", "assemblyscript"}, configFile: config.File{Token: "123"}, api: mock.API{ GetTokenSelfFn: tokenOK, diff --git a/pkg/compute/init.go b/pkg/compute/init.go index 12cacd03e..9d7f2c5e9 100644 --- a/pkg/compute/init.go +++ b/pkg/compute/init.go @@ -299,8 +299,6 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { } if c.backendPort == 0 { - c.backendPort = 80 - input, err := text.Input(out, "Backend port number: [80] ", in) if err != nil { return fmt.Errorf("error reading input %w", err) @@ -308,7 +306,8 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { portnumber, err := strconv.Atoi(input) if err != nil { - return fmt.Errorf("error converting input %w", err) + text.Warning(out, "error converting input: %v. We'll use the default port number: [80].", err) + portnumber = 80 } c.backendPort = uint(portnumber) From 38d6e5635ea16601f82c6acbde731013bd08ee8a Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Tue, 9 Feb 2021 12:52:32 +0000 Subject: [PATCH 5/5] ensure default port 80 --- pkg/compute/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/compute/init.go b/pkg/compute/init.go index 9d7f2c5e9..2da801c14 100644 --- a/pkg/compute/init.go +++ b/pkg/compute/init.go @@ -295,6 +295,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) { } if c.backend == "" || c.backend == "originless" { c.backend = "127.0.0.1" + c.backendPort = uint(80) } }