-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from clarsonneur/flow-management
Flow management
- Loading branch information
Showing
33 changed files
with
1,139 additions
and
185 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
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,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/forj-oss/forjj-modules/trace" | ||
) | ||
|
||
// FlowStart load the flow in memory, | ||
// configure it with Forjfile information | ||
// and update Forjfile inMemory object data. | ||
func (a *Forj)FlowInit() error { | ||
bInError := false | ||
if err := a.flows.Load(a.f.GetDeclaredFlows() ...) ; err != nil { | ||
return err | ||
} | ||
default_flow_to_apply := "default" | ||
if v, found := a.f.Get("settings", "default", "flow") ; found { | ||
default_flow_to_apply = v.GetString() | ||
} | ||
|
||
if err := a.flows.Apply(default_flow_to_apply, nil, &a.f) ; err != nil {// Applying Flow to Forjfile | ||
gotrace.Error("Forjfile: %s", err) | ||
bInError = true | ||
} | ||
|
||
for _, repo := range a.f.Forjfile().Repos { | ||
flow_to_apply := default_flow_to_apply | ||
if repo.Flow.Name != "" { | ||
flow_to_apply = repo.Flow.Name | ||
} | ||
|
||
if err := a.flows.Apply(flow_to_apply, repo, &a.f) ; err != nil {// Applying Flow to Forjfile repo | ||
gotrace.Error("Repo '%s': %s", repo.GetString("name"), err) | ||
bInError = true | ||
} | ||
} | ||
|
||
if bInError { | ||
return fmt.Errorf("Several errors has been detected when trying to apply flows on Repositories. %s", "Please review and fix them.") | ||
} | ||
|
||
return nil | ||
} | ||
|
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,108 @@ | ||
package flow | ||
|
||
import ( | ||
"forjj/forjfile" | ||
"github.com/forj-oss/forjj-modules/trace" | ||
"fmt" | ||
"forjj/utils" | ||
) | ||
|
||
type FlowDefine struct { // Yaml structure | ||
Name string | ||
Title string // Flow title | ||
Define map[string]FlowPluginTypeDef | ||
OnRepo map[string]FlowTaskDef `yaml:"on-repo-do"` | ||
OnForj map[string]FlowTaskDef `yaml:"on-forjfile-do"` | ||
} | ||
|
||
func (fd *FlowDefine)apply(repo *forjfile.RepoStruct, Forjfile *forjfile.Forge) error { | ||
bInError := false | ||
|
||
var tasks map[string]FlowTaskDef | ||
if repo == nil { | ||
tasks = fd.OnForj | ||
} else { | ||
tasks = fd.OnRepo | ||
} | ||
|
||
for _, flowTask := range tasks { | ||
onWhat := "Forjfile" | ||
if repo != nil { | ||
onWhat = fmt.Sprintf("repository '%s'", repo.GetString("name")) | ||
} else { | ||
|
||
} | ||
gotrace.Trace("flow '%s': %s on %s is being checked.\n---", fd.Name, flowTask.Description, onWhat) | ||
|
||
task_to_set, err := flowTask.if_section(repo, Forjfile) | ||
if err != nil { | ||
gotrace.Error("Flow '%s' - if section: Unable to apply flow task '%s'.", fd.Name, err) | ||
bInError = true | ||
continue | ||
} | ||
|
||
if ! task_to_set { | ||
gotrace.Trace("Flow task not applied to %s. if condition fails.", onWhat) | ||
continue | ||
} | ||
|
||
gotrace.Trace("'%s' flow task \"%s\" applying to %s.", fd.Name, flowTask.Description, onWhat) | ||
|
||
tmpl_data := New_FlowTaskModel(repo, Forjfile) | ||
|
||
if flowTask.List == nil { | ||
if err := flowTask.Set.apply(tmpl_data, Forjfile); err != nil { | ||
gotrace.Error("Unable to apply '%s' flow task '%s' on %s. %s", fd.Name, flowTask.Description, onWhat, err) | ||
continue | ||
} | ||
gotrace.Trace("'%s' flow task '%s' applied on %s.\n---", fd.Name, flowTask.Description, onWhat) | ||
continue | ||
} | ||
|
||
// Load list | ||
max := make([]int, len(flowTask.List)) | ||
|
||
for index, taskList := range flowTask.List { | ||
taskList.list = taskList.Get(repo, Forjfile) | ||
max[index] = len(taskList.list) | ||
} | ||
|
||
// Loop on list and set CurrentList | ||
looplist := utils.NewMLoop(max...) | ||
tmpl_data.List = make(map[string]interface{}) | ||
for !looplist.Eol() { | ||
for index, pos := range looplist.Cur() { | ||
flowTaskList := flowTask.List[index] | ||
tmpl_data.List[flowTaskList.Name] = flowTaskList.list[pos] | ||
} | ||
|
||
if err := flowTask.Set.apply(tmpl_data, Forjfile); err != nil { | ||
gotrace.Error("Unable to apply flow task '%s' on %s. %s", fd.Name, onWhat, err) | ||
} else { | ||
gotrace.Trace("'%s' flow task '%s' applied on %s.\n---", fd.Name, flowTask.Description, onWhat) | ||
} | ||
|
||
looplist.Increment() | ||
} | ||
} | ||
if bInError { | ||
return fmt.Errorf("Failed to apply '%s'. Errors detected.", fd.Name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ftd *FlowTaskDef)if_section(repo *forjfile.RepoStruct, Forjfile *forjfile.Forge) (task_to_set bool, _ error) { | ||
task_to_set = true | ||
if ftd.If != nil { | ||
for _, ftif := range ftd.If { | ||
if v, err := ftif.IfEvaluate(repo, Forjfile); err != nil { | ||
return false, err | ||
} else if !v { | ||
task_to_set = false | ||
break | ||
} | ||
} | ||
} | ||
return | ||
} |
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,50 @@ | ||
package flow | ||
|
||
import ( | ||
"forjj/forjfile" | ||
"text/template" | ||
"fmt" | ||
"bytes" | ||
"strconv" | ||
"strings" | ||
"github.com/forj-oss/forjj-modules/trace" | ||
) | ||
|
||
type FlowTaskIf struct { | ||
Rule string | ||
List map[string]string `yaml:",inline"` | ||
} | ||
|
||
// IfEvaluate will interpret | ||
func (fti *FlowTaskIf)IfEvaluate(repo *forjfile.RepoStruct, Forjfile *forjfile.Forge) (_ bool, _ error) { | ||
if fti.Rule != "" { | ||
var doc bytes.Buffer | ||
|
||
if t, err:= template.New("flow-eval").Funcs(template.FuncMap{ | ||
}).Parse(fti.Rule); err != nil { | ||
return false, fmt.Errorf("Error in template evaluation. %s", err) | ||
} else { | ||
t.Execute(&doc, New_FlowTaskModel(repo, Forjfile)) | ||
} | ||
|
||
result := doc.String() | ||
gotrace.Trace("'%s' evaluated to '%s'", fti.Rule, result) | ||
switch strings.ToLower(result) { | ||
case "", "not found" : | ||
return | ||
case "found" : | ||
return true, nil | ||
default: | ||
return strconv.ParseBool(result) | ||
} | ||
} | ||
|
||
if fti.List != nil { | ||
rules := make([]string, 0, len(fti.List)) | ||
for key, value := range fti.List { | ||
rules = append(rules, key + ":" + value) | ||
} | ||
return repo.HasValues(rules ...) | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.