-
Notifications
You must be signed in to change notification settings - Fork 0
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 #36 from ccremer/package
Restructure packages
- Loading branch information
Showing
21 changed files
with
288 additions
and
294 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 |
---|---|---|
|
@@ -11,15 +11,14 @@ import ( | |
"testing" | ||
|
||
pipeline "github.com/ccremer/go-command-pipeline" | ||
"github.com/ccremer/go-command-pipeline/predicate" | ||
) | ||
|
||
func TestExample_Git(t *testing.T) { | ||
p := pipeline.NewPipeline() | ||
p.WithSteps( | ||
predicate.ToStep("clone repository", CloneGitRepository(), predicate.Not(DirExists("my-repo"))), | ||
pipeline.NewStep("checkout branch", CheckoutBranch()), | ||
pipeline.NewStep("pull", Pull()).WithResultHandler(logSuccess), | ||
CloneGitRepository(), | ||
CheckoutBranch(), | ||
Pull().WithResultHandler(logSuccess), | ||
) | ||
result := p.Run() | ||
if !result.IsSuccessful() { | ||
|
@@ -28,29 +27,29 @@ func TestExample_Git(t *testing.T) { | |
} | ||
|
||
func logSuccess(_ context.Context, result pipeline.Result) error { | ||
log.Println("handler called") | ||
log.Println("handler called", result.Name()) | ||
return result.Err() | ||
} | ||
|
||
func CloneGitRepository() pipeline.ActionFunc { | ||
return func(_ context.Context) pipeline.Result { | ||
func CloneGitRepository() pipeline.Step { | ||
return pipeline.ToStep("clone repository", func(_ context.Context) error { | ||
err := execGitCommand("clone", "[email protected]/ccremer/go-command-pipeline") | ||
return pipeline.NewResultWithError("clone repository", err) | ||
} | ||
return err | ||
}, pipeline.Not(DirExists("my-repo"))) | ||
} | ||
|
||
func Pull() pipeline.ActionFunc { | ||
return func(_ context.Context) pipeline.Result { | ||
func Pull() pipeline.Step { | ||
return pipeline.NewStepFromFunc("pull", func(_ context.Context) error { | ||
err := execGitCommand("pull") | ||
return pipeline.NewResultWithError("pull", err) | ||
} | ||
return err | ||
}) | ||
} | ||
|
||
func CheckoutBranch() pipeline.ActionFunc { | ||
return func(_ context.Context) pipeline.Result { | ||
func CheckoutBranch() pipeline.Step { | ||
return pipeline.NewStepFromFunc("checkout branch", func(_ context.Context) error { | ||
err := execGitCommand("checkout", "master") | ||
return pipeline.NewResultWithError("checkout branch", err) | ||
} | ||
return err | ||
}) | ||
} | ||
|
||
func execGitCommand(args ...string) error { | ||
|
@@ -61,7 +60,7 @@ func execGitCommand(args ...string) error { | |
return err | ||
} | ||
|
||
func DirExists(path string) predicate.Predicate { | ||
func DirExists(path string) pipeline.Predicate { | ||
return func(_ context.Context) bool { | ||
if info, err := os.Stat(path); err != nil || !info.IsDir() { | ||
return false | ||
|
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,42 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
/* | ||
NewFanOutStep creates a pipeline step that runs nested pipelines in their own Go routines. | ||
The function provided as Supplier is expected to close the given channel when no more pipelines should be executed, otherwise this step blocks forever. | ||
The step waits until all pipelines are finished. | ||
If the given ParallelResultHandler is non-nil it will be called after all pipelines were run, otherwise the step is considered successful. | ||
If the context is canceled, no new pipelines will be retrieved from the channel and the Supplier is expected to stop supplying new instances. | ||
Also, once canceled, the step waits for the remaining children pipelines and collects their result via given ParallelResultHandler. | ||
However, the error returned from ParallelResultHandler is wrapped in context.Canceled. | ||
*/ | ||
func NewFanOutStep(name string, pipelineSupplier Supplier, handler ParallelResultHandler) Step { | ||
step := Step{Name: name} | ||
step.F = func(ctx context.Context) Result { | ||
pipelineChan := make(chan *Pipeline) | ||
m := sync.Map{} | ||
var wg sync.WaitGroup | ||
i := uint64(0) | ||
|
||
go pipelineSupplier(ctx, pipelineChan) | ||
for pipe := range pipelineChan { | ||
p := pipe | ||
wg.Add(1) | ||
n := i | ||
i++ | ||
go func() { | ||
defer wg.Done() | ||
m.Store(n, p.RunWithContext(ctx)) | ||
}() | ||
} | ||
wg.Wait() | ||
res := collectResults(ctx, handler, &m) | ||
return setResultErrorFromContext(ctx, name, res) | ||
} | ||
return step | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.