Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devx): Developer.md discovery system #81

Draft
wants to merge 8 commits into
base: feat/devx
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cli/cmd/cmds/devx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ package cmds

import (
"fmt"
"log/slog"
"os"

"github.com/input-output-hk/catalyst-forge/cli/pkg/command"
"github.com/input-output-hk/catalyst-forge/cli/pkg/run"
)

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 \"<markdown-path> <command-name>\"")
}
if c.MarkdownPath != "" && c.CommandName == "" {
return fmt.Errorf("expected \"<command-name>\"")
}

// 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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions cli/pkg/command/discovery.go
Original file line number Diff line number Diff line change
@@ -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
}