diff options
| author | ewy <ewy0@protonmail.com> | 2026-05-02 20:17:50 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-05-02 20:17:50 +0200 |
| commit | 209a3bfa289513c1dfe28947407b0ecbedbc2b50 (patch) | |
| tree | 8705df02226d88d33901d38cbae575496bb79efe /modes.go | |
| parent | 81235da8faa7a2cfee4ed93fe301cd4e21529b70 (diff) | |
rework systems slightly to accommodate profiling (not working yet)
Diffstat (limited to 'modes.go')
| -rw-r--r-- | modes.go | 31 |
1 files changed, 23 insertions, 8 deletions
@@ -9,6 +9,8 @@ import ( "github.com/ewy1/pik/run" "github.com/ewy1/pik/spool" "os" + "runtime" + "runtime/pprof" ) // ModeMap maps flags to specific operation modes @@ -16,14 +18,14 @@ 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, -// run that mode. If Continue is returned, it's non-exclusive. Otherwise, +// 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) -// no additional error handling is required -func (m ModeMap[T]) Traverse(then func(in T) error) { +func (m ModeMap[T]) Traverse(then func(in T) error) error { for enabled, mode := range m { if !*enabled { continue @@ -32,14 +34,15 @@ func (m ModeMap[T]) Traverse(then func(in T) error) { if errors.Is(err, Continue) { continue } else if err != nil { - _, _ = spool.Warn("%v\n", err) - os.Exit(1) - } else { - os.Exit(0) + return err } + return Success } + return nil } +var profileFd *os.File + // statelessModes are program modes which do not require state to operate. // like --version and --completion var statelessModes = ModeMap[func() error]{ @@ -49,7 +52,19 @@ var statelessModes = ModeMap[func() error]{ }, flags.Completion: func() error { return completion.Echo() - + }, + 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 }, } |
