/
cmd-common.go
85 lines (71 loc) · 2.06 KB
/
cmd-common.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
package cmd
import (
"context"
"errors"
"fmt"
"sync"
"github.com/go-git/go-git/v5"
"github.com/pterm/pterm"
"github.com/tevino/abool/v2"
)
func printProjectInfoContext(ctx context.Context) {
if printProjectInfoHeaderFn, ok := ctx.Value(ctxKeyPrintProjectInfoHeaderFn{}).(func()); ok {
printProjectInfoHeaderFn()
}
}
func taskUpdateFn(
ctx context.Context,
cmdName string,
config *configStateV2,
index int,
repoPath string,
runFn func(context.Context, string) error,
cleanupFn func(string, int, error) error,
) func() error {
return func() error {
var (
printProjectInfoHeaderOnce sync.Once
isUpdateMutexLocked = abool.New()
)
printProjectInfoHeaderFn := func() {
printProjectInfoHeaderOnce.Do(func() {
_, remoteURL, branchName, err := gitProjectInfo(repoPath)
if err != nil {
err = fmt.Errorf("'%s': %w", repoPath, err)
ptermErrorMessageStyle.Println(err.Error())
return
}
pterm.Println()
pterm.Printfln("[%d/%d] (active: %d)", index, config.TotalCount, len(config.Paths))
pterm.Println(repoPath)
ptermInfoMessageStyle.Println(remoteURL)
ptermScopeStyle.Println(branchName.Name().Short())
})
}
// context setup
ctx = context.WithValue(ctx, ctxKeyPrintProjectInfoHeaderFn{}, printProjectInfoHeaderFn)
ctx = context.WithValue(ctx, ctxKeyIsUpdateMutexLocked{}, isUpdateMutexLocked)
ctx = context.WithValue(ctx, ctxKeyShouldUpdateMutexUnlock{}, false)
err := runFn(ctx, repoPath)
// NOTE: error check comes after lock
if isUpdateMutexLocked.IsNotSet() {
updateMutex.Lock()
}
defer updateMutex.Unlock()
onlyUpdated, _ := ctx.Value(ctxKeyOnlyUpdated{}).(bool)
if !onlyUpdated {
printProjectInfoContext(ctx)
}
// NOTE: delayed error check
if err != nil {
// skip missing repositories
if errors.Is(err, git.ErrRepositoryNotExists) {
return cleanupFn(repoPath, index, nil)
}
printProjectInfoContext(ctx)
ptermErrorMessageStyle.Println(err.Error())
err = fmt.Errorf("%s '%s': %w", cmdName, repoPath, err)
}
return cleanupFn(repoPath, index, err)
}
}