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
|
package main
import (
"errors"
"github.com/ewy1/pik/completion"
"github.com/ewy1/pik/flags"
"github.com/ewy1/pik/model"
"github.com/ewy1/pik/paths"
"github.com/ewy1/pik/run"
"github.com/ewy1/pik/spool"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
)
// ModeMap maps flags to specific operation modes
type ModeMap[T any] map[*bool]T
// Continue can be returned as an error to continue program flow
var Continue = errors.New("not an error; continue flow")
var Success = errors.New("not an error; finished operations")
// Traverse checks the entries of the map. If any flags are set on,
// pik that mode. If Continue is returned, it's non-exclusive. Otherwise,
// we quit after one mode.
//
// `then` should simply be the method call (necessary due to generics)
func (m ModeMap[T]) Traverse(then func(in T) error) error {
for enabled, mode := range m {
if !*enabled {
continue
}
err := then(mode)
if errors.Is(err, Continue) {
continue
} else if err != nil {
return err
}
return Success
}
return nil
}
var profileFd *os.File
var UnknownShellError = errors.New("$SHELL not set or empty")
// uninitializedModes are modes which can run before the program runs initializers
var uninitializedModes = ModeMap[func() error]{
flags.Version: func() error {
_, err := spool.Print("%s\n", version)
return err
},
flags.Completion: func() error {
return completion.Echo()
},
}
// statelessModes are program modes which do not require state to operate.
// like --version and --completion
var statelessModes = ModeMap[func() error]{
flags.InstallCompletion: func() error {
sh := os.Getenv("SHELL")
if sh == "" {
return UnknownShellError
}
_, sh = filepath.Split(sh)
return completion.Add(sh)
},
flags.Profile: func() error {
fd, err := os.Create("pik-profile.out")
if err != nil {
return err
}
runtime.SetCPUProfileRate(1000)
err = pprof.StartCPUProfile(profileFd)
if err != nil {
return err
}
profileFd = fd
return Continue
},
}
var NoTargetsError = errors.New("no targets or sources to list")
// statefulModes are program modes which require a built state to be executed
var statefulModes = ModeMap[func(st *model.State) error]{
flags.List: func(st *model.State) error {
count := 0
for _, s := range st.Sources {
count++
_, _ = spool.Print("%v", s.Label()+paths.Ifs)
for _, t := range s.Targets {
_, _ = spool.Print("%v", t.ShortestId()+paths.Ifs)
count++
}
}
if count == 0 {
return NoTargetsError
}
return nil
},
}
// selectionModes are program modes which require a selected target, through menu or args
var selectionModes = ModeMap[func(st *model.State, src *model.Source, t model.Target) error]{
flags.Edit: func(st *model.State, src *model.Source, t model.Target) error {
return run.Edit(t, src)
},
}
|