Skip to content

Commit

Permalink
fix: execution of html pages
Browse files Browse the repository at this point in the history
  • Loading branch information
daulet committed Sep 8, 2024
1 parent 5b2aa79 commit 4101477
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 23 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ func generate(
return buf.String(), nil
}

// TODO return correct exit code when -run or -exec fails
func runBlock(block *parser.CodeBlock) error {
// TODO return correct exit code when -run or -exec fails - is this fixed?
func runBlock(block *parser.CodeBlock, tempDir string) error {
switch block.Lang {
case parser.Go:
code := block.Code
// TODO remove when prompt engineering is there to add "make it runnable"
if !strings.HasPrefix(block.Code, "package") {
code = fmt.Sprintf("package main\n\n%s", block.Code)
}
path := fmt.Sprintf("%smain.go", os.TempDir())
path := fmt.Sprintf("%smain.go", tempDir)
if err := os.WriteFile(path, []byte(code), 0644); err != nil {
return err
}
Expand All @@ -136,15 +136,27 @@ func runBlock(block *parser.CodeBlock) error {
return err
}
case parser.HTML:
path := fmt.Sprintf("%sindex.html", os.TempDir())
path := fmt.Sprintf("%sindex.html", tempDir)
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
return err
}
if err := runCmd("open", fmt.Sprintf("file://%s", path)); err != nil {
return err
}
case parser.JavaScript:
path := fmt.Sprintf("%sscript.js", tempDir) // TODO can this be parsed from model output? sometimes changes
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
return err
}
// assumed to be part of html, so not executed separately
case parser.CSS:
path := fmt.Sprintf("%sstyle.css", tempDir) // TODO can this be parsed from model output? sometimes changes
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
return err
}
// assumed to be part of html, so not executed separately
case parser.Python:
path := fmt.Sprintf("%smain.py", os.TempDir())
path := fmt.Sprintf("%smain.py", tempDir)
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
return err
}
Expand Down Expand Up @@ -280,8 +292,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
codeW, blockCh := parser.NewCode()
go func() {
defer close(done)
tempDir := os.TempDir()
for block := range blockCh {
_ = runBlock(block)
_ = runBlock(block, tempDir)
}
}()
// no output to the user, we just execute the code
Expand Down Expand Up @@ -310,8 +323,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
out.Close()
}
<-done
tempDir := os.TempDir()
for _, block := range blocks {
if err := runBlock(block); err != nil {
if err := runBlock(block, tempDir); err != nil {
return "", err
}
}
Expand All @@ -338,13 +352,14 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
return fmt.Errorf("failed to open /dev/tty: %w", err)
}
}
if usrMsg == "" && pipeContent == "" {
if usrMsg == "" && pipeContent == "" && !flagVals.Interactive {
return fmt.Errorf("what's your command?")
}
if pipeContent != "" {
usrMsg = fmt.Sprintf(CONTEXT_TEMPLATE, pipeContent, usrMsg)
}

// TODO require -f flag for file input, use it for image too
if _, err := os.Stat(usrMsg); err == nil {
f, err := os.Open(usrMsg)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions parser/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
Go
Bash
HTML
JavaScript
CSS
Python
)

Expand All @@ -25,6 +27,10 @@ func language(s string) Language {
return Bash
case "html":
return HTML
case "javascript", "js":
return JavaScript
case "css":
return CSS
case "python", "python3":
return Python
default:
Expand Down

0 comments on commit 4101477

Please sign in to comment.