Skip to content

Commit 488d698

Browse files
committed
De-duplicate code
Common code de-duplicated Signed-off-by: Eric Curtin <[email protected]>
1 parent 7fdb650 commit 488d698

File tree

21 files changed

+373
-460
lines changed

21 files changed

+373
-460
lines changed

cmd/cli/commands/inspect.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@ func newInspectCmd() *cobra.Command {
1616
c := &cobra.Command{
1717
Use: "inspect MODEL",
1818
Short: "Display detailed information on one model",
19-
Args: func(cmd *cobra.Command, args []string) error {
20-
if len(args) != 1 {
21-
return fmt.Errorf(
22-
"'docker model inspect' requires 1 argument.\n\n" +
23-
"Usage: docker model inspect MODEL\n\n" +
24-
"See 'docker model inspect --help' for more information",
25-
)
26-
}
27-
return nil
28-
},
19+
Args: requireExactArgs(1, "inspect", "MODEL"),
2920
RunE: func(cmd *cobra.Command, args []string) error {
3021
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd)); err != nil {
3122
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/install-runner.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,12 @@ func newInstallRunner() *cobra.Command {
312312
},
313313
ValidArgsFunction: completion.NoComplete,
314314
}
315-
c.Flags().Uint16Var(&port, "port", 0,
316-
"Docker container port for Docker Model Runner (default: 12434 for Docker Engine, 12435 for Cloud mode)")
317-
c.Flags().StringVar(&host, "host", "127.0.0.1", "Host address to bind Docker Model Runner")
318-
c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda|rocm|musa|cann)")
319-
c.Flags().StringVar(&backend, "backend", "", backendUsage)
320-
c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner")
315+
addRunnerFlags(c, runnerFlagOptions{
316+
Port: &port,
317+
Host: &host,
318+
GpuMode: &gpuMode,
319+
Backend: &backend,
320+
DoNotTrack: &doNotTrack,
321+
})
321322
return c
322323
}

cmd/cli/commands/package.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,8 @@ func newPackagedCmd() *cobra.Command {
3535
"When packaging a Safetensors model, --safetensors-dir should point to a directory containing .safetensors files and config files (*.json, merges.txt). All files will be auto-discovered and config files will be packaged into a tar archive.\n" +
3636
"When packaging from an existing model using --from, you can modify properties like context size to create a variant of the original model.",
3737
Args: func(cmd *cobra.Command, args []string) error {
38-
if len(args) != 1 {
39-
return fmt.Errorf(
40-
"'docker model package' requires 1 argument.\n\n"+
41-
"Usage: docker model %s\n\n"+
42-
"See 'docker model package --help' for more information",
43-
cmd.Use,
44-
)
38+
if err := requireExactArgs(1, "package", "DIRECTORY")(cmd, args); err != nil {
39+
return err
4540
}
4641

4742
// Validate that exactly one of --gguf, --safetensors-dir, or --from is provided (mutually exclusive)

cmd/cli/commands/pull.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@ func newPullCmd() *cobra.Command {
1717
c := &cobra.Command{
1818
Use: "pull MODEL",
1919
Short: "Pull a model from Docker Hub or HuggingFace to your local environment",
20-
Args: func(cmd *cobra.Command, args []string) error {
21-
if len(args) != 1 {
22-
return fmt.Errorf(
23-
"'docker model pull' requires 1 argument.\n\n" +
24-
"Usage: docker model pull MODEL\n\n" +
25-
"See 'docker model pull --help' for more information",
26-
)
27-
}
28-
return nil
29-
},
20+
Args: requireExactArgs(1, "pull", "MODEL"),
3021
RunE: func(cmd *cobra.Command, args []string) error {
3122
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd)); err != nil {
3223
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/push.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ func newPushCmd() *cobra.Command {
1313
c := &cobra.Command{
1414
Use: "push MODEL",
1515
Short: "Push a model to Docker Hub",
16-
Args: func(cmd *cobra.Command, args []string) error {
17-
if len(args) != 1 {
18-
return fmt.Errorf(
19-
"'docker model push' requires 1 argument.\n\n" +
20-
"Usage: docker model push MODEL\n\n" +
21-
"See 'docker model push --help' for more information",
22-
)
23-
}
24-
return nil
25-
},
16+
Args: requireExactArgs(1, "push", "MODEL"),
2617
RunE: func(cmd *cobra.Command, args []string) error {
2718
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd)); err != nil {
2819
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/reinstall-runner.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ func newReinstallRunner() *cobra.Command {
2727
},
2828
ValidArgsFunction: completion.NoComplete,
2929
}
30-
c.Flags().Uint16Var(&port, "port", 0,
31-
"Docker container port for Docker Model Runner (default: 12434 for Docker Engine, 12435 for Cloud mode)")
32-
c.Flags().StringVar(&host, "host", "127.0.0.1", "Host address to bind Docker Model Runner")
33-
c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda|musa|rocm|cann)")
34-
c.Flags().StringVar(&backend, "backend", "", backendUsage)
35-
c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner")
30+
addRunnerFlags(c, runnerFlagOptions{
31+
Port: &port,
32+
Host: &host,
33+
GpuMode: &gpuMode,
34+
Backend: &backend,
35+
DoNotTrack: &doNotTrack,
36+
})
3637
return c
3738
}

cmd/cli/commands/restart-runner.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ func newRestartRunner() *cobra.Command {
3333
},
3434
ValidArgsFunction: completion.NoComplete,
3535
}
36-
c.Flags().Uint16Var(&port, "port", 0,
37-
"Docker container port for Docker Model Runner (default: 12434 for Docker Engine, 12435 for Cloud mode)")
38-
c.Flags().StringVar(&host, "host", "127.0.0.1", "Host address to bind Docker Model Runner")
39-
c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda|musa|rocm|cann)")
40-
c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner")
36+
addRunnerFlags(c, runnerFlagOptions{
37+
Port: &port,
38+
Host: &host,
39+
GpuMode: &gpuMode,
40+
DoNotTrack: &doNotTrack,
41+
})
4142
return c
4243
}

cmd/cli/commands/rm.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,7 @@ func newRemoveCmd() *cobra.Command {
1414
c := &cobra.Command{
1515
Use: "rm [MODEL...]",
1616
Short: "Remove local models downloaded from Docker Hub",
17-
Args: func(cmd *cobra.Command, args []string) error {
18-
if len(args) < 1 {
19-
return fmt.Errorf(
20-
"'docker model rm' requires at least 1 argument.\n\n" +
21-
"Usage: docker model rm [MODEL...]\n\n" +
22-
"See 'docker model rm --help' for more information",
23-
)
24-
}
25-
return nil
26-
},
17+
Args: requireMinArgs(1, "rm", "[MODEL...]"),
2718
RunE: func(cmd *cobra.Command, args []string) error {
2819
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd)); err != nil {
2920
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/run.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -730,17 +730,7 @@ func newRunCmd() *cobra.Command {
730730
},
731731
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
732732
}
733-
c.Args = func(cmd *cobra.Command, args []string) error {
734-
if len(args) < 1 {
735-
return fmt.Errorf(
736-
"'docker model run' requires at least 1 argument.\n\n" +
737-
"Usage: docker model run " + cmdArgs + "\n\n" +
738-
"See 'docker model run --help' for more information",
739-
)
740-
}
741-
742-
return nil
743-
}
733+
c.Args = requireMinArgs(1, "run", cmdArgs)
744734

745735
c.Flags().BoolVar(&debug, "debug", false, "Enable debug logging")
746736
c.Flags().BoolVar(&ignoreRuntimeMemoryCheck, "ignore-runtime-memory-check", false, "Do not block pull if estimated runtime memory for model exceeds system resources.")

cmd/cli/commands/start-runner.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ func newStartRunner() *cobra.Command {
2424
},
2525
ValidArgsFunction: completion.NoComplete,
2626
}
27-
c.Flags().Uint16Var(&port, "port", 0,
28-
"Docker container port for Docker Model Runner (default: 12434 for Docker Engine, 12435 for Cloud mode)")
29-
c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda|musa|rocm|cann)")
30-
c.Flags().StringVar(&backend, "backend", "", backendUsage)
31-
c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner")
27+
addRunnerFlags(c, runnerFlagOptions{
28+
Port: &port,
29+
GpuMode: &gpuMode,
30+
Backend: &backend,
31+
DoNotTrack: &doNotTrack,
32+
})
3233
return c
3334
}

0 commit comments

Comments
 (0)