diff --git a/cmd/pick.go b/cmd/pick.go index 47d7955..38aeaa2 100644 --- a/cmd/pick.go +++ b/cmd/pick.go @@ -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" @@ -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) @@ -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 diff --git a/config/config.go b/config/config.go index 5fe63f0..3505454 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/lang/base.go b/lang/base.go index e1fb283..319d5cf 100644 --- a/lang/base.go +++ b/lang/base.go @@ -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 { diff --git a/lang/gen.go b/lang/gen.go index 9fffd3b..4ba6976 100644 --- a/lang/gen.go +++ b/lang/gen.go @@ -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" @@ -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 }