From 7c118abd5e1a3e034c6ee7b0496a41b0bd42609f Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 16 Oct 2024 17:22:03 +0700 Subject: [PATCH 1/5] fix: sync devx ctx to cli --- cli/cmd/cmds/devx.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/cmd/cmds/devx.go b/cli/cmd/cmds/devx.go index 0b4955b3..96404f43 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" @@ -14,7 +13,7 @@ type DevX struct { CommandName string `arg:"" help:"Command to be executed."` } -func (c *DevX) Run(ctx run.RunContext, logger *slog.Logger) error { +func (c *DevX) Run(ctx run.RunContext) error { raw, err := os.ReadFile(c.MarkdownPath) if err != nil { return fmt.Errorf("could not read file at %s: %v", c.MarkdownPath, err) @@ -25,5 +24,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) } From ec27e4f1f051440dbdfe47737efc97d446ef0cee Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 21 Oct 2024 19:39:06 +0700 Subject: [PATCH 2/5] fix: forge version --- cli/go.mod | 2 +- cli/pkg/command/program.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/go.mod b/cli/go.mod index e878241d..7140d0a7 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -18,6 +18,7 @@ require ( github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21 github.com/spf13/afero v1.11.0 github.com/stretchr/testify v1.9.0 + github.com/yuin/goldmark v1.7.4 golang.org/x/exp v0.0.0-20231006140011-7918f672742d ) @@ -75,7 +76,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/program.go b/cli/pkg/command/program.go index e226fd6d..871da2ae 100644 --- a/cli/pkg/command/program.go +++ b/cli/pkg/command/program.go @@ -60,7 +60,7 @@ func (cmd *Command) exec(logger *slog.Logger) error { logger, executor.WithRedirect(), ) - _, err := localExec.Execute(lang.GetExecutorCommand(), lang.GetExecutorArgs(cmd.content)) + _, err := localExec.Execute(lang.GetExecutorCommand(), lang.GetExecutorArgs(cmd.content)...) return err } From 6b2c9333591c2e8739133fab81db72c874abf15a Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 21 Oct 2024 19:55:41 +0700 Subject: [PATCH 3/5] feat: discovery function --- cli/pkg/command/discovery.go | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cli/pkg/command/discovery.go diff --git a/cli/pkg/command/discovery.go b/cli/pkg/command/discovery.go new file mode 100644 index 00000000..0014240a --- /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 +} From b96d6dfc3b25aaff2e6bf39118eb05d326efdeee Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 22 Oct 2024 13:41:36 +0700 Subject: [PATCH 4/5] chore: pkg --- cli/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/go.mod b/cli/go.mod index f7b04efb..51766477 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -19,8 +19,8 @@ require ( github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21 github.com/spf13/afero v1.11.0 github.com/stretchr/testify v1.9.0 - github.com/yuin/goldmark v1.7.4 github.com/willabides/kongplete v0.4.0 + github.com/yuin/goldmark v1.7.4 golang.org/x/exp v0.0.0-20231006140011-7918f672742d ) From e25168da3465d863f366a76bf5f41e38321ed060 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 22 Oct 2024 14:35:05 +0700 Subject: [PATCH 5/5] feat: discovery flag --- cli/cmd/cmds/devx.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cli/cmd/cmds/devx.go b/cli/cmd/cmds/devx.go index 96404f43..b93b5d21 100644 --- a/cli/cmd/cmds/devx.go +++ b/cli/cmd/cmds/devx.go @@ -9,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) 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)