-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from input-output-hk/feat/initial-devx
feat: add initial DevX
- Loading branch information
Showing
10 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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."` | ||
} | ||
|
||
func (c *DevX) Run(ctx run.RunContext, logger *slog.Logger) error { | ||
raw, err := os.ReadFile(c.MarkdownPath) | ||
if err != nil { | ||
return fmt.Errorf("could not read file at %s: %v", c.MarkdownPath, err) | ||
} | ||
|
||
prog, err := command.ExtractDevXMarkdown(raw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return prog.ProcessCmd(c.CommandName, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
exec git init . | ||
|
||
exec forge devx ./Developer.md run-some-command | ||
cmp stdout expect.txt | ||
|
||
-- Developer.md -- | ||
# My cool dev docs | ||
|
||
### Run Some Command !! | ||
|
||
``` sh | ||
echo "should run this command (1)" | ||
``` | ||
|
||
``` sh | ||
echo "should not run this command (1)" | ||
``` | ||
-- expect.txt -- | ||
should run this command (1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# My cool dev docs | ||
|
||
### Run Some Command !! | ||
|
||
``` sh | ||
echo "should run this command (1)" | ||
``` | ||
|
||
``` sh | ||
echo "should not run this command (1)" | ||
``` | ||
|
||
### Extra $Cool$ Command | ||
|
||
``` sh | ||
echo "should run this command (2)" | ||
echo "should run another command (2)" | ||
``` | ||
|
||
``` sh | ||
echo "should not run this command (2)" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package command | ||
|
||
type LanguageExecutor struct { | ||
executor map[string]Executor | ||
} | ||
|
||
func NewDefaultLanguageExecutor() LanguageExecutor { | ||
return LanguageExecutor{ | ||
executor: map[string]Executor{ | ||
"sh": ShellLanguageExecutor{}, | ||
}, | ||
} | ||
} | ||
|
||
type Executor interface { | ||
GetExecutorCommand() string | ||
GetExecutorArgs(content string) []string | ||
} | ||
|
||
// shell | ||
type ShellLanguageExecutor struct{} | ||
|
||
func (e ShellLanguageExecutor) GetExecutorCommand() string { | ||
return "sh" | ||
} | ||
|
||
func (e ShellLanguageExecutor) GetExecutorArgs(content string) []string { | ||
return formatArgs([]string{"-c", "$"}, content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/input-output-hk/catalyst-forge/cli/pkg/executor" | ||
) | ||
|
||
type Program struct { | ||
name string | ||
groups []CommandGroup | ||
} | ||
|
||
type CommandGroup struct { | ||
name string | ||
commands []Command | ||
} | ||
|
||
type Command struct { | ||
content string | ||
lang *string | ||
platform *string | ||
} | ||
|
||
func (prog *Program) ProcessCmd(cmd string, logger *slog.Logger) error { | ||
var foundCmd *Command | ||
for _, v := range prog.groups { | ||
if v.GetId() == cmd { | ||
// TODO: should get the fisrt (most specified) command corresponding to the current host platform | ||
foundCmd = &v.commands[0] | ||
} | ||
} | ||
|
||
if foundCmd == nil { | ||
return fmt.Errorf("command '%s' not found in markdown", cmd) | ||
} | ||
|
||
return foundCmd.exec(logger) | ||
} | ||
|
||
func (cmd *Command) exec(logger *slog.Logger) error { | ||
if cmd.lang == nil { | ||
return fmt.Errorf("command block without specified language") | ||
} | ||
|
||
lang, ok := NewDefaultLanguageExecutor().executor[*cmd.lang] | ||
if !ok { | ||
return fmt.Errorf("only commands running with `sh` can be executed") | ||
} | ||
if _, err := exec.LookPath(lang.GetExecutorCommand()); err != nil { | ||
return fmt.Errorf("command '%s' is unavailable", lang.GetExecutorCommand()) | ||
} | ||
|
||
localExec := executor.NewLocalExecutor( | ||
logger, | ||
executor.WithRedirect(), | ||
) | ||
_, err := localExec.Execute(lang.GetExecutorCommand(), lang.GetExecutorArgs(cmd.content)) | ||
|
||
return err | ||
} | ||
|
||
func (cg *CommandGroup) GetId() string { | ||
var result []rune | ||
|
||
for _, char := range cg.name { | ||
if unicode.IsLetter(char) || unicode.IsDigit(char) { | ||
result = append(result, unicode.ToLower(char)) | ||
} else if unicode.IsSpace(char) { | ||
result = append(result, '-') | ||
} | ||
} | ||
|
||
joined := string(result) | ||
|
||
re := regexp.MustCompile(`-+`) | ||
joined = re.ReplaceAllString(joined, "-") | ||
|
||
return strings.Trim(joined, "-") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/text" | ||
) | ||
|
||
func ExtractDevXMarkdown(data []byte) (*Program, error) { | ||
md := goldmark.New() | ||
reader := text.NewReader(data) | ||
doc := md.Parser().Parse(reader) | ||
|
||
// store the command groups and commands | ||
groups := []CommandGroup{} | ||
var progName *string | ||
var currentPlatform *string | ||
|
||
// walk through the ast nodes | ||
ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
// look up for headers | ||
if heading, ok := n.(*ast.Heading); ok && entering { | ||
if heading.Level == 1 { | ||
title := string(heading.Text(data)) | ||
|
||
progName = &title | ||
} | ||
|
||
if heading.Level == 3 { | ||
currentPlatform = nil | ||
commandName := string(heading.Text(data)) | ||
|
||
groups = append(groups, CommandGroup{ | ||
name: commandName, | ||
commands: []Command{}, | ||
}) | ||
} | ||
|
||
/* if heading.Level == 4 && len(groups) > 0 { | ||
platform := string(heading.Text(data)) | ||
currentPlatform = &platform | ||
} */ | ||
} | ||
|
||
// look up for code blocks | ||
if block, ok := n.(*ast.FencedCodeBlock); ok && entering && len(groups) > 0 { | ||
i := len(groups) - 1 | ||
lang := string(block.Language(data)) | ||
|
||
var buf bytes.Buffer | ||
for i := 0; i < block.Lines().Len(); i++ { | ||
line := block.Lines().At(i) | ||
buf.Write(line.Value(data)) | ||
} | ||
|
||
groups[i].commands = append(groups[i].commands, Command{ | ||
content: buf.String(), | ||
lang: &lang, | ||
platform: currentPlatform, | ||
}) | ||
} | ||
|
||
return ast.WalkContinue, nil | ||
}) | ||
|
||
if len(groups) == 0 { | ||
return nil, errors.New("no command groups found in the markdown") | ||
} | ||
if progName == nil { | ||
return nil, errors.New("no title found in the markdown") | ||
} | ||
|
||
prog := Program{ | ||
name: *progName, | ||
groups: groups, | ||
} | ||
|
||
return &prog, nil | ||
} | ||
|
||
func formatArgs(base []string, replacement string) []string { | ||
replaced := make([]string, len(base)) | ||
|
||
for i, str := range base { | ||
replaced[i] = strings.ReplaceAll(str, "$", replacement) | ||
} | ||
|
||
return replaced | ||
} |