Skip to content
Open
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
6 changes: 5 additions & 1 deletion cmd/pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/AlecAivazis/survey/v2"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"

"github.com/j178/leetgo/editor"
Expand Down Expand Up @@ -105,7 +106,6 @@ leetgo pick two-sum`,
RunE: func(cmd *cobra.Command, args []string) error {
c := leetcode.NewClient(leetcode.ReadCredentials())
var q *leetcode.QuestionData

if len(args) > 0 {
qid := args[0]
qs, err := leetcode.ParseQID(qid, c)
Expand Down Expand Up @@ -136,6 +136,10 @@ leetgo pick two-sum`,
if err != nil {
return err
}

if result.PostPickError != "" {
log.Error("error", "post_pick_action", result.PostPickError)
}
if !skipEditor {
err = editor.Open(result)
return err
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type BaseLangConfig struct {
SeparateDescriptionFile bool `yaml:"separate_description_file,omitempty" mapstructure:"separate_description_file" comment:"Generate question description into a separate question.md file, otherwise it will be embed in the code file."`
Blocks []Block `yaml:"blocks,omitempty" mapstructure:"blocks" comment:"Replace some blocks of the generated code."`
Modifiers []Modifier `yaml:"modifiers,omitempty" mapstructure:"modifiers" comment:"Functions that modify the generated code."`
PostPickAction string `yaml:"post_pick_action,omitempty" mapstructure:"post_pick_action" comment: "Run command after picking problem."`
}

type GoConfig struct {
Expand Down
15 changes: 8 additions & 7 deletions lang/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ const (
const manualWarning = "Warning: this is a manual question, the generated test code may be incorrect."

type GenerateResult struct {
mask int
Question *leetcode.QuestionData
Lang Lang
OutDir string
SubDir string
Files []FileOutput
ResultHooks []func(*GenerateResult) error
mask int
Question *leetcode.QuestionData
Lang Lang
OutDir string
SubDir string
Files []FileOutput
ResultHooks []func(*GenerateResult) error
PostPickError string
}

type FileOutput struct {
Expand Down
38 changes: 38 additions & 0 deletions lang/gen.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package lang

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"text/template"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
Expand Down Expand Up @@ -105,6 +108,41 @@ func generate(q *leetcode.QuestionData) (Lang, *GenerateResult, error) {
}
result.Files[i].Written = written
}

// Execute post-pick action after file writes
postAction := getCodeStringConfig(gen, "post_pick_action")
if postAction != "" {
// Build the substitution context.
data := struct {
Folder string
}{
Folder: result.TargetDir(),
}
// Parse and execute the template.
tmpl, err := template.New("postPick").Parse(postAction)
if err != nil {
result.PostPickError = fmt.Sprintf("post-pick action template parsing failed: %v", err)
} else {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
result.PostPickError = fmt.Sprintf("post-pick action template execution failed: %v", err)
} else {
substitutedCommand := buf.String()
// Split the substituted command into parts.
parts := strings.Fields(substitutedCommand)
if len(parts) > 0 {
log.Info("executing", "post_pick_action", substitutedCommand)
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
result.PostPickError = fmt.Sprintf("%v", err)
}
}
}
}
}

return gen, result, nil
}

Expand Down