-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (60 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"sync"
"github.com/VarthanV/simple-ci-pipeline-runner/pkg/objects"
"github.com/VarthanV/simple-ci-pipeline-runner/pkg/pipeline"
"github.com/fatih/color"
"github.com/google/uuid"
)
func main() {
dirName := uuid.NewString()
cleanupDone := make(chan struct{})
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
once := sync.Once{}
cleanup := func(dirName string) {
defer close(cleanupDone)
color.Blue("Cleaning up...")
// Change dir to parent
currentDir, err := os.Getwd()
if err != nil {
fmt.Println("Error:", err)
return
}
parentDir := filepath.Dir(currentDir)
err = os.Chdir(parentDir)
if err != nil {
log.Println("unablr to change to parent dir")
}
err = os.RemoveAll(dirName)
if err != nil {
log.Println("error in removing dir ", err)
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-sig
once.Do(func() {
cancel()
cleanup(dirName)
})
}()
// FIXME: can be refactored and improve readablity with custom contexts
// will do in the upcoming iteration
ctx = context.WithValue(ctx, objects.PipelineValueDirectoryName, dirName)
pipeline.Run(ctx)
once.Do(func() {
go func() {
cancel()
cleanup(dirName)
}()
})
<-cleanupDone
}