♻️ Quality: dockerinit and docsearch without smells (3 → 0)
- dockerinit: the flags as dockerArgs and parseFlags - docsearch.collect with a walker (visit is WalkDir's step) Checked old vs new binary: 7 mm dockerinit runs (-h, an argument, an unknown flag, --yes twice, answers, a missing agent file) and /docs over a folder with hidden, node_modules, binary and symlinked files and a missing folder: identical output and files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f062af9 parent: 3d09337 modified
internal/dockerinit/main.go +28 -14 | @@ -49,42 +49,56 @@ func (p place) resolve(path string) string { | ||
| 49 | 49 | return filepath.Join(p.dir, path) |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | -func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | |
| 52 | +// dockerArgs are mm dockerinit's flags; explicit says -agent was given. | |
| 53 | +type dockerArgs struct { | |
| 54 | + name, agentFile string | |
| 55 | + yes, explicit bool | |
| 56 | +} | |
| 57 | + | |
| 58 | +// parseFlags reads the flags. ok is false when run must stop with code: 0 | |
| 59 | +// for -h, 2 for an unknown flag or an argument. | |
| 60 | +func parseFlags(args []string, stdout io.Writer) (a dockerArgs, code int, ok bool) { | |
| 53 | 61 | fs := flag.NewFlagSet("mm dockerinit", flag.ContinueOnError) |
| 54 | 62 | fs.SetOutput(stdout) |
| 55 | - name := fs.String("name", "", "the service's name (default: acp.name in the agent file, else mm)") | |
| 56 | - agentFile := fs.String("agent", "agent.yaml", "the agent file to name the service after and to start (default: agent.yaml, then .mm/agent.yaml)") | |
| 57 | - yes := fs.Bool("yes", false, "ask nothing: an existing file stops everything, nothing is overwritten") | |
| 63 | + fs.StringVar(&a.name, "name", "", "the service's name (default: acp.name in the agent file, else mm)") | |
| 64 | + fs.StringVar(&a.agentFile, "agent", "agent.yaml", "the agent file to name the service after and to start (default: agent.yaml, then .mm/agent.yaml)") | |
| 65 | + fs.BoolVar(&a.yes, "yes", false, "ask nothing: an existing file stops everything, nothing is overwritten") | |
| 58 | 66 | if err := fs.Parse(args); err != nil { |
| 59 | 67 | if errors.Is(err, flag.ErrHelp) { |
| 60 | - return 0 | |
| 68 | + return a, 0, false | |
| 61 | 69 | } |
| 62 | - return 2 | |
| 70 | + return a, 2, false | |
| 63 | 71 | } |
| 64 | 72 | if fs.NArg() > 0 { |
| 65 | 73 | fmt.Fprintf(stdout, "⚠️ mm dockerinit takes no argument, got %q\n", fs.Arg(0)) |
| 66 | - return 2 | |
| 74 | + return a, 2, false | |
| 67 | 75 | } |
| 76 | + fs.Visit(func(f *flag.Flag) { a.explicit = a.explicit || f.Name == "agent" }) | |
| 77 | + return a, 0, true | |
| 78 | +} | |
| 68 | 79 | |
| 80 | +func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | |
| 81 | + a, code, ok := parseFlags(args, stdout) | |
| 82 | + if !ok { | |
| 83 | + return code | |
| 84 | + } | |
| 69 | 85 | // Absolute: the .mm folder is recognised by comparing absolute paths. |
| 70 | 86 | project, err := filepath.Abs(at.resolve(".")) |
| 71 | 87 | if err != nil { |
| 72 | 88 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) |
| 73 | 89 | return 1 |
| 74 | 90 | } |
| 75 | - explicit := false | |
| 76 | - fs.Visit(func(f *flag.Flag) { explicit = explicit || f.Name == "agent" }) | |
| 77 | - agentPath := scaffold.FindAgentFile(*agentFile, project, explicit, at.resolve) | |
| 91 | + agentPath := scaffold.FindAgentFile(a.agentFile, project, a.explicit, at.resolve) | |
| 78 | 92 | // A .mm project (ticket 212): its paths are written relative to the |
| 79 | 93 | // folder of compose.yaml, the project, as compose reads them. |
| 80 | 94 | mm := scaffold.MMFolderOf(agentPath, project) |
| 81 | - start := *agentFile | |
| 95 | + start := a.agentFile | |
| 82 | 96 | if mm != nil { |
| 83 | 97 | mm.Base = project |
| 84 | 98 | start = startArg(mm) |
| 85 | 99 | } |
| 86 | 100 | |
| 87 | - chosen := *name | |
| 101 | + chosen := a.name | |
| 88 | 102 | if chosen == "" { |
| 89 | 103 | chosen = scaffold.AgentName(agentPath, stdout) |
| 90 | 104 | } |
| @@ -93,7 +107,7 @@ func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | ||
| 93 | 107 | {Path: at.resolve("Dockerfile"), Text: Dockerfile(), Replace: at.overwrite}, |
| 94 | 108 | {Path: at.resolve("compose.yaml"), Text: compose(service, os.Getuid(), os.Getgid(), mm), Replace: at.overwrite}, |
| 95 | 109 | } |
| 96 | - wrote, err := scaffold.Write(files, stdin, stdout, *yes) | |
| 110 | + wrote, err := scaffold.Write(files, stdin, stdout, a.yes) | |
| 97 | 111 | if err != nil { |
| 98 | 112 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) |
| 99 | 113 | return 1 |
| @@ -102,7 +116,7 @@ func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | ||
| 102 | 116 | if mm != nil { |
| 103 | 117 | reportMMFolder(stdout, mm) |
| 104 | 118 | } |
| 105 | - printNext(stdout, service, start, missing(agentPath, *agentFile)) | |
| 119 | + printNext(stdout, service, start, missing(agentPath, a.agentFile)) | |
| 106 | 120 | } |
| 107 | 121 | return 0 |
| 108 | 122 | } |
| @@ -49,42 +49,56 @@ func (p place) resolve(path string) string { | |||
| 49 | return filepath.Join(p.dir, path) | 49 | return filepath.Join(p.dir, path) |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | -func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | 52 | +// dockerArgs are mm dockerinit's flags; explicit says -agent was given. |
| 53 | +type dockerArgs struct { | ||
| 54 | + name, agentFile string | ||
| 55 | + yes, explicit bool | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +// parseFlags reads the flags. ok is false when run must stop with code: 0 | ||
| 59 | +// for -h, 2 for an unknown flag or an argument. | ||
| 60 | +func parseFlags(args []string, stdout io.Writer) (a dockerArgs, code int, ok bool) { | ||
| 53 | fs := flag.NewFlagSet("mm dockerinit", flag.ContinueOnError) | 61 | fs := flag.NewFlagSet("mm dockerinit", flag.ContinueOnError) |
| 54 | fs.SetOutput(stdout) | 62 | fs.SetOutput(stdout) |
| 55 | - name := fs.String("name", "", "the service's name (default: acp.name in the agent file, else mm)") | 63 | + fs.StringVar(&a.name, "name", "", "the service's name (default: acp.name in the agent file, else mm)") |
| 56 | - agentFile := fs.String("agent", "agent.yaml", "the agent file to name the service after and to start (default: agent.yaml, then .mm/agent.yaml)") | 64 | + fs.StringVar(&a.agentFile, "agent", "agent.yaml", "the agent file to name the service after and to start (default: agent.yaml, then .mm/agent.yaml)") |
| 57 | - yes := fs.Bool("yes", false, "ask nothing: an existing file stops everything, nothing is overwritten") | 65 | + fs.BoolVar(&a.yes, "yes", false, "ask nothing: an existing file stops everything, nothing is overwritten") |
| 58 | if err := fs.Parse(args); err != nil { | 66 | if err := fs.Parse(args); err != nil { |
| 59 | if errors.Is(err, flag.ErrHelp) { | 67 | if errors.Is(err, flag.ErrHelp) { |
| 60 | - return 0 | 68 | + return a, 0, false |
| 61 | } | 69 | } |
| 62 | - return 2 | 70 | + return a, 2, false |
| 63 | } | 71 | } |
| 64 | if fs.NArg() > 0 { | 72 | if fs.NArg() > 0 { |
| 65 | fmt.Fprintf(stdout, "⚠️ mm dockerinit takes no argument, got %q\n", fs.Arg(0)) | 73 | fmt.Fprintf(stdout, "⚠️ mm dockerinit takes no argument, got %q\n", fs.Arg(0)) |
| 66 | - return 2 | 74 | + return a, 2, false |
| 67 | } | 75 | } |
| 76 | + fs.Visit(func(f *flag.Flag) { a.explicit = a.explicit || f.Name == "agent" }) | ||
| 77 | + return a, 0, true | ||
| 78 | +} | ||
| 68 | 79 | ||
| 80 | +func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | ||
| 81 | + a, code, ok := parseFlags(args, stdout) | ||
| 82 | + if !ok { | ||
| 83 | + return code | ||
| 84 | + } | ||
| 69 | // Absolute: the .mm folder is recognised by comparing absolute paths. | 85 | // Absolute: the .mm folder is recognised by comparing absolute paths. |
| 70 | project, err := filepath.Abs(at.resolve(".")) | 86 | project, err := filepath.Abs(at.resolve(".")) |
| 71 | if err != nil { | 87 | if err != nil { |
| 72 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) | 88 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) |
| 73 | return 1 | 89 | return 1 |
| 74 | } | 90 | } |
| 75 | - explicit := false | 91 | + agentPath := scaffold.FindAgentFile(a.agentFile, project, a.explicit, at.resolve) |
| 76 | - fs.Visit(func(f *flag.Flag) { explicit = explicit || f.Name == "agent" }) | ||
| 77 | - agentPath := scaffold.FindAgentFile(*agentFile, project, explicit, at.resolve) | ||
| 78 | // A .mm project (ticket 212): its paths are written relative to the | 92 | // A .mm project (ticket 212): its paths are written relative to the |
| 79 | // folder of compose.yaml, the project, as compose reads them. | 93 | // folder of compose.yaml, the project, as compose reads them. |
| 80 | mm := scaffold.MMFolderOf(agentPath, project) | 94 | mm := scaffold.MMFolderOf(agentPath, project) |
| 81 | - start := *agentFile | 95 | + start := a.agentFile |
| 82 | if mm != nil { | 96 | if mm != nil { |
| 83 | mm.Base = project | 97 | mm.Base = project |
| 84 | start = startArg(mm) | 98 | start = startArg(mm) |
| 85 | } | 99 | } |
| 86 | 100 | ||
| 87 | - chosen := *name | 101 | + chosen := a.name |
| 88 | if chosen == "" { | 102 | if chosen == "" { |
| 89 | chosen = scaffold.AgentName(agentPath, stdout) | 103 | chosen = scaffold.AgentName(agentPath, stdout) |
| 90 | } | 104 | } |
| @@ -93,7 +107,7 @@ func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | |||
| 93 | {Path: at.resolve("Dockerfile"), Text: Dockerfile(), Replace: at.overwrite}, | 107 | {Path: at.resolve("Dockerfile"), Text: Dockerfile(), Replace: at.overwrite}, |
| 94 | {Path: at.resolve("compose.yaml"), Text: compose(service, os.Getuid(), os.Getgid(), mm), Replace: at.overwrite}, | 108 | {Path: at.resolve("compose.yaml"), Text: compose(service, os.Getuid(), os.Getgid(), mm), Replace: at.overwrite}, |
| 95 | } | 109 | } |
| 96 | - wrote, err := scaffold.Write(files, stdin, stdout, *yes) | 110 | + wrote, err := scaffold.Write(files, stdin, stdout, a.yes) |
| 97 | if err != nil { | 111 | if err != nil { |
| 98 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) | 112 | fmt.Fprintln(stdout, "⚠️ "+err.Error()) |
| 99 | return 1 | 113 | return 1 |
| @@ -102,7 +116,7 @@ func run(args []string, stdin io.Reader, stdout io.Writer, at place) int { | |||
| 102 | if mm != nil { | 116 | if mm != nil { |
| 103 | reportMMFolder(stdout, mm) | 117 | reportMMFolder(stdout, mm) |
| 104 | } | 118 | } |
| 105 | - printNext(stdout, service, start, missing(agentPath, *agentFile)) | 119 | + printNext(stdout, service, start, missing(agentPath, a.agentFile)) |
| 106 | } | 120 | } |
| 107 | return 0 | 121 | return 0 |
| 108 | } | 122 | } |
modified
internal/docsearch/walk.go +32 -18 | @@ -33,28 +33,42 @@ func collect(opts Options) (files []file, missing []string) { | ||
| 33 | 33 | missing = append(missing, dir) |
| 34 | 34 | continue |
| 35 | 35 | } |
| 36 | - _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { | |
| 37 | - if err != nil { | |
| 38 | - return nil // an unreadable folder or file: skip it, keep walking | |
| 39 | - } | |
| 40 | - if d.IsDir() { | |
| 41 | - if path != root && skipFolder(d.Name()) { | |
| 42 | - return filepath.SkipDir | |
| 43 | - } | |
| 44 | - return nil | |
| 45 | - } | |
| 46 | - if d.Type()&fs.ModeSymlink != 0 || !exts[strings.ToLower(filepath.Ext(path))] { | |
| 47 | - return nil | |
| 48 | - } | |
| 49 | - if f, ok := readFile(path, opts); ok { | |
| 50 | - files = append(files, f) | |
| 51 | - } | |
| 52 | - return nil | |
| 53 | - }) | |
| 36 | + w := &walker{root: root, exts: exts, opts: opts} | |
| 37 | + _ = filepath.WalkDir(root, w.visit) | |
| 38 | + files = append(files, w.files...) | |
| 54 | 39 | } |
| 55 | 40 | return files, missing |
| 56 | 41 | } |
| 57 | 42 | |
| 43 | +// walker collects the files of one folder. | |
| 44 | +type walker struct { | |
| 45 | + root string | |
| 46 | + exts map[string]bool | |
| 47 | + opts Options | |
| 48 | + files []file | |
| 49 | +} | |
| 50 | + | |
| 51 | +// visit is WalkDir's step: hidden folders and node_modules skipped, symbolic | |
| 52 | +// links and other extensions left out, each candidate read. | |
| 53 | +func (w *walker) visit(path string, d fs.DirEntry, err error) error { | |
| 54 | + if err != nil { | |
| 55 | + return nil // an unreadable folder or file: skip it, keep walking | |
| 56 | + } | |
| 57 | + if d.IsDir() { | |
| 58 | + if path != w.root && skipFolder(d.Name()) { | |
| 59 | + return filepath.SkipDir | |
| 60 | + } | |
| 61 | + return nil | |
| 62 | + } | |
| 63 | + if d.Type()&fs.ModeSymlink != 0 || !w.exts[strings.ToLower(filepath.Ext(path))] { | |
| 64 | + return nil | |
| 65 | + } | |
| 66 | + if f, ok := readFile(path, w.opts); ok { | |
| 67 | + w.files = append(w.files, f) | |
| 68 | + } | |
| 69 | + return nil | |
| 70 | +} | |
| 71 | + | |
| 58 | 72 | func skipFolder(name string) bool { |
| 59 | 73 | return strings.HasPrefix(name, ".") || name == "node_modules" |
| 60 | 74 | } |
| @@ -33,28 +33,42 @@ func collect(opts Options) (files []file, missing []string) { | |||
| 33 | missing = append(missing, dir) | 33 | missing = append(missing, dir) |
| 34 | continue | 34 | continue |
| 35 | } | 35 | } |
| 36 | - _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { | 36 | + w := &walker{root: root, exts: exts, opts: opts} |
| 37 | - if err != nil { | 37 | + _ = filepath.WalkDir(root, w.visit) |
| 38 | - return nil // an unreadable folder or file: skip it, keep walking | 38 | + files = append(files, w.files...) |
| 39 | - } | ||
| 40 | - if d.IsDir() { | ||
| 41 | - if path != root && skipFolder(d.Name()) { | ||
| 42 | - return filepath.SkipDir | ||
| 43 | - } | ||
| 44 | - return nil | ||
| 45 | - } | ||
| 46 | - if d.Type()&fs.ModeSymlink != 0 || !exts[strings.ToLower(filepath.Ext(path))] { | ||
| 47 | - return nil | ||
| 48 | - } | ||
| 49 | - if f, ok := readFile(path, opts); ok { | ||
| 50 | - files = append(files, f) | ||
| 51 | - } | ||
| 52 | - return nil | ||
| 53 | - }) | ||
| 54 | } | 39 | } |
| 55 | return files, missing | 40 | return files, missing |
| 56 | } | 41 | } |
| 57 | 42 | ||
| 43 | +// walker collects the files of one folder. | ||
| 44 | +type walker struct { | ||
| 45 | + root string | ||
| 46 | + exts map[string]bool | ||
| 47 | + opts Options | ||
| 48 | + files []file | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +// visit is WalkDir's step: hidden folders and node_modules skipped, symbolic | ||
| 52 | +// links and other extensions left out, each candidate read. | ||
| 53 | +func (w *walker) visit(path string, d fs.DirEntry, err error) error { | ||
| 54 | + if err != nil { | ||
| 55 | + return nil // an unreadable folder or file: skip it, keep walking | ||
| 56 | + } | ||
| 57 | + if d.IsDir() { | ||
| 58 | + if path != w.root && skipFolder(d.Name()) { | ||
| 59 | + return filepath.SkipDir | ||
| 60 | + } | ||
| 61 | + return nil | ||
| 62 | + } | ||
| 63 | + if d.Type()&fs.ModeSymlink != 0 || !w.exts[strings.ToLower(filepath.Ext(path))] { | ||
| 64 | + return nil | ||
| 65 | + } | ||
| 66 | + if f, ok := readFile(path, w.opts); ok { | ||
| 67 | + w.files = append(w.files, f) | ||
| 68 | + } | ||
| 69 | + return nil | ||
| 70 | +} | ||
| 71 | + | ||
| 58 | func skipFolder(name string) bool { | 72 | func skipFolder(name string) bool { |
| 59 | return strings.HasPrefix(name, ".") || name == "node_modules" | 73 | return strings.HasPrefix(name, ".") || name == "node_modules" |
| 60 | } | 74 | } |