Skip to content

Commit dca1d17

Browse files
authored
Merge pull request #59 from dgageot/cleaning-commands
Cleaning commands
2 parents d2ba12d + 398a2e6 commit dca1d17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1075
-1070
lines changed

cmd/docker-mcp/catalog/add.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,24 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/spf13/cobra"
9-
108
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/yq"
119
)
1210

13-
type addOpts struct {
14-
Force bool
15-
}
16-
17-
func newAddCommand() *cobra.Command {
18-
opts := &addOpts{}
19-
cmd := &cobra.Command{
20-
Use: "add <catalog> <server-name> <catalog-file>",
21-
Short: "Add a server to your catalog",
22-
Args: cobra.ExactArgs(3),
23-
RunE: func(_ *cobra.Command, args []string) error {
24-
parsedArgs := parseAddArgs(args[0], args[1], args[2])
25-
if err := validateArgs(*parsedArgs); err != nil {
26-
return err
27-
}
28-
return runAdd(*parsedArgs, *opts)
29-
},
30-
Hidden: true,
31-
}
32-
flags := cmd.Flags()
33-
flags.BoolVar(&opts.Force, "force", false, "Overwrite existing server in the catalog")
34-
return cmd
35-
}
36-
3711
type ParsedAddArgs struct {
3812
Src string
3913
Dst string
4014
SeverName string
4115
}
4216

43-
func parseAddArgs(dst, src, catalogFile string) *ParsedAddArgs {
17+
func ParseAddArgs(dst, src, catalogFile string) *ParsedAddArgs {
4418
return &ParsedAddArgs{
4519
Src: catalogFile,
4620
Dst: dst,
4721
SeverName: src,
4822
}
4923
}
5024

51-
func validateArgs(args ParsedAddArgs) error {
25+
func ValidateArgs(args ParsedAddArgs) error {
5226
cfg, err := ReadConfig()
5327
if err != nil {
5428
return err
@@ -73,7 +47,7 @@ func validateArgs(args ParsedAddArgs) error {
7347
return nil
7448
}
7549

76-
func runAdd(args ParsedAddArgs, opts addOpts) error {
50+
func Add(args ParsedAddArgs, force bool) error {
7751
srcContent, err := os.ReadFile(args.Src)
7852
if err != nil {
7953
return err
@@ -90,7 +64,7 @@ func runAdd(args ParsedAddArgs, opts addOpts) error {
9064
return err
9165
}
9266
dstServerJSON, err := extractServerJSON(dstContentBefore, args.SeverName)
93-
if err == nil && len(dstServerJSON) > 4 && !opts.Force {
67+
if err == nil && len(dstServerJSON) > 4 && !force {
9468
fmt.Println(string(dstServerJSON))
9569
return fmt.Errorf("server %q already exists in catalog %q (use --force to overwrite)", args.SeverName, args.Dst)
9670
}

cmd/docker-mcp/catalog/catalog.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package catalog
33
import (
44
"fmt"
55

6-
"github.com/spf13/cobra"
76
"gopkg.in/yaml.v3"
87

98
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/yq"
@@ -18,25 +17,6 @@ var aliasToURL = map[string]string{
1817
DockerCatalogName: DockerCatalogURL,
1918
}
2019

21-
func NewCatalogCmd() *cobra.Command {
22-
cmd := &cobra.Command{
23-
Use: "catalog",
24-
Aliases: []string{"catalogs"},
25-
Short: "Manage the catalog",
26-
}
27-
cmd.AddCommand(newImportCmd())
28-
cmd.AddCommand(newLsCommand())
29-
cmd.AddCommand(newRmCommand())
30-
cmd.AddCommand(newUpdateCommand())
31-
cmd.AddCommand(newShowCommand())
32-
cmd.AddCommand(newForkCmd())
33-
cmd.AddCommand(newCreateCmd())
34-
cmd.AddCommand(newInitCmd())
35-
cmd.AddCommand(newAddCommand())
36-
cmd.AddCommand(newResetCommand())
37-
return cmd
38-
}
39-
4020
type MetaData struct {
4121
Name string `yaml:"name,omitempty"`
4222
DisplayName string `yaml:"displayName,omitempty"`

cmd/docker-mcp/catalog/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func ReadConfigWithDefaultCatalog(ctx context.Context) (*Config, error) {
6060
return cfg, nil
6161
}
6262

63-
if err := runImport(ctx, DockerCatalogName); err != nil {
63+
if err := Import(ctx, DockerCatalogName); err != nil {
6464
return nil, err
6565
}
6666

cmd/docker-mcp/catalog/create.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@ package catalog
22

33
import (
44
"fmt"
5-
6-
"github.com/spf13/cobra"
75
)
86

9-
func newCreateCmd() *cobra.Command {
10-
cmd := &cobra.Command{
11-
Use: "create <name>",
12-
Short: "Create a new catalog",
13-
Args: cobra.ExactArgs(1),
14-
RunE: func(_ *cobra.Command, args []string) error {
15-
return runCreate(args[0])
16-
},
17-
Hidden: true,
18-
}
19-
return cmd
20-
}
21-
22-
func runCreate(name string) error {
7+
func Create(name string) error {
238
cfg, err := ReadConfig()
249
if err != nil {
2510
return err

cmd/docker-mcp/catalog/fork.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@ package catalog
22

33
import (
44
"fmt"
5-
6-
"github.com/spf13/cobra"
75
)
86

9-
func newForkCmd() *cobra.Command {
10-
cmd := &cobra.Command{
11-
Use: "fork <src-catalog> <new-name>",
12-
Short: "Fork a catalog",
13-
Args: cobra.ExactArgs(2),
14-
RunE: func(_ *cobra.Command, args []string) error {
15-
return runFork(args[0], args[1])
16-
},
17-
Hidden: true,
18-
}
19-
return cmd
20-
}
21-
22-
func runFork(src, dst string) error {
7+
func Fork(src, dst string) error {
238
cfg, err := ReadConfig()
249
if err != nil {
2510
return err

cmd/docker-mcp/catalog/import.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,9 @@ import (
99
"os"
1010
"path/filepath"
1111

12-
"github.com/spf13/cobra"
13-
1412
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/tui"
1513
)
1614

17-
func newImportCmd() *cobra.Command {
18-
cmd := &cobra.Command{
19-
Use: "import <alias|url|file>",
20-
Short: "Import a catalog",
21-
Args: cobra.ExactArgs(1),
22-
RunE: func(cmd *cobra.Command, args []string) error {
23-
return runImport(cmd.Context(), args[0])
24-
},
25-
Hidden: true,
26-
}
27-
return cmd
28-
}
29-
3015
func isValidURL(u string) bool {
3116
parsedURL, err := url.ParseRequestURI(u)
3217
if err != nil {
@@ -55,7 +40,7 @@ func DownloadFile(ctx context.Context, url string) ([]byte, error) {
5540
return io.ReadAll(resp.Body)
5641
}
5742

58-
func runImport(ctx context.Context, nameOrURL string) error {
43+
func Import(ctx context.Context, nameOrURL string) error {
5944
// Accept urls or catalog names.
6045
url := nameOrURL
6146
if urlFromAlias, ok := aliasToURL[nameOrURL]; ok {

cmd/docker-mcp/catalog/init.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package catalog
22

33
import (
4-
"github.com/spf13/cobra"
4+
"context"
55
)
66

7-
func newInitCmd() *cobra.Command {
8-
return &cobra.Command{
9-
Use: "init",
10-
Short: "Initialize the catalog",
11-
Args: cobra.NoArgs,
12-
RunE: func(cmd *cobra.Command, _ []string) error {
13-
return runImport(cmd.Context(), DockerCatalogName)
14-
},
15-
}
7+
func Init(ctx context.Context) error {
8+
return Import(ctx, DockerCatalogName)
169
}

cmd/docker-mcp/catalog/ls.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,15 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
8-
"github.com/spf13/cobra"
97
)
108

11-
type lsOpts struct {
12-
JSON bool
13-
}
14-
15-
func newLsCommand() *cobra.Command {
16-
opts := &lsOpts{}
17-
cmd := &cobra.Command{
18-
Use: "ls",
19-
Short: "List configured catalogs",
20-
Args: cobra.NoArgs,
21-
RunE: func(cmd *cobra.Command, _ []string) error {
22-
return runLs(cmd.Context(), *opts)
23-
},
24-
}
25-
flags := cmd.Flags()
26-
flags.BoolVar(&opts.JSON, "json", false, "Print as JSON.")
27-
return cmd
28-
}
29-
30-
func runLs(ctx context.Context, opts lsOpts) error {
9+
func Ls(ctx context.Context, outputJSON bool) error {
3110
cfg, err := ReadConfigWithDefaultCatalog(ctx)
3211
if err != nil {
3312
return err
3413
}
3514

36-
if opts.JSON {
15+
if outputJSON {
3716
data, err := json.Marshal(cfg)
3817
if err != nil {
3918
return err
@@ -42,6 +21,7 @@ func runLs(ctx context.Context, opts lsOpts) error {
4221
} else {
4322
humanPrintCatalog(*cfg)
4423
}
24+
4525
return nil
4626
}
4727

@@ -50,6 +30,7 @@ func humanPrintCatalog(cfg Config) {
5030
fmt.Println("No catalogs configured.")
5131
return
5232
}
33+
5334
for name, catalog := range cfg.Catalogs {
5435
fmt.Printf("%s: %s\n", name, catalog.DisplayName)
5536
}

cmd/docker-mcp/catalog/reset.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
package catalog
22

33
import (
4+
"context"
45
"os"
56

6-
"github.com/spf13/cobra"
7-
87
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/config"
98
)
109

11-
func newResetCommand() *cobra.Command {
12-
return &cobra.Command{
13-
Use: "reset",
14-
Aliases: []string{"empty"},
15-
Short: "Empty the catalog",
16-
Args: cobra.NoArgs,
17-
RunE: func(*cobra.Command, []string) error {
18-
catalogsDir, err := config.FilePath("catalogs")
19-
if err != nil {
20-
return err
21-
}
22-
if err := os.RemoveAll(catalogsDir); err != nil {
23-
return err
24-
}
25-
26-
return WriteConfig(&Config{})
27-
},
10+
func Reset(context.Context) error {
11+
catalogsDir, err := config.FilePath("catalogs")
12+
if err != nil {
13+
return err
2814
}
15+
if err := os.RemoveAll(catalogsDir); err != nil {
16+
return err
17+
}
18+
19+
return WriteConfig(&Config{})
2920
}

cmd/docker-mcp/catalog/rm.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,10 @@ package catalog
33
import (
44
"fmt"
55

6-
"github.com/spf13/cobra"
7-
86
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/config"
97
)
108

11-
func newRmCommand() *cobra.Command {
12-
cmd := &cobra.Command{
13-
Use: "rm <name>",
14-
Short: "Remove a catalog",
15-
Args: cobra.ExactArgs(1),
16-
RunE: func(_ *cobra.Command, args []string) error {
17-
return runRm(args[0])
18-
},
19-
Hidden: true,
20-
}
21-
return cmd
22-
}
23-
24-
func runRm(name string) error {
9+
func Rm(name string) error {
2510
cfg, err := ReadConfig()
2611
if err != nil {
2712
return err

0 commit comments

Comments
 (0)