From 8b2e37eb873cd46e416d4c05d532af23decd5514 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Sun, 3 Apr 2022 16:17:29 +0200 Subject: [PATCH] feat(dockerized): add -p option to map a port (#31) --- main.go | 60 +++++++++++++++++++++++++++++++++++++++-------- pkg/dockerized.go | 10 +++++++- pkg/help/help.go | 16 +++++++------ pkg/util/util.go | 5 ++++ 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 7e5aa2f..50d2562 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( var Version string var contains = util.Contains +var hasKey = util.HasKey func main() { err, exitCode := RunCli(os.Args[1:]) @@ -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) @@ -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", @@ -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, ':') { @@ -232,5 +272,5 @@ func parseArguments(args []string) ([]string, string, string, []string) { commandVersion = "?" } } - return dockerizedOptions, commandName, commandVersion, commandArgs + return optionMap, commandName, commandVersion, commandArgs } diff --git a/pkg/dockerized.go b/pkg/dockerized.go index d04ea87..d93f240 100644 --- a/pkg/dockerized.go +++ b/pkg/dockerized.go @@ -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 @@ -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 diff --git a/pkg/help/help.go b/pkg/help/help.go index bcee6f7..a1713e5 100644 --- a/pkg/help/help.go +++ b/pkg/help/help.go @@ -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 Exposes given port to host, e.g. -p 8080") + fmt.Println(" -p : 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(" : 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(" : 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:") diff --git a/pkg/util/util.go b/pkg/util/util.go index 1e8813e..9ce81ea 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -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 +}