Skip to content

Commit e57e90c

Browse files
authored
Merge pull request #389 from docker/dedup4
De-duplicate code
2 parents f998715 + 0e8fdfe commit e57e90c

27 files changed

+387
-470
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), false); err != nil {
3122
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/install-runner.go

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

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", "MODEL")(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), false); 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), false); err != nil {
2819
return fmt.Errorf("unable to initialize standalone model runner: %w", err)

cmd/cli/commands/reinstall-runner.go

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

cmd/cli/commands/restart-runner.go

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

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), false); 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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ func newStartRunner() *cobra.Command {
2525
},
2626
ValidArgsFunction: completion.NoComplete,
2727
}
28-
c.Flags().Uint16Var(&port, "port", 0,
29-
"Docker container port for Docker Model Runner (default: 12434 for Docker Engine, 12435 for Cloud mode)")
30-
c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda|musa|rocm|cann)")
31-
c.Flags().StringVar(&backend, "backend", "", backendUsage)
32-
c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner")
33-
c.Flags().BoolVar(&debug, "debug", false, "Enable debug logging")
28+
addRunnerFlags(c, runnerFlagOptions{
29+
Port: &port,
30+
GpuMode: &gpuMode,
31+
Backend: &backend,
32+
DoNotTrack: &doNotTrack,
33+
Debug: &debug,
34+
})
3435
return c
3536
}

0 commit comments

Comments
 (0)