|
| 1 | +// Copyright 2020 The go-lpc Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Command mim-daq starts a TDAQ run-control with MIM specificities. |
| 6 | +package main // import "github.com/go-lpc/mim/cmd/mim-daq" |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/go-daq/tdaq" |
| 16 | + "github.com/go-daq/tdaq/config" |
| 17 | + "github.com/go-daq/tdaq/flags" |
| 18 | + "github.com/go-daq/tdaq/log" |
| 19 | + "github.com/peterh/liner" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + cmd := flags.NewRunControl() |
| 24 | + |
| 25 | + run(cmd, os.Stdout) |
| 26 | +} |
| 27 | + |
| 28 | +func run(cfg config.RunCtl, stdout io.Writer) { |
| 29 | + rc, err := tdaq.NewRunControl(cfg, os.Stdout) |
| 30 | + if err != nil { |
| 31 | + log.Errorf("could not create run control: %+v", err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + if cfg.Interactive { |
| 36 | + term := newShell(cfg, rc) |
| 37 | + defer term.Close() |
| 38 | + } |
| 39 | + |
| 40 | + err = rc.Run(context.Background()) |
| 41 | + if err != nil { |
| 42 | + log.Errorf("could not run run-ctl: %+v", err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func newShell(cfg config.RunCtl, rc *tdaq.RunControl) *liner.State { |
| 48 | + |
| 49 | + fmt.Printf(` |
| 50 | +:::::::::::::::::::::::::: |
| 51 | +::: RunControl shell ::: |
| 52 | +:::::::::::::::::::::::::: |
| 53 | +
|
| 54 | +- /config -> configure tdaq processes |
| 55 | +- /init -> initialize tdaq processes |
| 56 | +- /run -> start a new run |
| 57 | +- /stop -> stop current run |
| 58 | +- /reset -> reset tdaq processes |
| 59 | +- /status -> display status of all tdaq processes |
| 60 | +- /quit -> terminate tdaq processes (and quit) |
| 61 | +
|
| 62 | +`) |
| 63 | + |
| 64 | + ps1 := cfg.Name + ">> " |
| 65 | + term := liner.NewLiner() |
| 66 | + term.SetWordCompleter(shellCompleter) |
| 67 | + term.SetTabCompletionStyle(liner.TabPrints) |
| 68 | + |
| 69 | + go func() { |
| 70 | + quit := make(chan struct{}) |
| 71 | + ctx := context.Background() |
| 72 | + defer func() { |
| 73 | + select { |
| 74 | + case <-quit: |
| 75 | + default: |
| 76 | + go rc.Do(ctx, tdaq.CmdQuit) |
| 77 | + } |
| 78 | + }() |
| 79 | + |
| 80 | + for { |
| 81 | + o, err := term.Prompt(ps1) |
| 82 | + if err != nil { |
| 83 | + os.Stdout.Write([]byte("\n")) |
| 84 | + if err != io.EOF { |
| 85 | + log.Fatalf("error: %+v", err) |
| 86 | + } |
| 87 | + return |
| 88 | + } |
| 89 | + words := strings.Split(strings.TrimSpace(o), " ") |
| 90 | + if len(words) == 0 { |
| 91 | + continue |
| 92 | + } |
| 93 | + switch words[0] { |
| 94 | + case "": |
| 95 | + continue |
| 96 | + case "/config": |
| 97 | + term.AppendHistory(o) |
| 98 | + err = rc.Do(ctx, tdaq.CmdConfig) |
| 99 | + if err != nil { |
| 100 | + log.Errorf("could not run /config: %+v", err) |
| 101 | + continue |
| 102 | + } |
| 103 | + case "/init": |
| 104 | + term.AppendHistory(o) |
| 105 | + err = rc.Do(ctx, tdaq.CmdInit) |
| 106 | + if err != nil { |
| 107 | + log.Errorf("could not run /init: %+v", err) |
| 108 | + continue |
| 109 | + } |
| 110 | + case "/reset": |
| 111 | + term.AppendHistory(o) |
| 112 | + err = rc.Do(ctx, tdaq.CmdReset) |
| 113 | + if err != nil { |
| 114 | + log.Errorf("could not run /reset: %+v", err) |
| 115 | + continue |
| 116 | + } |
| 117 | + case "/run": |
| 118 | + term.AppendHistory(o) |
| 119 | + err = rc.Do(ctx, tdaq.CmdStart) |
| 120 | + if err != nil { |
| 121 | + log.Errorf("could not run /start: %+v", err) |
| 122 | + continue |
| 123 | + } |
| 124 | + case "/stop": |
| 125 | + term.AppendHistory(o) |
| 126 | + err = rc.Do(ctx, tdaq.CmdStop) |
| 127 | + if err != nil { |
| 128 | + log.Errorf("could not run /stop: %+v", err) |
| 129 | + continue |
| 130 | + } |
| 131 | + case "/quit": |
| 132 | + term.AppendHistory(o) |
| 133 | + err = rc.Do(ctx, tdaq.CmdQuit) |
| 134 | + if err != nil { |
| 135 | + log.Errorf("could not run /quit: %+v", err) |
| 136 | + continue |
| 137 | + } |
| 138 | + close(quit) |
| 139 | + return |
| 140 | + case "/status": |
| 141 | + term.AppendHistory(o) |
| 142 | + err = rc.Do(ctx, tdaq.CmdStatus) |
| 143 | + if err != nil { |
| 144 | + log.Errorf("could not run /status: %+v", err) |
| 145 | + continue |
| 146 | + } |
| 147 | + default: |
| 148 | + log.Errorf("invalid tdaq command %q", o) |
| 149 | + continue |
| 150 | + } |
| 151 | + } |
| 152 | + }() |
| 153 | + |
| 154 | + return term |
| 155 | +} |
| 156 | + |
| 157 | +func shellCompleter(line string, pos int) (prefix string, completions []string, suffix string) { |
| 158 | + if pos != len(line) { |
| 159 | + // TODO(sbinet): better mid-line matching... |
| 160 | + prefix, completions, suffix = shellCompleter(line[:pos], pos) |
| 161 | + return prefix, completions, suffix + line[pos:] |
| 162 | + } |
| 163 | + |
| 164 | + if strings.TrimSpace(line) == "" { |
| 165 | + return line, nil, "" |
| 166 | + } |
| 167 | + |
| 168 | + cmds := []string{ |
| 169 | + "/config", "/init", "/reset", |
| 170 | + "/run", "/stop", |
| 171 | + "/quit", |
| 172 | + "/status", |
| 173 | + } |
| 174 | + |
| 175 | + for _, cmd := range cmds { |
| 176 | + if strings.HasPrefix(cmd, line) { |
| 177 | + completions = append(completions, cmd[pos:]) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return line, completions, "" |
| 182 | +} |
0 commit comments