cmd/sketch: add mount flag for container volumes
Add a mount flag that simplifies mounting host directories into the container.
When specified with format /path/on/host:/path/in/container it's automatically
converted to a docker volume mount argument (-v) in the docker invocation.
This simplifies volume mounting compared to the more generic docker-args flag.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: s67251d45fd28831ek
diff --git a/cmd/sketch/main.go b/cmd/sketch/main.go
index 4a18e51..140c83d 100644
--- a/cmd/sketch/main.go
+++ b/cmd/sketch/main.go
@@ -136,6 +136,26 @@
}
// CLIFlags holds all command-line arguments
+// StringSliceFlag is a custom flag type that allows for repeated flag values.
+// It collects all values into a slice.
+type StringSliceFlag []string
+
+// String returns the string representation of the flag value.
+func (f *StringSliceFlag) String() string {
+ return strings.Join(*f, ",")
+}
+
+// Set adds a value to the flag.
+func (f *StringSliceFlag) Set(value string) error {
+ *f = append(*f, value)
+ return nil
+}
+
+// Get returns the flag values.
+func (f *StringSliceFlag) Get() any {
+ return []string(*f)
+}
+
type CLIFlags struct {
addr string
skabandAddr string
@@ -168,6 +188,7 @@
outsideWorkingDir string
sketchBinaryLinux string
dockerArgs string
+ mounts StringSliceFlag
termUI bool
}
@@ -195,6 +216,7 @@
flag.BoolVar(&flags.forceRebuild, "force-rebuild-container", false, "rebuild Docker container")
flag.StringVar(&flags.initialCommit, "initial-commit", "HEAD", "the git commit reference to use as starting point (incompatible with -unsafe)")
flag.StringVar(&flags.dockerArgs, "docker-args", "", "additional arguments to pass to the docker create command (e.g., --memory=2g --cpus=2)")
+ flag.Var(&flags.mounts, "mount", "volume to mount in the container in format /path/on/host:/path/in/container (can be repeated)")
flag.BoolVar(&flags.termUI, "termui", true, "enable terminal UI")
// Flags geared towards sketch developers or sketch internals:
@@ -289,6 +311,7 @@
InitialCommit: flags.initialCommit,
Verbose: flags.verbose,
DockerArgs: flags.dockerArgs,
+ Mounts: flags.mounts,
ExperimentFlag: flags.experimentFlag.String(),
TermUI: flags.termUI,
}
diff --git a/dockerimg/dockerimg.go b/dockerimg/dockerimg.go
index a6d012e..4b50ae1 100644
--- a/dockerimg/dockerimg.go
+++ b/dockerimg/dockerimg.go
@@ -99,6 +99,9 @@
// DockerArgs are additional arguments to pass to the docker create command
DockerArgs string
+ // Mounts specifies volumes to mount in the container in format /path/on/host:/path/in/container
+ Mounts []string
+
// ExperimentFlag contains the experimental features to enable
ExperimentFlag string
@@ -484,6 +487,13 @@
}
// colima does this by default, but Linux docker seems to need this set explicitly
cmdArgs = append(cmdArgs, "--add-host", "host.docker.internal:host-gateway")
+
+ // Add volume mounts if specified
+ for _, mount := range config.Mounts {
+ if mount != "" {
+ cmdArgs = append(cmdArgs, "-v", mount)
+ }
+ }
cmdArgs = append(
cmdArgs,
imgName,