Skip to content

Commit

Permalink
Add BindMount handler for Linux
Browse files Browse the repository at this point in the history
Add handleBindMount to remove consistency entity\nRevise comments for linting and clarity

Signed-off-by: Sam Chew <[email protected]>
  • Loading branch information
chews93319 committed Oct 2, 2024
1 parent c004516 commit ca3c2e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
11 changes: 2 additions & 9 deletions cmd/finch/nerdctl_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func resolveIP(host string, logger flog.Logger, _ command.Creator) (string, erro
return host, nil
}

// removes the consistency key-value entity from --mount.
func handleBindMounts(_ NerdctlCommandSystemDeps, _ *config.Finch, nerdctlCmdArgs []string, index int) error {
prefix := nerdctlCmdArgs[index]
var (
Expand All @@ -68,14 +67,8 @@ func handleBindMounts(_ NerdctlCommandSystemDeps, _ *config.Finch, nerdctlCmdArg
}
}

// This is where the 'consistency=cached' strings should be removed....
// "consistency will be one of the keys in the following map"

// eg --mount type=bind,source="$(pwd)"/target,target=/app,readonly
// eg --mount type=bind,
// source=/Users/stchew/projs/arbtest_devcontainers_extensions,
// target=/workspaces/arbtest_devcontainers_extensions,
// consistency=cached
// eg --mount type=bind, source=${pwd}/source_dir, target=<path>/target_dir, consistency=cached
// https://docs.docker.com/storage/bind-mounts/#choose-the--v-or---mount-flag order does not matter, so convert to a map
entries := strings.Split(v, ",")
m := make(map[string]string)
Expand All @@ -93,7 +86,7 @@ func handleBindMounts(_ NerdctlCommandSystemDeps, _ *config.Finch, nerdctlCmdArg
return nil
}

// Remove 'consistency' key-value pair
// Remove 'consistency' key-value pair, if present
delete(m, "consistency")

// Convert to string representation
Expand Down
83 changes: 74 additions & 9 deletions cmd/finch/nerdctl_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"golang.org/x/exp/slices"

"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/config"
"github.com/runfinch/finch/pkg/flog"
)

func (nc *nerdctlCommand) run(cmdName string, args []string) error {

var (
hasCmdHandler, hasArgHandler bool
cmdHandler commandHandler
Expand Down Expand Up @@ -70,14 +70,12 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
}
}

// TODO: Extra manipulation if overwriting cmdName with alias
//splitName := strings.Split(cmdName, " ")
//cmdArgs := append([]string{splitName[0]}, splitName[1:]...)
//cmdArgs = append(cmdArgs, args...)

cmdArgs := append([]string{cmdName}, args...)
// Extra manipulation for cases that overwrite cmdName with alias
splitName := strings.Split(cmdName, " ")
cmdArgs := append([]string{splitName[0]}, splitName[1:]...)
cmdArgs = append(cmdArgs, args...)

if nc.shouldReplaceForHelp(cmdName, args) {
if nc.shouldReplaceForHelp(splitName[0], args) {
return nc.ncc.RunWithReplacingStdout(
[]command.Replacement{{Source: "nerdctl", Target: "finch"}},
cmdArgs...,
Expand All @@ -89,6 +87,73 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {

var osAliasMap = map[string]string{}

var osArgHandlerMap = map[string]map[string]argHandler{}
var osArgHandlerMap = map[string]map[string]argHandler{
"container run": {
"--mount": handleBindMounts,
},
}

var osCommandHandlerMap = map[string]commandHandler{}

func handleBindMounts(_ NerdctlCommandSystemDeps, _ *config.Finch, nerdctlCmdArgs []string, index int) error {
prefix := nerdctlCmdArgs[index]
var (
v string
found bool
before string
)
if strings.Contains(nerdctlCmdArgs[index], "=") {
before, v, found = strings.Cut(prefix, "=")
} else {
if (index + 1) < len(nerdctlCmdArgs) {
v = nerdctlCmdArgs[index+1]
} else {
return fmt.Errorf("invalid positional parameter for %s", prefix)
}
}

// eg --mount type=bind,source="$(pwd)"/target,target=/app,readonly
// eg --mount type=bind, source=${pwd}/source_dir, target=<path>/target_dir, consistency=cached
// https://docs.docker.com/storage/bind-mounts/#choose-the--v-or---mount-flag order does not matter, so convert to a map
entries := strings.Split(v, ",")
m := make(map[string]string)
ro := []string{}
for _, e := range entries {
parts := strings.Split(e, "=")
if len(parts) < 2 {
ro = append(ro, parts...)
} else {
m[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
// Check if type is bind mount, else return
if m["type"] != "bind" {
return nil
}

// Remove 'consistency' key-value pair, if present
delete(m, "consistency")

// Convert to string representation
s := mapToString(m)
// append read-only key if present
if len(ro) > 0 {
s = s + "," + strings.Join(ro, ",")
}
if found {
nerdctlCmdArgs[index] = fmt.Sprintf("%s=%s", before, s)
} else {
nerdctlCmdArgs[index+1] = s
}

return nil
}

func mapToString(m map[string]string) string {
var parts []string
for k, v := range m {
part := fmt.Sprintf("%s=%s", k, v)
parts = append(parts, part)
}
return strings.Join(parts, ",")
}
3 changes: 1 addition & 2 deletions cmd/finch/nerdctl_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ func handleBindMounts(systemDeps NerdctlCommandSystemDeps, _ *config.Finch, nerd
}

// e.g. --mount type=bind,source="$(pwd)"/target,target=/app,readonly
// e.g. --mount type=bind,source=/Users/stchew/projs/arbtest_devcontainers_extensions,
// target=/workspaces/arbtest_devcontainers_extensions,consistency=cached
// eg --mount type=bind, source="${pwd}"/source_dir, target=<path>/target_dir, consistency=cached
// https://docs.docker.com/storage/bind-mounts/#choose-the--v-or---mount-flag order does not matter, so convert to a map
entries := strings.Split(v, ",")
m := make(map[string]string)
Expand Down

0 comments on commit ca3c2e2

Please sign in to comment.