@@ -175,7 +175,8 @@ func (gn *graphNode) execute(ctx context.Context, rs *runState) (err error) {
175175 "task %s: mismatch between task Provides declaration and returned bindings: missing bindings [%s], got extra bindings [%s]" ,
176176 gn .task .Name (),
177177 strings .Join (missing , ", " ),
178- strings .Join (extra , ", " ))
178+ strings .Join (extra , ", " ),
179+ )
179180 }
180181
181182 for _ , dependent := range gn .dependents {
@@ -263,7 +264,8 @@ func (g *graph) Run(ctx context.Context, inputs ...Binding) (b Binder, err error
263264 if err != nil {
264265 result = "error"
265266 }
266- executionLatency .WithLabelValues (g .name , result ).Observe (float64 (time .Since (startTime ) / time .Millisecond ))
267+ executionLatency .WithLabelValues (g .name , result ).
268+ Observe (float64 (time .Since (startTime ) / time .Millisecond ))
267269 }()
268270 base , err := g .buildInputBinder (inputs ... )
269271 if err != nil {
@@ -352,7 +354,10 @@ func (g *graph) AsTask(exposeKeys ...ID) (Task, error) {
352354 }
353355 }
354356 if len (missing ) > 0 {
355- return nil , wrapStackErrorf ("exposed key(s) not bound after graph execution: %s" , strings .Join (missing , ", " ))
357+ return nil , wrapStackErrorf (
358+ "exposed key(s) not bound after graph execution: %s" ,
359+ strings .Join (missing , ", " ),
360+ )
356361 }
357362
358363 // The exposed keys are added to the external binder via the graphTaskBinder, so we don't return
@@ -371,7 +376,10 @@ func (g *graph) Graphviz(includeInputs bool) string {
371376 for _ , dep := range n .task .Depends () {
372377 if ! g .allProvided .Contains (dep ) {
373378 inputID := fmt .Sprintf ("%s_input_%s" , n .id , dep .id )
374- nodes = append (nodes , fmt .Sprintf (" %s [label=\" Input - %s\" , shape=diamond];" , inputID , dep ))
379+ nodes = append (
380+ nodes ,
381+ fmt .Sprintf (" %s [label=\" Input - %s\" , shape=diamond];" , inputID , dep ),
382+ )
375383 edges = append (edges , fmt .Sprintf (" %s -> %s;" , inputID , n .id ))
376384 }
377385 }
@@ -384,8 +392,14 @@ func (g *graph) Graphviz(includeInputs bool) string {
384392 for _ , dep := range n .task .Provides () {
385393 if ! g .allDependencies .Contains (dep ) {
386394 outputID := fmt .Sprintf ("%s_output_%s" , n .id , dep )
387- nodes = append (nodes , fmt .Sprintf (" %s [label=\" Output\" , shape=diamond];" , outputID ))
388- edges = append (edges , fmt .Sprintf (" %s -> %s [label=\" %s\" ];" , n .id , outputID , dep ))
395+ nodes = append (
396+ nodes ,
397+ fmt .Sprintf (" %s [label=\" Output\" , shape=diamond];" , outputID ),
398+ )
399+ edges = append (
400+ edges ,
401+ fmt .Sprintf (" %s -> %s [label=\" %s\" ];" , n .id , outputID , dep ),
402+ )
389403 }
390404 }
391405 }
@@ -402,15 +416,17 @@ func (g *graph) Graphviz(includeInputs bool) string {
402416 return buf .String ()
403417}
404418
405- type GraphOptions struct {
419+ type graphOptions struct {
406420 tasks []Task
407421 tracer trace.Tracer
408422}
409423
410- type GraphOption func (opts * GraphOptions ) error
424+ // A GraphOption is used to configure a new Graph.
425+ type GraphOption func (opts * graphOptions ) error
411426
427+ // WithTasks sets the tasks which form the graph.
412428func WithTasks (tasks ... TaskSet ) GraphOption {
413- return func (opts * GraphOptions ) error {
429+ return func (opts * graphOptions ) error {
414430 opts .tasks = taskset (tasks ).Tasks ()
415431
416432 if len (opts .tasks ) > taskLimit {
@@ -421,8 +437,9 @@ func WithTasks(tasks ...TaskSet) GraphOption {
421437 }
422438}
423439
440+ // WithTracer sets a tracer to record graph execution.
424441func WithTracer (tracer trace.Tracer ) GraphOption {
425- return func (opts * GraphOptions ) error {
442+ return func (opts * graphOptions ) error {
426443 opts .tracer = tracer
427444
428445 return nil
@@ -433,7 +450,7 @@ func WithTracer(tracer trace.Tracer) GraphOption {
433450//
434451// Ideally, Graphs should be created on program startup, rather than creating them dynamically.
435452func New (name string , opts ... GraphOption ) (Graph , error ) {
436- o := & GraphOptions {
453+ o := & graphOptions {
437454 tracer : noop .NewTracerProvider ().Tracer ("github.com/thought-machine/taskgraph" ),
438455 }
439456
@@ -458,7 +475,10 @@ func New(name string, opts ...GraphOption) (Graph, error) {
458475 var badTaskErrs error
459476 for _ , t := range g .tasks {
460477 if t .Name () == "" || t .Location () == "" {
461- badTaskErrs = errors .Join (badTaskErrs , fmt .Errorf ("tasks must have a name and location: (%s, %s)" , t .Name (), t .Location ()))
478+ badTaskErrs = errors .Join (
479+ badTaskErrs ,
480+ fmt .Errorf ("tasks must have a name and location: (%s, %s)" , t .Name (), t .Location ()),
481+ )
462482 }
463483 node := & graphNode {
464484 id : sanitizeTaskName (t .Name ()),
@@ -477,7 +497,10 @@ func New(name string, opts ...GraphOption) (Graph, error) {
477497
478498 g .allProvided .Append (t .Provides ()... )
479499 for _ , id := range t .Provides () {
480- provideTasks [id .String ()] = append (provideTasks [id .String ()], fmt .Sprintf ("%s - %s" , t .Name (), t .Location ()))
500+ provideTasks [id .String ()] = append (
501+ provideTasks [id .String ()],
502+ fmt .Sprintf ("%s - %s" , t .Name (), t .Location ()),
503+ )
481504 }
482505 }
483506 if badTaskErrs != nil {
@@ -486,20 +509,34 @@ func New(name string, opts ...GraphOption) (Graph, error) {
486509 var duplicateTaskNames []string
487510 for name , locations := range taskLocations {
488511 if len (locations ) > 1 {
489- duplicateTaskNames = append (duplicateTaskNames , fmt .Sprintf ("%s (%s)" , name , strings .Join (locations , ", " )))
512+ duplicateTaskNames = append (
513+ duplicateTaskNames ,
514+ fmt .Sprintf ("%s (%s)" , name , strings .Join (locations , ", " )),
515+ )
490516 }
491517 }
492518 if len (duplicateTaskNames ) > 0 {
493- return nil , wrapStackErrorf ("%w: %s" , ErrDuplicateTaskNames , strings .Join (duplicateTaskNames , ", " ))
519+ return nil , wrapStackErrorf (
520+ "%w: %s" ,
521+ ErrDuplicateTaskNames ,
522+ strings .Join (duplicateTaskNames , ", " ),
523+ )
494524 }
495525 var duplicateProvides []string
496526 for id , tasks := range provideTasks {
497527 if len (tasks ) > 1 {
498- duplicateProvides = append (duplicateProvides , fmt .Sprintf ("%s (%s)" , id , strings .Join (tasks , ", " )))
528+ duplicateProvides = append (
529+ duplicateProvides ,
530+ fmt .Sprintf ("%s (%s)" , id , strings .Join (tasks , ", " )),
531+ )
499532 }
500533 }
501534 if len (duplicateProvides ) > 0 {
502- return nil , wrapStackErrorf ("%w: %s" , ErrDuplicateProvidedKeys , strings .Join (duplicateProvides , ", " ))
535+ return nil , wrapStackErrorf (
536+ "%w: %s" ,
537+ ErrDuplicateProvidedKeys ,
538+ strings .Join (duplicateProvides , ", " ),
539+ )
503540 }
504541
505542 for _ , node := range g .nodes {
@@ -543,7 +580,11 @@ func sanitizeTaskName(name string) string {
543580func checkCycle (node * graphNode , path []string ) error {
544581 for i := len (path ) - 1 ; i >= 0 ; i -- {
545582 if path [i ] == node .task .Name () {
546- return wrapStackErrorf ("%w: %s" , ErrGraphCycle , strings .Join (append (path [i :], path [i ]), " -> " ))
583+ return wrapStackErrorf (
584+ "%w: %s" ,
585+ ErrGraphCycle ,
586+ strings .Join (append (path [i :], path [i ]), " -> " ),
587+ )
547588 }
548589 }
549590 path = append (path , node .task .Name ())
0 commit comments