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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package main
import (
_ "embed"
"fmt"
"github.com/spf13/pflag"
"os"
"pik/cache"
"pik/crawl"
"pik/flags"
"pik/git"
"pik/indexers/pikdex"
"pik/menu"
"pik/model"
"pik/paths"
"pik/run"
"pik/runner/exc"
"pik/runner/gnumake"
"pik/runner/just"
"pik/runner/python"
"pik/runner/shell"
"pik/search"
"pik/spool"
"sync"
)
// syncInitializers are ran before the initializers.
// useful for initializing stuff like paths, preparing directories, and reading the environment
var syncInitializers = []model.Initializer{
paths.Paths,
cache.Init,
}
// initializers are ran before indexing with the indexers,
// data from the syncInitializers can be accessed at this time.
var initializers = []model.Initializer{
pikdex.Indexer,
python.Python,
git.Git,
}
// indexers are methods which scan a directory and return a number of targets.
var indexers = []model.Indexer{
pikdex.Indexer,
just.Indexer,
gnumake.Indexer,
}
// runners are modules which know how to turn a file into an exec.Cmd
// all indexers have access to these but only pikdex uses it
var runners = []model.Runner{
shell.Runner,
python.Python,
exc.Exc,
}
// hydrators are ran when the menu is required
// for example adding git info, descriptions, icons...
var hydrators = []model.Modder{
pikdex.Indexer,
git.Git,
}
// ForceConfirm means we will have to ask for confirmation before running no matter what
var ForceConfirm = false
// SourcesWithoutResults is a failed cache from the previous iteration
// used for stripping out results to prevent double-index
var SourcesWithoutResults *cache.Cache
//go:embed version.txt
var version string
func main() {
pflag.Parse()
switch {
case *flags.Version:
_, _ = spool.Print("%s\n", version)
os.Exit(0)
}
for _, i := range syncInitializers {
err := i.Init()
if err != nil {
_, _ = spool.Warn("%v\n", err)
}
}
wg := sync.WaitGroup{}
for _, i := range initializers {
wg.Go(func() {
err := i.Init()
if err != nil {
_, _ = spool.Warn("%v\n", err)
}
})
}
wg.Wait()
here, err := os.Getwd()
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
locs := crawl.RichLocations(here)
last := locs[len(locs)-1]
root, err := os.OpenRoot(last)
if root == nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
fs := root.FS()
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
var st *model.State
var stateErrors []error
var c *cache.Cache
if !*flags.All {
st, stateErrors = model.NewState(fs, locs, indexers, runners)
err = cache.Insert(st)
if err != nil {
spool.Warn("%v\n", err)
}
} else {
c, err = cache.LoadFile(fs, cache.Path[1:])
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
st, stateErrors = cache.LoadState(fs, c, indexers, runners)
}
if stateErrors != nil {
_, _ = spool.Warn("%v\n", stateErrors)
}
if *flags.List {
for _, s := range st.Sources {
for _, t := range s.Targets {
_, _ = spool.Print(t.ShortestId() + paths.Ifs)
}
}
os.Exit(0)
}
args := pflag.Args()
if len(args) == 0 {
source, target, err := menu.Show(st, hydrators)
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
if target == nil {
_, _ = spool.Warn("no target selected.\n")
os.Exit(0)
}
err = run.Run(source.Source, target.Target(), args...)
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
return
}
result := search.Search(st, args...)
// TODO: Move auto-all logic into Search?
if !*flags.All && result.Target == nil && len(result.Args) > 0 {
ForceConfirm = true
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
SourcesWithoutResults = c
main()
return
}
if result.Target == nil {
_, _ = spool.Warn("target not found.")
os.Exit(1)
return
}
if result.NeedsConfirmation || ForceConfirm {
_, _ = fmt.Fprintf(os.Stderr, "this target is out of tree.\n")
if !menu.Confirm(os.Stdin, result.Source, result.Target, args...) {
os.Exit(0)
}
}
if result.Overridden {
_, _ = fmt.Fprintln(os.Stderr, menu.OverrideWarning(result.Target))
}
if *flags.Edit {
err = run.Edit(result.Target, result.Source)
} else {
err = run.Run(result.Source, result.Target, result.Args...)
}
if err != nil {
_, _ = spool.Warn("%v\n", err)
os.Exit(1)
}
}
|