Skip to content

Commit

Permalink
feat: refactor cli parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Nov 29, 2024
1 parent 0c143a9 commit 076d2ea
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 853 deletions.
97 changes: 0 additions & 97 deletions cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,6 @@ var dockerCompatCmds = map[string]string{
"buildx": "build version",
}

var aliasMap = map[string]string{
"build": "image build",
"run": "container run",
"cp": "container cp",
}

var commandHandlerMap = map[string]commandHandler{

Check failure on line 207 in cmd/finch/nerdctl.go

View workflow job for this annotation

GitHub Actions / lint

var `commandHandlerMap` is unused (unused)
"buildx": handleBuildx,
"inspect": handleDockerCompatInspect,
Expand All @@ -219,33 +213,6 @@ var argHandlerMap = map[string]map[string]argHandler{
"image build": {
"--load": handleDockerBuildLoad,
},
"container run": {
"--mount": handleBindMounts,
},
}

var cmdFlagSetMap = map[string]map[string]sets.Set[string]{
"container run": {
"shortBoolFlags": sets.New[string]("-d", "-i", "-t"),
"longBoolFlags": sets.New[string](
"--detach", "--init", "--interactive", "--oom-kill-disable",
"--privileged", "--read-only", "--rm", "--rootfs", "--tty", "--sig-proxy"),
"shortArgFlags": sets.New[string]("-e", "-h", "-m", "-u", "-w", "-p", "-l", "-v"),
},
"exec": {
"shortBoolFlags": sets.New[string]("-d", "-i", "-t"),
"longBoolFlags": sets.New[string](
"--detach", "--init", "--interactive", "--oom-kill-disable",
"--privileged", "--read-only", "--rm", "--rootfs", "--tty"),
"shortArgFlags": sets.New[string]("-e", "-h", "-m", "-u", "-w", "-p", "-l", "-v"),
},
"compose": {
"shortBoolFlags": sets.New[string]("-d", "-i", "-t"),
"longBoolFlags": sets.New[string](
"--detach", "--init", "--interactive", "--oom-kill-disable",
"--privileged", "--read-only", "--rm", "--rootfs", "--tty"),
"shortArgFlags": sets.New[string]("-e", "-h", "-m", "-u", "-w", "-p", "-l", "-v"),
},
}

// converts "docker build --load" flag to "nerdctl build --output=type=docker".
Expand Down Expand Up @@ -344,67 +311,3 @@ func handleDockerCompatInspect(_ NerdctlCommandSystemDeps, fc *config.Finch, cmd

return nil
}

// handles the argument & value of --mount option
//
// invokes OS specific path handler for source path of the bind mount
// and removes the consistency key-value entity from value
func handleBindMounts(systemDeps 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")

// Invoke the OS specific path handler
err := handleBindMountPath(systemDeps, m)
if err != nil {
return err
}

// 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
}
14 changes: 0 additions & 14 deletions cmd/finch/nerdctl_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,3 @@ func resolveIP(host string, logger flog.Logger, _ command.Creator) (string, erro
}
return host, nil
}

func handleBindMountPath(_ NerdctlCommandSystemDeps, _ map[string]string) error {
// Do nothing by default
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, ",")
}
45 changes: 1 addition & 44 deletions cmd/finch/nerdctl_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import (

func (nc *nerdctlCommand) run(cmdName string, args []string) error {
var (
hasCmdHandler, hasArgHandler bool
cmdHandler commandHandler
aMap map[string]argHandler
err error
inspectType string
inspectType string
)

// eat the debug arg, and set the log level to avoid nerdctl parsing this flag
Expand All @@ -31,45 +27,6 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
nc.logger.SetLevel(flog.Debug)
}

alias, hasAlias := aliasMap[cmdName]
if hasAlias {
cmdHandler, hasCmdHandler = commandHandlerMap[alias]
aMap, hasArgHandler = argHandlerMap[alias]
} else {
cmdHandler, hasCmdHandler = commandHandlerMap[cmdName]
aMap, hasArgHandler = argHandlerMap[cmdName]

if !hasArgHandler && len(args) > 0 {
// for commands like image build, container run
key := fmt.Sprintf("%s %s", cmdName, args[0])
cmdHandler, hasCmdHandler = commandHandlerMap[key]
aMap, hasArgHandler = argHandlerMap[key]
}
}

// First check if the command has a command handler
if hasCmdHandler {
err := cmdHandler(nc.systemDeps, nc.fc, &cmdName, &args, &inspectType)
if err != nil {
return err
}
}

for i, arg := range args {
// Check if individual argument (and possibly following value) requires manipulation-in-place handling
if hasArgHandler {
// Check if argument for the command needs handling, sometimes it can be --file=<filename>
b, _, _ := strings.Cut(arg, "=")
h, ok := aMap[b]
if ok {
err = h(nc.systemDeps, nc.fc, args, i)
if err != nil {
return err
}
}
}
}

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

0 comments on commit 076d2ea

Please sign in to comment.