-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the include command and some tests, support to include other conf…
…ig files (#103)
- Loading branch information
Showing
15 changed files
with
327 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: actions step | ||
include: | ||
- actions_step1.yaml | ||
- actions_step2.yaml | ||
init: | ||
- print: | ||
input: call init from actions.yaml | ||
actions: | ||
- print: | ||
input: call actions from actions.yaml | ||
clear: | ||
- print: | ||
input: call clear from actions.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: actions step 1 | ||
init: | ||
- print: | ||
input: call init from actions_step1.yaml | ||
actions: | ||
- print: | ||
input: call actions from actions_step1.yaml | ||
clear: | ||
- print: | ||
input: call clear from actions_step1.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: actions step 2 | ||
init: | ||
- print: | ||
input: call init from actions_step2.yaml | ||
actions: | ||
- print: | ||
input: call actions from actions_step2.yaml | ||
clear: | ||
- print: | ||
input: call clear from actions_step2.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: clear step | ||
include: | ||
- clear_step1.yaml | ||
- clear_step2.yaml | ||
init: | ||
- print: | ||
input: call init from clear.yaml | ||
actions: | ||
- print: | ||
input: call actions from clear.yaml | ||
clear: | ||
- print: | ||
input: call clear from clear.yaml | ||
- rm: | ||
source: ./source | ||
- rm: | ||
source: ./dest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: clear step 1 | ||
init: | ||
- print: | ||
input: call init from clear_step1.yaml | ||
actions: | ||
- print: | ||
input: call actions from clear_step1.yaml | ||
clear: | ||
- print: | ||
input: call clear from clear_step1.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: clear step 2 | ||
init: | ||
- print: | ||
input: call init from clear_step2.yaml | ||
actions: | ||
- print: | ||
input: call actions from clear_step2.yaml | ||
clear: | ||
- print: | ||
input: call clear from clear_step2.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: command examples with include | ||
include: | ||
- init_source.yaml | ||
- init_dest.yaml | ||
- actions.yaml | ||
- clear.yaml | ||
init: | ||
- print: | ||
input: call init from include.yaml | ||
actions: | ||
- print: | ||
input: call actions from include.yaml | ||
clear: | ||
- print: | ||
input: call clear from include.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: command examples of infinite recursion | ||
include: | ||
- infinite_recursion.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: init dest path | ||
init: | ||
- print: | ||
input: call init from init_dest.yaml | ||
- mkdir: | ||
source: ./dest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: init source path | ||
init: | ||
- print: | ||
input: call init from init_source.yaml | ||
- mkdir: | ||
source: ./source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
errInfiniteRecursion = errors.New("find an infinite recursion") | ||
) | ||
|
||
const ( | ||
maxRecursionDepth = 10000 | ||
) | ||
|
||
type parser struct { | ||
c int // check infinite recursion | ||
} | ||
|
||
func newParser() *parser { | ||
return &parser{} | ||
} | ||
|
||
func (p *parser) ParseConfigFile(path string) (commands *Commands, err error) { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return | ||
} | ||
var c Config | ||
err = yaml.Unmarshal(data, &c) | ||
if err != nil { | ||
return | ||
} | ||
if len(c.IncludePath) == 0 { | ||
c.IncludePath = filepath.Dir(path) | ||
} | ||
return p.ParseConfig(c) | ||
} | ||
|
||
func (p *parser) ParseConfig(conf Config) (commands *Commands, err error) { | ||
commands, err = p.parseConfig(conf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
init []Command | ||
actions []Command | ||
clear []Command | ||
) | ||
for _, f := range conf.Include { | ||
baseCommands, err := p.ParseConfigFile(filepath.Join(conf.IncludePath, f)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
init = append(init, baseCommands.Init...) | ||
actions = append(actions, baseCommands.Actions...) | ||
clear = append(clear, baseCommands.Clear...) | ||
} | ||
|
||
commands.Init = append(init, commands.Init...) | ||
commands.Actions = append(actions, commands.Actions...) | ||
commands.Clear = append(clear, commands.Clear...) | ||
return commands, nil | ||
} | ||
|
||
func (p *parser) parseConfig(conf Config) (commands *Commands, err error) { | ||
p.c++ | ||
if p.c > maxRecursionDepth { | ||
return nil, errInfiniteRecursion | ||
} | ||
commands = &Commands{ | ||
Name: conf.Name, | ||
} | ||
commands.Init, err = p.parseCommands(conf.Init) | ||
if err != nil { | ||
return nil, err | ||
} | ||
commands.Actions, err = p.parseCommands(conf.Actions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
commands.Clear, err = p.parseCommands(conf.Clear) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return commands, nil | ||
} | ||
|
||
func (p *parser) parseCommands(actions []Action) (commands []Command, err error) { | ||
for _, action := range actions { | ||
var c Command | ||
for name, fn := range allCommands { | ||
if _, ok := action[name]; ok { | ||
c, err = fn(action) | ||
break | ||
} | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
if c == nil { | ||
return nil, errUnsupportedCommand | ||
} | ||
commands = append(commands, c) | ||
} | ||
return commands, nil | ||
} | ||
|
||
// ParseConfigFile parse the config file to a command list | ||
func ParseConfigFile(path string) (commands *Commands, err error) { | ||
return newParser().ParseConfigFile(path) | ||
} | ||
|
||
// ParseConfig parse the config to a command list | ||
func ParseConfig(conf Config) (commands *Commands, err error) { | ||
return newParser().ParseConfig(conf) | ||
} |
Oops, something went wrong.