Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit a749f65

Browse files
committed
add compose metadata subcommand to return information about model runner cli usage as a Compose provider
Signed-off-by: Guillaume Lours <[email protected]>
1 parent dadaec2 commit a749f65

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

commands/compose.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"github.com/spf13/pflag"
78
"slices"
89
"strconv"
910
"strings"
@@ -18,8 +19,9 @@ func newComposeCmd() *cobra.Command {
1819
c := &cobra.Command{
1920
Use: "compose EVENT",
2021
}
21-
c.AddCommand(newUpCommand())
22-
c.AddCommand(newDownCommand())
22+
upCmd := newUpCommand()
23+
downCmd := newDownCommand()
24+
c.AddCommand(upCmd, downCmd, newMetadataCommand(upCmd, downCmd))
2325
c.Hidden = true
2426
c.PersistentFlags().String("project-name", "", "compose project name") // unused by model
2527

@@ -62,19 +64,40 @@ func newUpCommand() *cobra.Command {
6264
},
6365
}
6466
c.Flags().StringArrayVar(&models, "model", nil, "model to use")
67+
_ = c.MarkFlagRequired("model")
6568
return c
6669
}
6770

6871
func newDownCommand() *cobra.Command {
69-
var model []string
7072
c := &cobra.Command{
7173
Use: "down",
7274
RunE: func(cmd *cobra.Command, args []string) error {
7375
// No required cleanup on down
7476
return nil
7577
},
7678
}
77-
c.Flags().StringArrayVar(&model, "model", nil, "model to use")
79+
return c
80+
}
81+
82+
func newMetadataCommand(upCmd, downCmd *cobra.Command) *cobra.Command {
83+
c := &cobra.Command{
84+
Use: "metadata",
85+
Short: "Metadata for Docker Compose",
86+
RunE: func(cmd *cobra.Command, args []string) error {
87+
providerMetadata := ProviderMetadata{
88+
Description: "Docker Model Runner",
89+
}
90+
providerMetadata.Up = commandParameters(upCmd)
91+
providerMetadata.Down = commandParameters(downCmd)
92+
93+
jsonMetadata, err := json.Marshal(providerMetadata)
94+
if err != nil {
95+
return err
96+
}
97+
fmt.Printf(string(jsonMetadata))
98+
return nil
99+
},
100+
}
78101
return c
79102
}
80103

@@ -155,3 +178,36 @@ func sendInfo(s string) error {
155178
_, err = fmt.Println(string(marshal))
156179
return err
157180
}
181+
182+
func commandParameters(cmd *cobra.Command) CommandMetadata {
183+
cmdMetadata := CommandMetadata{}
184+
cmd.Flags().VisitAll(func(f *pflag.Flag) {
185+
_, isRequired := f.Annotations[cobra.BashCompOneRequiredFlag]
186+
cmdMetadata.Parameters = append(cmdMetadata.Parameters, ParameterMetadata{
187+
Name: f.Name,
188+
Description: f.Usage,
189+
Required: isRequired,
190+
Type: f.Value.Type(),
191+
Default: f.DefValue,
192+
})
193+
})
194+
return cmdMetadata
195+
}
196+
197+
type ProviderMetadata struct {
198+
Description string `json:"description"`
199+
Up CommandMetadata `json:"up"`
200+
Down CommandMetadata `json:"down"`
201+
}
202+
203+
type CommandMetadata struct {
204+
Parameters []ParameterMetadata `json:"parameters"`
205+
}
206+
207+
type ParameterMetadata struct {
208+
Name string `json:"name"`
209+
Description string `json:"description"`
210+
Required bool `json:"required"`
211+
Type string `json:"type"`
212+
Default string `json:"default,omitempty"`
213+
}

docs/reference/docker_model_compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ pname: docker model
33
plink: docker_model.yaml
44
cname:
55
- docker model compose down
6+
- docker model compose metadata
67
- docker model compose up
78
clink:
89
- docker_model_compose_down.yaml
10+
- docker_model_compose_metadata.yaml
911
- docker_model_compose_up.yaml
1012
options:
1113
- option: project-name

docs/reference/docker_model_compose_down.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ command: docker model compose down
22
usage: docker model compose down
33
pname: docker model compose
44
plink: docker_model_compose.yaml
5-
options:
6-
- option: model
7-
value_type: stringArray
8-
default_value: '[]'
9-
description: model to use
10-
deprecated: false
11-
hidden: false
12-
experimental: false
13-
experimentalcli: false
14-
kubernetes: false
15-
swarm: false
165
inherited_options:
176
- option: project-name
187
value_type: string
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
command: docker model compose metadata
2+
short: Metadata for Docker Compose
3+
long: Metadata for Docker Compose
4+
usage: docker model compose metadata
5+
pname: docker model compose
6+
plink: docker_model_compose.yaml
7+
inherited_options:
8+
- option: project-name
9+
value_type: string
10+
description: compose project name
11+
deprecated: false
12+
hidden: false
13+
experimental: false
14+
experimentalcli: false
15+
kubernetes: false
16+
swarm: false
17+
deprecated: false
18+
hidden: true
19+
experimental: false
20+
experimentalcli: true
21+
kubernetes: false
22+
swarm: false
23+

0 commit comments

Comments
 (0)