-
Notifications
You must be signed in to change notification settings - Fork 269
[services] Let users set the port for process-compose #2299
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ type ProcessComposeOpts struct { | |
BinPath string | ||
ExtraFlags []string | ||
Background bool | ||
PCPort int | ||
} | ||
|
||
func newGlobalProcessComposeConfig() *globalProcessComposeConfig { | ||
|
@@ -128,10 +129,13 @@ func StartProcessManager( | |
config := readGlobalProcessComposeJSON(configFile) | ||
config.File = configFile | ||
|
||
// Get the port to use for this project | ||
port, err := getAvailablePort() | ||
port, err := selectPort(processComposeConfig.PCPort) | ||
if err != nil { | ||
return err | ||
return fmt.Errorf("failed to select port: %v", err) | ||
} | ||
|
||
if !isPortAvailable(port) { | ||
return fmt.Errorf("port %d is already in use", port) | ||
|
||
} | ||
|
||
// Start building the process-compose command | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
@@ -60,6 +63,34 @@ func getAvailablePort() (int, error) { | |
return 0, errors.New("no available port") | ||
} | ||
|
||
func selectPort(configPort int) (int, error) { | ||
if configPort != 0 { | ||
return configPort, nil | ||
} | ||
|
||
if portStr, exists := os.LookupEnv("PC_PORT_NUM"); exists { | ||
|
||
port, err := strconv.Atoi(portStr) | ||
if err != nil { | ||
return 0, fmt.Errorf("invalid PC_PORT_NUM environment variable: %v", err) | ||
} | ||
if port <= 0 { | ||
return 0, fmt.Errorf("invalid PC_PORT_NUM environment variable: ports cannot be less than 0") | ||
} | ||
return port, nil | ||
|
||
} | ||
|
||
return getAvailablePort() | ||
} | ||
|
||
func isAllowed(port int) bool { | ||
return port > 1024 && disallowedPorts[port] == "" | ||
} | ||
|
||
func isPortAvailable(port int) bool { | ||
|
||
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
return false | ||
} | ||
ln.Close() | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProcesssComposePort