diff --git a/cli/cmd/cmds/devx.go b/cli/cmd/cmds/devx.go index 0b4955b..b93b5d2 100644 --- a/cli/cmd/cmds/devx.go +++ b/cli/cmd/cmds/devx.go @@ -2,7 +2,6 @@ package cmds import ( "fmt" - "log/slog" "os" "github.com/input-output-hk/catalyst-forge/cli/pkg/command" @@ -10,11 +9,26 @@ import ( ) type DevX struct { - MarkdownPath string `arg:"" help:"Path to the markdown file."` - CommandName string `arg:"" help:"Command to be executed."` + Discover bool `kong:"short=d" help:"List all markdown files available in the project or all commands in the markdown file if file is specified."` + MarkdownPath string `kong:"arg,predictor=path,optional" help:"Path to the markdown file."` + CommandName string `kong:"arg,optional" help:"Command to be executed."` } -func (c *DevX) Run(ctx run.RunContext, logger *slog.Logger) error { +func (c *DevX) Run(ctx run.RunContext) error { + // for the "-d" flag + if c.Discover { + return nil + } + + // validate args if without "-d" flag + if c.MarkdownPath == "" { + return fmt.Errorf("expected \" \"") + } + if c.MarkdownPath != "" && c.CommandName == "" { + return fmt.Errorf("expected \"\"") + } + + // read the markdown and execute the command raw, err := os.ReadFile(c.MarkdownPath) if err != nil { return fmt.Errorf("could not read file at %s: %v", c.MarkdownPath, err) @@ -25,5 +39,5 @@ func (c *DevX) Run(ctx run.RunContext, logger *slog.Logger) error { return err } - return prog.ProcessCmd(c.CommandName, logger) + return prog.ProcessCmd(c.CommandName, ctx.Logger) } diff --git a/cli/go.mod b/cli/go.mod index a721629..5176647 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -20,6 +20,7 @@ require ( github.com/spf13/afero v1.11.0 github.com/stretchr/testify v1.9.0 github.com/willabides/kongplete v0.4.0 + github.com/yuin/goldmark v1.7.4 golang.org/x/exp v0.0.0-20231006140011-7918f672742d ) @@ -80,7 +81,6 @@ require ( github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect - github.com/yuin/goldmark v1.7.4 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/mod v0.20.0 // indirect golang.org/x/net v0.28.0 // indirect diff --git a/cli/pkg/command/discovery.go b/cli/pkg/command/discovery.go new file mode 100644 index 0000000..0014240 --- /dev/null +++ b/cli/pkg/command/discovery.go @@ -0,0 +1,43 @@ +package command + +import ( + "fmt" + "os" + "path/filepath" +) + +// Finds all `Developer.md` files within the specified `rootPath`. +// If `rootPath` is nil, it defaults to the current working directory. +func DiscoverMarkdownFiles(rootPath *string) ([]string, error) { + // Default to the current directory if rootPath is not provided + var searchPath string + if rootPath != nil && *rootPath != "" { + searchPath = *rootPath + } else { + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + searchPath = cwd + } + + var result []string + + err := filepath.Walk(searchPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return fmt.Errorf("error accessing path %q: %v", path, err) + } + + if !info.IsDir() && info.Name() == "Developer.md" { + result = append(result, path) + } + + return nil + }) + + if err != nil { + return nil, fmt.Errorf("error walking through the directory: %v", err) + } + + return result, nil +}