Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"strconv"

"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
Expand Down Expand Up @@ -33,6 +34,8 @@ microcks start --driver [driver you wnat either 'docker' or 'podman']
# Define name of your microcks container/instance
microcks start --name [name of you container/instance]`,
Run: func(cmd *cobra.Command, args []string) {
err := validateStartPort(hostPort)
errors.CheckError(err)

configFile := globalClientOpts.ConfigPath
localConfig, err := config.ReadLocalConfig(configFile)
Expand Down Expand Up @@ -150,3 +153,11 @@ microcks start --name [name of you container/instance]`,
startCmd.Flags().StringVar(&driver, "driver", "docker", "use --driver to change driver from docker to podman")
return startCmd
}

func validateStartPort(port string) error {
portNum, err := strconv.Atoi(port)
if err != nil || portNum < 1 || portNum > 65535 {
return fmt.Errorf("--port must be a number between 1 and 65535, got %q", port)
}
return nil
}
35 changes: 35 additions & 0 deletions cmd/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateStartPort(t *testing.T) {
tests := []struct {
name string
port string
wantErr bool
}{
{name: "lowest valid port", port: "1"},
{name: "default port", port: "8585"},
{name: "highest valid port", port: "65535"},
{name: "non numeric port", port: "abc", wantErr: true},
{name: "zero port", port: "0", wantErr: true},
{name: "negative port", port: "-1", wantErr: true},
{name: "port above range", port: "65536", wantErr: true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateStartPort(tt.port)
if tt.wantErr {
require.EqualError(t, err, "--port must be a number between 1 and 65535, got "+strconv.Quote(tt.port))
return
}
require.NoError(t, err)
})
}
}
Loading