Skip to content

Commit 2e99bb1

Browse files
committed
add compose metadata subcommand to return information about model runner cli usage as a Compose provider
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
1 parent dadaec2 commit 2e99bb1

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
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+
}

0 commit comments

Comments
 (0)