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 port prompt for compute init #203

Merged
merged 5 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 2 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ COMMANDS
package
--backend=BACKEND A hostname, IPv4, or IPv6 address for the
package backend
--backend-port=BACKEND-PORT
A port number for the package backend

compute build [<flags>]
Build a Compute@Edge package locally
Expand Down
28 changes: 14 additions & 14 deletions pkg/compute/compute_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Integralist marked this conversation as resolved.
Show resolved Hide resolved
configFile: config.File{Token: "123"},
api: mock.API{
GetTokenSelfFn: tokenOK,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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\"",
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
41 changes: 30 additions & 11 deletions pkg/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +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
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.
Expand All @@ -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)
Comment on lines +100 to +101
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed these flags were assigning values to the wrong variable 🤦🏻 so have fixed in this PR.

c.CmdClause.Flag("backend-port", "A port number for the package backend").UintVar(&c.backendPort)

return &c
}
Expand Down Expand Up @@ -296,6 +298,22 @@ 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)
}

portnumber, err := strconv.Atoi(input)
if err != nil {
return fmt.Errorf("error converting input %w", err)
}

c.backendPort = uint(portnumber)
}

text.Break(out)

if !c.Globals.Verbose() {
Expand Down Expand Up @@ -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.backendPort,
})
if err != nil {
return fmt.Errorf("error creating backend: %w", err)
Expand Down