-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
179 lines (152 loc) · 4.94 KB
/
catalog.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package diagnostics
import (
"fmt"
"io/fs"
"os"
"reflect"
"github.com/nestoca/joy/internal/config"
"github.com/nestoca/joy/internal/git"
"github.com/nestoca/joy/internal/references"
"github.com/nestoca/joy/internal/style"
"github.com/nestoca/joy/pkg/catalog"
)
type GitOpts struct {
IsValid func(string) bool
GetUncommittedChanges func(string) ([]string, error)
GetCurrentBranch func(string) (string, error)
IsInSyncWithRemote func(string, string) (bool, error)
GetCurrentCommit func(string) (string, error)
}
type CatalogOpts struct {
Stat func(string) (fs.FileInfo, error)
LoadCatalog func(string, []string) (*catalog.Catalog, error)
CheckCatalog func(string) error
Git GitOpts
}
func diagnoseCatalog(cfg *config.Config, opts CatalogOpts) (group Group) {
if opts.Stat == nil {
opts.Stat = os.Stat
}
if opts.CheckCatalog == nil {
opts.CheckCatalog = config.CheckCatalogDir
}
if opts.LoadCatalog == nil {
opts.LoadCatalog = catalog.Load
}
if reflect.ValueOf(opts.Git).IsZero() {
opts.Git = GitOpts{
IsValid: git.IsValid,
GetUncommittedChanges: git.GetUncommittedChanges,
GetCurrentBranch: git.GetCurrentBranch,
IsInSyncWithRemote: git.IsBranchInSyncWithRemote,
GetCurrentCommit: git.GetCurrentCommit,
}
}
group.Title = "Catalog"
group.topLevel = true
group.SubGroups = append(group.SubGroups, func() (group Group) {
group.Title = "Git working copy"
if _, err := opts.Stat(cfg.CatalogDir); os.IsNotExist(err) {
group.AddMsg(failed, label("Directory does not exist", cfg.CatalogDir))
return
}
group.AddMsg(info, label("Directory exists", cfg.CatalogDir))
if !git.IsValid(cfg.CatalogDir) {
group.AddMsg(failed, "Working copy is invalid")
return
}
group.AddMsg(success, "Working copy is valid")
uncommittedChanges, err := opts.Git.GetUncommittedChanges(cfg.CatalogDir)
if err != nil {
group.AddMsg(failed, label("Failed checking for uncommitted changes", err.Error()))
}
if len(uncommittedChanges) > 0 {
group.AddMsg(
warning,
fmt.Sprintf("Working copy has %d uncommitted change(s)", len(uncommittedChanges)),
msg(hint, fmt.Sprintf("Commit your changes or discard them using: %s", style.Code("joy git reset --hard && joy git clean -fd"))),
)
} else {
group.AddMsg(success, "Working copy has no uncommitted changes")
}
const defaultBranch = "master"
currentBranch, err := opts.Git.GetCurrentBranch(cfg.CatalogDir)
if err != nil {
group.AddMsg(failed, label("Failed getting current branch", err.Error()))
} else {
if currentBranch != defaultBranch {
group.AddMsg(
warning,
fmt.Sprintf("Default branch %s should be checked out (instead of %s)", style.Code(defaultBranch), style.Code(currentBranch)),
msg(hint, fmt.Sprintf("Switch to default branch using: %s", style.Code("joy git checkout "+defaultBranch))),
)
} else {
group.AddMsg(success, fmt.Sprintf("Default branch %s is checked out", style.Code(currentBranch)))
}
}
isInSync, err := opts.Git.IsInSyncWithRemote(cfg.CatalogDir, defaultBranch)
if err != nil {
group.AddMsg(failed, label("Failed checking default branch sync state", err.Error()))
} else {
if isInSync {
group.AddMsg(success, "Default branch is in sync with remote")
} else {
group.AddMsg(
warning,
"Default branch is not in sync with remote",
msg(hint, fmt.Sprintf("Update your working copy using: %s", style.Code("joy pull"))),
)
}
}
commit, err := opts.Git.GetCurrentCommit(cfg.CatalogDir)
if err != nil {
group.AddMsg(failed, label("Failed getting current commit", err.Error()))
} else {
group.AddMsg(info, label("Current commit", commit))
}
return
}())
// Diagnose catalog working copy
var cat *catalog.Catalog
// Load catalog
group.AddSubGroup(func() (group Group) {
group.Title = "Loading catalog"
// Check catalog dir
if err := opts.CheckCatalog(cfg.CatalogDir); err != nil {
group.AddMsg(failed, label("Catalog not detected", err.Error()))
return
}
group.AddMsg(success, "Catalog detected")
cata, err := opts.LoadCatalog(cfg.CatalogDir, cfg.KnownChartRefs())
if err != nil {
group.AddMsg(failed, label("Failed loading catalog", err.Error()))
return
}
group.AddMsg(success, "Catalog loaded successfully")
cat = cata
return
}())
if cat == nil {
return
}
group.AddSubGroup(func() (group Group) {
group.Title = "Resources"
group.
AddMsg(info, label("Environments", len(cat.Environments))).
AddMsg(info, label("Projects", len(cat.Projects))).
AddMsg(info, label("Releases", len(cat.Releases.Items)))
return
}())
group.AddSubGroup(func() (group Group) {
group.Title = "Cross-references"
if err := cat.ResolveRefs(); err != nil {
for _, err := range references.AsMissingErrors(err) {
group.AddMsg(failed, err.StyledError())
}
return
}
group.AddMsg(success, "All resource cross-references resolved successfully")
return
}())
return
}