|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "runtime/trace" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/charmbracelet/lipgloss" |
| 14 | + "github.com/google/shlex" |
| 15 | + repl "github.com/openengineer/go-repl" |
| 16 | + |
| 17 | + "github.com/specterops/dawgs/tools/dawgrun/pkg/commands" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + //go:embed art.txt |
| 22 | + art string |
| 23 | + |
| 24 | + //go:embed banner.txt |
| 25 | + banner string |
| 26 | + |
| 27 | + bannerStyle = lipgloss.NewStyle(). |
| 28 | + Foreground(lipgloss.Color("#87dc70")). |
| 29 | + Background(lipgloss.Color("#533d25")) |
| 30 | +) |
| 31 | + |
| 32 | +func main() { |
| 33 | + fmt.Printf("\n%s\n%s", |
| 34 | + art, |
| 35 | + bannerStyle.Render(banner), |
| 36 | + ) |
| 37 | + fmt.Printf("\n\n\n") |
| 38 | + fmt.Printf(" ::: DAWGRUN REPL ::: Type 'help' for more info\n") |
| 39 | + |
| 40 | + userConfigDir, err := os.UserConfigDir() |
| 41 | + if err != nil { |
| 42 | + panic(fmt.Errorf("could not get user config dir: %w", err)) |
| 43 | + } |
| 44 | + |
| 45 | + // TODO: Someday, also load a config file from here for highlighting color scheme, colors enablement, etc |
| 46 | + appConfigBaseDir := path.Join(userConfigDir, "dawgrun") |
| 47 | + if err := os.MkdirAll(appConfigBaseDir, 0o750); err != nil { |
| 48 | + panic(fmt.Errorf("could not create dawgrun config dir: %w", err)) |
| 49 | + } |
| 50 | + |
| 51 | + handler := new(handler) |
| 52 | + handler.cmdScope = commands.NewScope() |
| 53 | + handler.r = repl.NewRepl(handler, &repl.Options{ |
| 54 | + HistoryFilePath: path.Join(appConfigBaseDir, "history.txt"), |
| 55 | + HistoryMaxLines: 10000, |
| 56 | + StatusWidgets: &repl.StatusWidgetFns{ |
| 57 | + Right: makeConnectionsStatusWidget(handler.cmdScope), |
| 58 | + }, |
| 59 | + }) |
| 60 | + |
| 61 | + if err := handler.r.Loop(); err != nil { |
| 62 | + slog.Error("repl encountered error: %w", slog.String("error", err.Error())) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +var _ repl.Handler = (*handler)(nil) |
| 67 | + |
| 68 | +type handler struct { |
| 69 | + r *repl.Repl |
| 70 | + cmdScope *commands.Scope |
| 71 | +} |
| 72 | + |
| 73 | +func (h *handler) Prompt() string { |
| 74 | + return "dawgrun > " |
| 75 | +} |
| 76 | + |
| 77 | +func (h *handler) Tab(buffer string) string { |
| 78 | + return "" |
| 79 | +} |
| 80 | + |
| 81 | +func (h *handler) Eval(line string) string { |
| 82 | + ctx, cancel := context.WithCancel(context.Background()) |
| 83 | + defer cancel() |
| 84 | + |
| 85 | + fields, err := shlex.Split(line) |
| 86 | + if err != nil { |
| 87 | + return fmt.Sprintf("Unparseable command: '%s': %s", line, err) |
| 88 | + } |
| 89 | + |
| 90 | + if len(fields) == 0 { |
| 91 | + return "Woof!" |
| 92 | + } |
| 93 | + |
| 94 | + command := strings.ToLower(fields[0]) |
| 95 | + rest := fields[1:] |
| 96 | + if cmd, ok := commands.Registry()[command]; ok { |
| 97 | + cmdCtx := commands.NewCommandContext(ctx, h.r, h.cmdScope) |
| 98 | + defer trace.StartRegion(cmdCtx, fmt.Sprintf("command-%s", command)).End() |
| 99 | + err := cmd.Fn(cmdCtx, rest) |
| 100 | + if err != nil { |
| 101 | + return fmt.Sprintf("%s failed: %v", command, err) |
| 102 | + } |
| 103 | + |
| 104 | + out := cmdCtx.OutputString() |
| 105 | + if !strings.HasSuffix(out, "\n") { |
| 106 | + return out + "\n" |
| 107 | + } else { |
| 108 | + return out |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return fmt.Sprintf("Unknown command %s; try `help`?", command) |
| 113 | +} |
| 114 | + |
| 115 | +func makeConnectionsStatusWidget(cmdScope *commands.Scope) repl.StatusWidgetFn { |
| 116 | + return func(r *repl.Repl) string { |
| 117 | + if numConns := cmdScope.NumConns(); numConns == 0 { |
| 118 | + return "No connections" |
| 119 | + } else { |
| 120 | + return fmt.Sprintf("%d connection(s)", numConns) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments