Skip to content

Commit

Permalink
Terminal User Interface (#67)
Browse files Browse the repository at this point in the history
* feat: terminal user interface

* feat: bare bones interactive tui

* feat: terminal user interface with bubbletea

* feat: inspect mode for table results

Co-authored-by: brittonhayes <brittonhayes@users.noreply.github.com>
  • Loading branch information
brittonhayes and brittonhayes committed Mar 21, 2022
1 parent e5dfe46 commit b7ed81a
Show file tree
Hide file tree
Showing 14 changed files with 542 additions and 6 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Expand Up @@ -6,7 +6,6 @@ linters:
- godot
- godox
- dupl
- exhaustive
- funlen
- gocritic
- goprintffuncname
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.44.2
rev: v1.45.0
hooks:
- id: golangci-lint
24 changes: 22 additions & 2 deletions go.mod
Expand Up @@ -5,33 +5,51 @@ go 1.17
require (
github.com/BurntSushi/toml v1.0.0
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/brittonhayes/glitter v0.2.0
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/evertras/bubble-table v0.8.4
github.com/gookit/color v1.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.26.1
github.com/spf13/afero v1.8.1
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/zricethezav/gitleaks/v8 v8.3.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gitleaks/go-gitdiff v0.7.4 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -40,7 +58,9 @@ require (
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
72 changes: 70 additions & 2 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions internal/commands/hunt.go
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/brittonhayes/pillager/pkg/format"
"github.com/brittonhayes/pillager/pkg/hunter"
"github.com/brittonhayes/pillager/pkg/rules"
"github.com/brittonhayes/pillager/pkg/tui/model"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)

Expand All @@ -21,6 +23,7 @@ var (
reporter string
templ string
workers int
interactive bool
)

// huntCmd represents the hunt command.
Expand Down Expand Up @@ -75,6 +78,10 @@ var huntCmd = &cobra.Command{
return err
}

if interactive {
return runInteractive(h)
}

results, err := h.Hunt()
if err != nil {
return err
Expand All @@ -88,8 +95,15 @@ var huntCmd = &cobra.Command{
},
}

func runInteractive(h *hunter.Hunter) error {
m := model.NewModel(h)
p := tea.NewProgram(m, tea.WithAltScreen())
return p.Start()
}

func init() {
rootCmd.AddCommand(huntCmd)
huntCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "run in interactive mode")
huntCmd.Flags().IntVarP(&workers, "workers", "w", runtime.NumCPU(), "number of concurrent workers")
huntCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable scanner verbose output")
huntCmd.Flags().StringVarP(&level, "log-level", "l", "error", "set logging level")
Expand Down
2 changes: 2 additions & 0 deletions pkg/format/format.go
Expand Up @@ -11,6 +11,8 @@ func StringToReporter(s string) Reporter {
switch strings.ToLower(s) {
case "json":
return JSON{}
case "raw":
return Raw{}
case "yaml":
return YAML{}
case "toml":
Expand Down
12 changes: 12 additions & 0 deletions pkg/format/report.go
Expand Up @@ -19,6 +19,18 @@ type Reporter interface {
type JSON struct{}

func (j JSON) Report(w io.Writer, findings []report.Finding) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", "\t")
if err := encoder.Encode(&findings); err != nil {
return err
}

return nil
}

type Raw struct{}

func (r Raw) Report(w io.Writer, findings []report.Finding) error {
encoder := json.NewEncoder(w)
if err := encoder.Encode(&findings); err != nil {
return err
Expand Down
59 changes: 59 additions & 0 deletions pkg/tui/model/keymap.go
@@ -0,0 +1,59 @@
package model

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)

type keymap struct {
Filter key.Binding
Inspect key.Binding
Start key.Binding
Quit key.Binding
Help key.Binding
}

func newKeyMap() keymap {
k := keymap{
Inspect: key.NewBinding(
key.WithKeys("i"),
key.WithHelp("i", "inspect"),
),
Filter: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "filter"),
),
Start: key.NewBinding(
key.WithKeys(tea.KeyEnter.String()),
key.WithHelp("enter", "scan"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q", tea.KeyEsc.String()),
key.WithHelp("ctrl+c/esc/q", "quit"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "help"),
),
}

// Assert complies with help interface
var _ help.KeyMap = k

return k
}

// FullHelp returns keybindings for the expanded help view. It's part of the
// key.Map interface.
func (k keymap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Help, k.Start, k.Inspect},
{k.Quit},
}
}

// ShortHelp returns keybindings for the short help view.
func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{k.Help, k.Start, k.Inspect, k.Quit}
}
118 changes: 118 additions & 0 deletions pkg/tui/model/model.go
@@ -0,0 +1,118 @@
package model

import (
"fmt"
"os"

"github.com/brittonhayes/pillager/pkg/hunter"
"github.com/brittonhayes/pillager/pkg/tui/style"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/evertras/bubble-table/table"
"github.com/zricethezav/gitleaks/v8/report"
"golang.org/x/term"
)

type model struct {
keymap keymap
loading loading
header header
body body
help help.Model
table table.Model

hunter *hunter.Hunter
results []report.Finding
err error

width int
height int
}

type loading struct {
active bool
spinner spinner.Model
}

type header struct {
title string
subtitle string
}

type selected struct {
visible bool
text string
}

type body struct {
toast string
selected selected
message string
}

type resultsMsg struct{ results []report.Finding }

type errMsg struct{ err error }

func (e errMsg) Error() string {
return fmt.Sprintf("🔥 Uh oh! Well that's not good. Looks like something went wrong and the application has exited: \n\n%s\n\n%s", style.Error.Render(e.Error()), "Press [q] to quit.")
}

func NewModel(hunt *hunter.Hunter) model {
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
width = 80
height = 24
}

// Create table model
t := newTable(width)
// Create keymap
k := newKeyMap()
// Create loading spinner.
s := newSpinner()
// Create help model
h := help.New()

m := model{
hunter: hunt,
header: header{
title: "Pillager",
subtitle: "Hunt inside the file system for valuable information",
},
table: t,
body: body{
message: "",
},
help: h,
loading: loading{
active: false,
spinner: s,
},
keymap: k,
width: width,
height: height,
}

return m
}

func (m model) Dimensions() (int, int) {
return m.width, m.height
}

func startScan(h *hunter.Hunter) tea.Cmd {
return func() tea.Msg {
h.Debug = false
h.Verbose = false
results, err := h.Hunt()
if err != nil {
// There was an error making our request. Wrap the error we received
// in a message and return it.
return errMsg{err}
}

return resultsMsg{results}
}
}
17 changes: 17 additions & 0 deletions pkg/tui/model/spinner.go
@@ -0,0 +1,17 @@
package model

import (
"time"

"github.com/brittonhayes/pillager/pkg/tui/style"
"github.com/charmbracelet/bubbles/spinner"
)

func newSpinner() spinner.Model {
// Create loading spinner.
s := spinner.NewModel()
s.Spinner = spinner.Dot
s.Style = style.Spinner
s.HideFor = 250 * time.Millisecond
return s
}

0 comments on commit b7ed81a

Please sign in to comment.