Skip to content

Commit

Permalink
Add leetgo extract
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Jan 30, 2023
1 parent ea25245 commit a8019ab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Available Commands:
test Run question test cases
submit Submit solution
edit Open solution in editor
extract Extract solution code from generated file
contest Generate contest questions
cache Manage local questions cache
config Show configurations
Expand Down Expand Up @@ -160,10 +161,11 @@ code:
# Replace some blocks of the generated code
blocks:
- name: beforeMarker
template: |
template: |+
package main
{{ if .NeedsDefinition -}} import . "github.com/j178/leetgo/testutils/go" {{- end }}
# Functions that modify the generated code
modifiers:
- name: removeUselessComments
Expand Down
4 changes: 3 additions & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Available Commands:
test Run question test cases
submit Submit solution
edit Open solution in editor
extract Extract solution code from generated file
contest Generate contest questions
cache Manage local questions cache
config Show configurations
Expand Down Expand Up @@ -162,10 +163,11 @@ code:
# Replace some blocks of the generated code
blocks:
- name: beforeMarker
template: |
template: |+
package main
{{ if .NeedsDefinition -}} import . "github.com/j178/leetgo/testutils/go" {{- end }}
# Functions that modify the generated code
modifiers:
- name: removeUselessComments
Expand Down
22 changes: 22 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,25 @@ var editCmd = &cobra.Command{
return editor.Open(result.Files)
},
}

var extractCmd = &cobra.Command{
Use: "extract qid",
Short: "Extract solution code from generated file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := leetcode.NewClient()
qs, err := leetcode.ParseQID(args[0], c)
if err != nil {
return err
}
if len(qs) > 1 {
return fmt.Errorf("multiple questions found")
}
code, err := lang.GetSolutionCode(qs[0])
if err != nil {
return err
}
cmd.Println(code)
return nil
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func initCommands() {
testCmd,
submitCmd,
editCmd,
extractCmd,
contestCmd,
cacheCmd,
configCmd,
Expand Down
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"strings"

"github.com/hashicorp/go-hclog"
"github.com/j178/leetgo/utils"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"

"github.com/j178/leetgo/utils"
)

const (
Expand Down Expand Up @@ -191,11 +192,12 @@ func Default() *Config {
Blocks: []Block{
{
Name: "beforeMarker", Template: fmt.Sprintf(
`package main
`package main
{{ if .NeedsDefinition -}} import . "%s" {{- end }}
`, GoTestUtilsModPath,
),
),
},
},
Modifiers: []Modifier{
Expand Down

0 comments on commit a8019ab

Please sign in to comment.