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

feat(dockerized): add option to map additional port #31

Merged
merged 1 commit into from
Apr 3, 2022
Merged
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
60 changes: 50 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var Version string

var contains = util.Contains
var hasKey = util.HasKey

func main() {
err, exitCode := RunCli(os.Args[1:])
Expand All @@ -28,11 +29,12 @@ func main() {
func RunCli(args []string) (err error, exitCode int) {
dockerizedOptions, commandName, commandVersion, commandArgs := parseArguments(args)

var optionHelp = contains(dockerizedOptions, "--help") || contains(dockerizedOptions, "-h")
var optionVerbose = contains(dockerizedOptions, "--verbose") || contains(dockerizedOptions, "-v")
var optionShell = contains(dockerizedOptions, "--shell")
var optionBuild = contains(dockerizedOptions, "--build")
var optionVersion = contains(dockerizedOptions, "--version")
var optionHelp = hasKey(dockerizedOptions, "--help") || hasKey(dockerizedOptions, "-h")
var optionVerbose = hasKey(dockerizedOptions, "--verbose") || hasKey(dockerizedOptions, "-v")
var optionShell = hasKey(dockerizedOptions, "--shell")
var optionBuild = hasKey(dockerizedOptions, "--build")
var optionVersion = hasKey(dockerizedOptions, "--version")
var optionPort = hasKey(dockerizedOptions, "-p")

dockerizedRoot := dockerized.GetDockerizedRoot()
dockerized.NormalizeEnvironment(dockerizedRoot)
Expand Down Expand Up @@ -106,6 +108,29 @@ func RunCli(args []string) (err error, exitCode int) {
WorkingDir: containerCwd,
}

var serviceOptions []func(config *types.ServiceConfig) error

if optionPort {
var port = dockerizedOptions["-p"]
if port == "" {
return fmt.Errorf("port option requires a port number"), 1
}
if optionVerbose {
fmt.Printf("Mapping port: %s\n", port)
}
serviceOptions = append(serviceOptions, func(config *types.ServiceConfig) error {
if !strings.ContainsRune(port, ':') {
port = port + ":" + port
}
portConfig, err := types.ParsePortConfig(port)
if err != nil {
return err
}
config.Ports = portConfig
return nil
})
}

volumes := []types.ServiceVolumeConfig{
{
Type: "bind",
Expand Down Expand Up @@ -190,38 +215,53 @@ func RunCli(args []string) (err error, exitCode int) {
return dockerized.DockerRun(image, runOptions, volumes)
}

return dockerized.DockerComposeRun(project, runOptions, volumes)
return dockerized.DockerComposeRun(project, runOptions, volumes, serviceOptions...)
}

func parseArguments(args []string) ([]string, string, string, []string) {
func parseArguments(args []string) (map[string]string, string, string, []string) {
var options = []string{
"--shell",
"--build",
"-h",
"--help",
"-p",
"-v",
"--verbose",
"--version",
}

var optionsWithParameters = []string{
"-p",
}

commandName := ""
var commandArgs []string
var dockerizedOptions []string
var commandVersion string

var optionMap = make(map[string]string)
var optionBefore = ""

for _, arg := range args {
if arg[0] == '-' && commandName == "" {
if util.Contains(options, arg) {
dockerizedOptions = append(dockerizedOptions, arg)
var option = arg
dockerizedOptions = append(dockerizedOptions, option)
optionBefore = option
optionMap[option] = ""
} else {
fmt.Println("Unknown option:", arg)
os.Exit(1)
}
} else {
if commandName == "" {
if contains(optionsWithParameters, optionBefore) {
optionMap[optionBefore] = arg
} else if commandName == "" {
commandName = arg
} else {
commandArgs = append(commandArgs, arg)
}
optionBefore = ""
}
}
if strings.ContainsRune(commandName, ':') {
Expand All @@ -232,5 +272,5 @@ func parseArguments(args []string) ([]string, string, string, []string) {
commandVersion = "?"
}
}
return dockerizedOptions, commandName, commandVersion, commandArgs
return optionMap, commandName, commandVersion, commandArgs
}
10 changes: 9 additions & 1 deletion pkg/dockerized.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func DockerComposeBuild(composeFilePaths []string, buildOptions api.BuildOptions
return backend.Build(ctx, project, buildOptions)
}

func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) (error, int) {
func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig, serviceOptions ...func(config *types.ServiceConfig) error) (error, int) {
err := os.Chdir(project.WorkingDir)
if err != nil {
return err, 1
Expand All @@ -520,6 +520,14 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes

stopGracePeriod := types.Duration(1)
service.Volumes = append(service.Volumes, volumes...)

for _, serviceOption := range serviceOptions {
err = serviceOption(&service)
if err != nil {
return err, 1
}
}

service.StopGracePeriod = &stopGracePeriod
service.StdinOpen = true

Expand Down
16 changes: 9 additions & 7 deletions pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ func Help(composeFilePaths []string) error {
fmt.Println()

fmt.Println("Options:")
fmt.Println(" --build Rebuild the container before running it.")
fmt.Println(" --shell Start a shell inside the command container. Similar to `docker run --entrypoint=sh`.")
fmt.Println(" -v, --verbose Log what dockerized is doing.")
fmt.Println(" -h, --help Show this help.")
fmt.Println(" --build Rebuild the container before running it.")
fmt.Println(" --shell Start a shell inside the command container. Similar to `docker run --entrypoint=sh`.")
fmt.Println(" -p <port> Exposes given port to host, e.g. -p 8080")
fmt.Println(" -p <port>:<port> Maps host port to container port, e.g. -p 80:8080")
fmt.Println(" -v, --verbose Log what dockerized is doing.")
fmt.Println(" -h, --help Show this help.")
fmt.Println()

fmt.Println("Version:")
fmt.Println(" :<version> The version of the command to run, e.g. 1, 1.8, 1.8.1.")
fmt.Println(" :? List all available versions. E.g. `dockerized go:?`")
fmt.Println(" : Same as ':?' .")
fmt.Println(" :<version> The version of the command to run, e.g. 1, 1.8, 1.8.1.")
fmt.Println(" :? List all available versions. E.g. `dockerized go:?`")
fmt.Println(" : Same as ':?' .")
fmt.Println()

fmt.Println("Arguments:")
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ func Contains(s []string, str string) bool {

return false
}

func HasKey(m map[string]string, key string) bool {
_, ok := m[key]
return ok
}