-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (79 loc) · 1.82 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"flag"
"fmt"
"os"
"sync"
"time"
"github.com/audunmo/action-version/internal/files"
"github.com/briandowns/spinner"
)
func process(fileNames []string, status chan string, done chan bool) error {
errs := make(chan error, len(fileNames))
var wg sync.WaitGroup
for _, file := range fileNames {
wg.Add(1)
go func(file string) {
defer wg.Done()
status <- fmt.Sprintf("Updating %s", file)
err := files.UpdateFile(file, status, os.WriteFile)
if err != nil {
errs <- err
}
}(file)
}
wg.Wait()
done <- true
close(errs)
return <-errs
}
func handleSpinner(cancel func(), status chan string, done chan bool) {
go func() {
s := spinner.New(spinner.CharSets[14], 50*time.Millisecond)
s.Color("yellow")
s.FinalMSG = ""
s.Start()
for {
select {
case ss := <-status:
s.Lock()
s.Suffix = ss
s.Unlock()
case <-done:
s.Stop()
cancel()
close(done)
return
}
}
}()
}
func main() {
var recursive bool
flag.BoolVar(&recursive, "r", false, "Recursively apply to .md, .yaml, and .yml files in subdirectories as well")
flag.Parse()
dir, err := os.Getwd()
if err != nil {
panic(err)
}
ff, err := files.ListDir(dir, recursive, make(map[string]bool))
if err != nil {
panic(err)
}
if len(ff) == 0 {
panic("No .md, .yaml or .yml files found. Please run this command in a directory with .yaml or .yml Github Action files.")
}
// Receive status updates from the process function, to push to the spinner.
status := make(chan string)
// Channel to stop the spinner. Indicates all processing is done
done := make(chan bool)
var wg sync.WaitGroup
wg.Add(1)
handleSpinner(func() { wg.Done() }, status, done)
err = process(ff, status, done)
if err != nil {
panic(err)
}
wg.Wait()
fmt.Printf("🚀 ✅ Successfully pinned Github Actions versions")
}