Skip to content

Commit

Permalink
Merge pull request #3 from pojntfx/add-disable-mount-flag
Browse files Browse the repository at this point in the history
[WIP] Add `readOnly` and `mount` flags
  • Loading branch information
pojntfx authored Sep 2, 2021
2 parents ca29bfd + ed3b2ce commit 8656dbd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ See https://github.com/pojntfx/hydrun for more information.

Usage: hydrun [OPTION...] "<COMMAND...>"
-a, --arch string Comma-separated list of architectures to run on (default "amd64")
-c, --context string Directory to use in the container
-c, --context string Directory to use in the container (default is the current working directory)
-e, --extra-args string Extra arguments to pass to the Docker command
-i, --it Attach stdin and setup a TTY
-j, --jobs int Maximum amount of parallel jobs (default 1)
-m, --mount Enable mounting the directory specified with the context flag (default true)
-o, --os string Comma-separated list of operating systems (Docker images) to run on (default "debian")
-p, --pull Always pull the specified tags of the operating systems (Docker images)
-q, --quiet Disable logging executed commands
-r, --readyOnly Mount the directory specified as read-only
```

## Contributing
Expand Down
52 changes: 37 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ Usage: %s [OPTION...] "<COMMAND...>"
osFlag := pflag.StringP("os", "o", "debian", "Comma-separated list of operating systems (Docker images) to run on")
jobFlag := pflag.Int64P("jobs", "j", 1, "Maximum amount of parallel jobs")
itFlag := pflag.BoolP("it", "i", false, "Attach stdin and setup a TTY")
contextFlag := pflag.StringP("context", "c", "", "Directory to use in the container")
contextFlag := pflag.StringP("context", "c", "", "Directory to use in the container (default is the current working directory)")
extraArgs := pflag.StringP("extra-args", "e", "", "Extra arguments to pass to the Docker command")
pullFlag := pflag.BoolP("pull", "p", false, "Always pull the specified tags of the operating systems (Docker images)")
quietFlag := pflag.BoolP("quiet", "q", false, "Disable logging executed commands")
mountFlag := pflag.BoolP("mount", "m", true, "Enable mounting the directory specified with the context flag")
readOnlyFlag := pflag.BoolP("readyOnly", "r", false, "Mount the directory specified as read-only")

pflag.Parse()

Expand Down Expand Up @@ -139,22 +141,42 @@ Usage: %s [OPTION...] "<COMMAND...>"

go func(t Target) {
// Construct the arguments
dockerArgs := fmt.Sprintf(`run %v %v:/data:z --platform linux/%v%v %v /bin/sh -c`, func() string {
// Attach stdin and setup a TTY
if *itFlag {
return "-it -v"
}
dockerArgs := fmt.Sprintf(
`run %v%v--platform linux/%v%v %v /bin/sh -c`,
func() string {
// Attach stdin and setup a TTY
if *itFlag {
return "-it "
}

return "-v"
}(), pwd, t.Architecture, func() string {
args := *extraArgs
if args != "" {
args = " " + args
}
return ""
}(),
func() string {
if *mountFlag {
if *readOnlyFlag {
return fmt.Sprintf("-v %v:/data:ro ", pwd)
}

return fmt.Sprintf("-v %v:/data:z ", pwd)
}

return ""
}(),
t.Architecture,
func() string {
args := *extraArgs
if args != "" {
args = " " + args
}

return args
}(), getImageNameWithSuffix(t.OS, t.Architecture))
commandArgs := fmt.Sprintf(`cd /data && %v`, t.Command)
return args
}(),
getImageNameWithSuffix(t.OS, t.Architecture),
)
commandArgs := t.Command
if *mountFlag {
commandArgs = fmt.Sprintf(`cd /data && %v`, t.Command)
}

// Construct the command
cmd := exec.Command("docker", append(strings.Split(dockerArgs, " "), commandArgs)...)
Expand Down

0 comments on commit 8656dbd

Please sign in to comment.