@@ -2,21 +2,20 @@ package tasks
22
33import (
44 "fmt"
5- "math/rand"
65 "time"
76
87 "github.com/briandowns/spinner"
98)
109
1110type Task func (args interface {}) (nextArgs interface {}, err error )
12- type TaskWithCleanup func (args interface {}) (nextArgs interface {}, cleanUpArgs interface {} , err error )
13- type CleanUpTask func (cleanUpArgs interface {} ) error
11+ type TaskWithCleanup [ T any ] func (args interface {}) (nextArgs interface {}, cleanupArgs T , err error )
12+ type Cleanup [ T any ] func (cleanupArgs T ) error
1413
1514type taskInfo struct {
1615 Name string
17- function TaskWithCleanup
18- cleanFunction CleanUpTask
19- cleanUpArgs interface {}
16+ function TaskWithCleanup [ any ]
17+ cleanFunction Cleanup [ any ]
18+ cleanupArgs interface {}
2019}
2120
2221type Tasks struct {
@@ -27,6 +26,7 @@ func Begin() *Tasks {
2726 return & Tasks {}
2827}
2928
29+ // Add a task that does not need cleanup
3030func (ts * Tasks ) Add (name string , task Task ) {
3131 ts .tasks = append (ts .tasks , taskInfo {
3232 Name : name ,
@@ -37,11 +37,16 @@ func (ts *Tasks) Add(name string, task Task) {
3737 })
3838}
3939
40- func (ts * Tasks ) AddWithCleanUp (name string , task TaskWithCleanup , clean CleanUpTask ) {
40+ // AddWithCleanUp adds a task to the list with a cleanup function in case of fail during tasks execution
41+ func AddWithCleanUp [T any ](ts * Tasks , name string , task TaskWithCleanup [T ], clean Cleanup [T ]) {
4142 ts .tasks = append (ts .tasks , taskInfo {
42- Name : name ,
43- function : task ,
44- cleanFunction : clean ,
43+ Name : name ,
44+ function : func (args interface {}) (nextArgs interface {}, cleanUpArgs any , err error ) {
45+ return task (args )
46+ },
47+ cleanFunction : func (cleanupArgs any ) error {
48+ return clean (cleanupArgs .(T ))
49+ },
4550 })
4651}
4752
@@ -50,32 +55,32 @@ func (ts *Tasks) Cleanup(failed int) {
5055 totalTasks := len (ts .tasks )
5156
5257 i := failed - 1
53- for ; i >= 0 ; i -= 1 {
58+ for ; i >= 0 ; i -- {
5459 task := ts .tasks [i ]
5560
5661 if task .cleanFunction != nil {
5762 fmt .Printf ("[%d/%d] Cleaning task %q\n " , i + 1 , totalTasks , task .Name )
5863
59- err := task .cleanFunction (task .cleanUpArgs )
64+ err := task .cleanFunction (task .cleanupArgs )
6065 if err != nil {
6166 fmt .Printf ("task %d failed to cleanup: %s" , i + 1 , err .Error ())
6267 }
6368 }
6469 }
6570}
6671
72+ // Execute tasks with interactive display and cleanup on fail
6773func (ts * Tasks ) Execute (data interface {}) (interface {}, error ) {
6874 var err error
6975 totalTasks := len (ts .tasks )
70- rand .Seed (time .Now ().UnixNano ())
71- spin := spinner .New (spinner .CharSets [rand .Int ()% 37 ], 100 * time .Millisecond )
76+ spin := spinner .New (spinner .CharSets [11 ], 100 * time .Millisecond )
7277
7378 for i := range ts .tasks {
7479 task := & ts .tasks [i ]
7580 fmt .Printf ("[%d/%d] %s\n " , i + 1 , totalTasks , task .Name )
7681 spin .Start ()
7782
78- data , task .cleanUpArgs , err = task .function (data )
83+ data , task .cleanupArgs , err = task .function (data )
7984 if err != nil {
8085 spin .Stop ()
8186 fmt .Println ("task failed, cleaning up created resources" )
0 commit comments