Add more tests for modifycommand
This commit is contained in:
@@ -6,11 +6,13 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ModifyCommand struct {
|
||||
Name string `yaml:"name"`
|
||||
Pattern string `yaml:"pattern"`
|
||||
LuaExpr string `yaml:"lua"`
|
||||
Lua string `yaml:"lua"`
|
||||
Files []string `yaml:"files"`
|
||||
Git bool `yaml:"git"`
|
||||
Reset bool `yaml:"reset"`
|
||||
@@ -22,7 +24,7 @@ func (c *ModifyCommand) Validate() error {
|
||||
if c.Pattern == "" {
|
||||
return fmt.Errorf("pattern is required")
|
||||
}
|
||||
if c.LuaExpr == "" {
|
||||
if c.Lua == "" {
|
||||
return fmt.Errorf("lua expression is required")
|
||||
}
|
||||
if len(c.Files) == 0 {
|
||||
@@ -143,7 +145,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
||||
|
||||
command := ModifyCommand{
|
||||
Pattern: args[0],
|
||||
LuaExpr: args[1],
|
||||
Lua: args[1],
|
||||
Files: args[2:],
|
||||
Git: *GitFlag,
|
||||
Reset: *ResetFlag,
|
||||
@@ -158,7 +160,39 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
|
||||
return nil, nil
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
||||
}
|
||||
|
||||
commands := []ModifyCommand{}
|
||||
cookFiles, err := doublestar.Glob(os.DirFS(cwd), "*.yaml")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to glob cook files: %w", err)
|
||||
}
|
||||
|
||||
for _, cookFile := range cookFiles {
|
||||
cookFileData, err := os.ReadFile(cookFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read cook file: %w", err)
|
||||
}
|
||||
newcommands, err := LoadCommandsFromCookFile(cookFileData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load commands from cook file: %w", err)
|
||||
}
|
||||
commands = append(commands, newcommands...)
|
||||
}
|
||||
|
||||
return commands, nil
|
||||
}
|
||||
|
||||
func LoadCommandsFromCookFile(cookFileData []byte) ([]ModifyCommand, error) {
|
||||
commands := []ModifyCommand{}
|
||||
err := yaml.Unmarshal(cookFileData, &commands)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal cook file: %w", err)
|
||||
}
|
||||
return commands, nil
|
||||
}
|
||||
|
||||
// CountGlobsBeforeDedup counts the total number of glob patterns across all commands before deduplication
|
||||
|
Reference in New Issue
Block a user