wip: more robust input parsing
continuous-integration/drone/push Build is passing Details

This commit is contained in:
decentral1se 2023-12-29 11:23:22 +01:00
parent bceecfa0ae
commit 253e33266b
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
6 changed files with 73 additions and 52 deletions

View File

@ -2,12 +2,13 @@ package main
import (
"bytes"
"d1.hackers.moe/cairde/ui"
"os"
"os/user"
"testing"
"time"
"d1.hackers.moe/cairde/ui"
openPrivacyLog "git.openprivacy.ca/openprivacy/log"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"

5
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/charmbracelet/x/exp/teatest v0.0.0-20230904163802-ca705a396e0f
github.com/muesli/reflow v0.3.0
github.com/mutecomm/go-sqlcipher/v4 v4.4.2
github.com/stretchr/testify v1.7.0
)
require (
@ -22,9 +23,11 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.1.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
@ -33,6 +36,7 @@ require (
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.14.0 // indirect
@ -41,4 +45,5 @@ require (
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

10
go.sum
View File

@ -26,6 +26,7 @@ github.com/charmbracelet/x/exp/teatest v0.0.0-20230904163802-ca705a396e0f h1:kI7
github.com/charmbracelet/x/exp/teatest v0.0.0-20230904163802-ca705a396e0f/go.mod h1:TckAxPtan3aJ5wbTgBkySpc50SZhXJRZ8PtYICnZJEw=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -35,6 +36,10 @@ github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c h1:gkfmnY4Rlt3VINCo4uKdpvngiibQyoENVj5Q88sxXhE=
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c/go.mod h1:tDPFhGdt3hJWqtKwx57i9baiB1Cj0yAg22VOPUqm5vY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
@ -61,11 +66,14 @@ github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/gomega v1.20.1 h1:PA/3qinGoukvymdIDV8pii6tiZgC8kbmJO6Z5+b002Q=
github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
@ -100,6 +108,8 @@ golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,6 +1,6 @@
.PHONY: format check clean build run loc test
DEFAULT: build
DEFAULT: run
format:
@gofmt -s -w ./...

View File

@ -6,6 +6,8 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
const unknownCmdHelp = "Unknown command? Run /help to see available commands"
const cmdHelp = `/acn on | Turn on the Tor ACN
/acn off | Turn off the Tor ACN
/clear | Clear the screen
@ -16,30 +18,6 @@ const cmdHelp = `/acn on | Turn on the Tor
/start | Show getting started guide
/quit | Quit the program`
func isHelpCmd(cmds []string) bool {
return cmds[0] == "help"
}
func isQuitCmd(cmds []string) bool {
return cmds[0] == "quit"
}
func isStartCmd(cmds []string) bool {
return cmds[0] == "start"
}
func isProfileCreateCmd(cmds []string) bool {
return cmds[0] == "profile" && cmds[1] == "create"
}
func isProfileUnlockCmd(cmds []string) bool {
return cmds[0] == "profile" && cmds[1] == "unlock"
}
func isProfileInfoCmd(cmds []string) bool {
return cmds[0] == "profile" && cmds[1] == "info"
}
func isAcnOnCmd(cmds []string) bool {
return cmds[0] == "acn" && cmds[1] == "on"
}
@ -56,41 +34,52 @@ func handleCommand(cmd, hiddenInput string) tea.Msg {
cmds := strings.Split(cmd, " ")
switch {
case isHelpCmd(cmds):
case cmds[0] == "help":
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
case isQuitCmd(cmds):
case cmds[0] == "quit":
return turnAcnOffMsg{quitProgram: true}
case isStartCmd(cmds):
case cmds[0] == "start":
return showGettingStartedGuideMsg{}
case isProfileCreateCmd(cmds):
if len(cmds) != 5 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
case cmds[0] == "profile":
if cmds[1] != "unlock" && cmds[1] != "info" && cmds[1] != "create" {
profileHelp := `Unknown "profile" command?
/profile create <name> <password> <password> | Create a new profile
/profile info | Show profile information
/profile unlock <password> | Unlock profile(s)`
return cmdMsg{output: strings.Split(profileHelp, "\n")}
}
passwords := strings.Split(strings.TrimSpace(hiddenInput), " ")
if passwords[0] != passwords[1] {
return cmdMsg{output: []string{"profile create: passwords do not match?"}}
}
switch {
case cmds[1] == "unlock":
if len(cmds) != 3 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
}
return createProfileMsg{
name: cmds[2],
password: passwords[1],
}
return profileUnlockMsg{
password: strings.TrimSpace(hiddenInput),
}
case isProfileUnlockCmd(cmds):
if len(cmds) != 3 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
}
case cmds[1] == "info":
return profileInfoMsg{}
return profileUnlockMsg{
password: strings.TrimSpace(hiddenInput),
}
case cmds[1] == "create":
if len(cmds) != 5 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
}
case isProfileInfoCmd(cmds):
return profileInfoMsg{}
passwords := strings.Split(strings.TrimSpace(hiddenInput), " ")
if passwords[0] != passwords[1] {
return cmdMsg{output: []string{"Profile create: passwords do not match?"}}
}
return createProfileMsg{
name: cmds[2],
password: passwords[1],
}
}
case isAcnOnCmd(cmds):
return turnAcnOnMsg{}
@ -100,10 +89,9 @@ func handleCommand(cmd, hiddenInput string) tea.Msg {
case isClearCmd(cmds):
return clearScreenMsg{}
default:
return cmdMsg{output: []string{"Unknown command"}}
}
return cmdMsg{output: []string{unknownCmdHelp}}
}
func hidePasswordInput(m *model) {

17
ui/input_test.go Normal file
View File

@ -0,0 +1,17 @@
package ui
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestHandleCommand(t *testing.T) {
msg := handleCommand("/foo", "")
switch msg := msg.(type) {
case cmdMsg:
assert.Equal(t, msg.output, []string{unknownCmdHelp})
default:
assert.IsType(t, msg, cmdMsg{})
}
}