Skip to content

Commit

Permalink
feat(form): add oracle support to the form mini tui app
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Jun 2, 2024
1 parent 24b8c08 commit bbdb414
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
45 changes: 43 additions & 2 deletions pkg/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/danvergara/dblab/pkg/command"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/muesli/termenv"

"github.com/danvergara/dblab/pkg/command"
)

const (
Expand Down Expand Up @@ -39,6 +40,11 @@ type Model struct {
sslPasswordInput textinput.Model
sslRootcertInput textinput.Model

// oracle specific.
traceFileInput textinput.Model
sslVerifyInput textinput.Model
walletInput textinput.Model

// std data.
hostInput textinput.Model
portInput textinput.Model
Expand All @@ -51,6 +57,7 @@ type Model struct {
// ssl.
postgreSQLSSLModes []string
mySQLSSLModes []string
oracleSSLModes []string
sqliteSSLModes []string
sslMode string
}
Expand Down Expand Up @@ -148,6 +155,18 @@ func (m *Model) SSLRootcert() string {
return m.sslRootcertInput.Value()
}

func (m *Model) SSLVerify() string {
return m.sslVerifyInput.Value()
}

func (m *Model) TraceFile() string {
return m.traceFileInput.Value()
}

func (m *Model) Wallet() string {
return m.walletInput.Value()
}

// Limit returns the limit input value from the user.
func (m *Model) Limit() (uint, error) {
// if the user skipped the question, resort to default value
Expand Down Expand Up @@ -235,15 +254,28 @@ func initModel() Model {
sslRootCert.Placeholder = "The name of a file containing SSL certificate authority (CA) certificate(s)"
sslRootCert.CharLimit = 1000

sslVerify := textinput.NewModel()
sslVerify.Placeholder = "SSL Verify"
sslVerify.CharLimit = 200

traceFile := textinput.NewModel()
traceFile.Placeholder = "Trace file"
traceFile.CharLimit = 1000

wallet := textinput.NewModel()
wallet.Placeholder = "Path to wallet"
wallet.CharLimit = 1000

m := Model{
// the supported drivers by the client.
drivers: []string{"postgres", "mysql", "sqlite"},
drivers: []string{"postgres", "mysql", "sqlite", "oracle"},
// our default value.
driver: "postgres",

sslMode: "disable",
postgreSQLSSLModes: []string{"disable", "require", "verify-full", "verify-ca"},
mySQLSSLModes: []string{"true", "false", "skip-verify", "preferred"},
oracleSSLModes: []string{"enable", "disable"},
sqliteSSLModes: []string{},

hostInput: host,
Expand All @@ -257,6 +289,9 @@ func initModel() Model {
sslKeyInput: sslKey,
sslPasswordInput: sslPassword,
sslRootcertInput: sslRootCert,
sslVerifyInput: sslVerify,
traceFileInput: traceFile,
walletInput: wallet,
}

return m
Expand Down Expand Up @@ -286,6 +321,9 @@ func Run() (command.Options, error) {
SSLKey: m.SSLKey(),
SSLPassword: m.SSLPassword(),
SSLRootcert: m.SSLRootcert(),
SSLVerify: m.SSLVerify(),
TraceFile: m.TraceFile(),
Wallet: m.Wallet(),
Limit: limit,
}

Expand All @@ -311,6 +349,9 @@ func IsEmpty(opts command.Options) bool {
"SSLKey",
"SSLPassword",
"SSLRootcert",
"TraceFile",
"SSLVerify",
"Wallet",
),
)
}
3 changes: 2 additions & 1 deletion pkg/form/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package form_test
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/danvergara/dblab/pkg/command"
"github.com/danvergara/dblab/pkg/form"
"github.com/stretchr/testify/assert"
)

func TestIsEmpty(t *testing.T) {
Expand Down
49 changes: 41 additions & 8 deletions pkg/form/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package form
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"

"github.com/danvergara/dblab/pkg/drivers"
)

Expand Down Expand Up @@ -182,6 +183,10 @@ func updateSSLMode(msg tea.Msg, m *Model) (tea.Model, tea.Cmd) {
if m.cursor < len(m.postgreSQLSSLModes)-1 {
m.cursor++
}
case drivers.Oracle:
if m.cursor < len(m.oracleSSLModes)-1 {
m.cursor++
}
case drivers.MySQL:
if m.cursor < len(m.mySQLSSLModes)-1 {
m.cursor++
Expand All @@ -193,12 +198,14 @@ func updateSSLMode(msg tea.Msg, m *Model) (tea.Model, tea.Cmd) {
m.sslMode = m.postgreSQLSSLModes[m.cursor]
case drivers.MySQL:
m.sslMode = m.mySQLSSLModes[m.cursor]
case drivers.Oracle:
m.sslMode = m.oracleSSLModes[m.cursor]
}

m.steps = 3
m.cursor = 0

if m.driver != drivers.Postgres || m.sslMode == "disable" {
if m.driver != drivers.Postgres || m.driver == drivers.Oracle || m.sslMode == "disable" {
return m, tea.Quit
}

Expand Down Expand Up @@ -269,24 +276,40 @@ func updateSSLConn(msg tea.Msg, m *Model) (tea.Model, tea.Cmd) {
func sslConnInputs(m *Model) []textinput.Model {
var inputs []textinput.Model

if m.driver == drivers.Postgres {
switch m.driver {
case drivers.Postgres:
inputs = []textinput.Model{
m.sslCertInput,
m.sslKeyInput,
m.sslPasswordInput,
m.sslRootcertInput,
}
case drivers.Oracle:
inputs = []textinput.Model{
m.traceFileInput,
m.sslVerifyInput,
m.walletInput,
}
}

return inputs
}

func assignSSLConnInputValues(m *Model, inputs []textinput.Model) {
if m.driver == drivers.Postgres && len(inputs) == 4 {
m.sslCertInput = inputs[0]
m.sslKeyInput = inputs[1]
m.sslPasswordInput = inputs[2]
m.sslRootcertInput = inputs[3]
switch m.driver {
case drivers.Postgres:
if len(inputs) == 4 {
m.sslCertInput = inputs[0]
m.sslKeyInput = inputs[1]
m.sslPasswordInput = inputs[2]
m.sslRootcertInput = inputs[3]
}
case drivers.Oracle:
if len(inputs) == 3 {
m.traceFileInput = inputs[0]
m.sslVerifyInput = inputs[1]
m.walletInput = inputs[2]
}
}
}

Expand All @@ -296,7 +319,8 @@ func updateSSLConnInputs(msg tea.Msg, m *Model) (*Model, tea.Cmd) {
cmds []tea.Cmd
)

if m.driver == drivers.Postgres {
switch m.driver {
case drivers.Postgres:
m.sslCertInput, cmd = m.sslCertInput.Update(msg)
cmds = append(cmds, cmd)

Expand All @@ -308,6 +332,15 @@ func updateSSLConnInputs(msg tea.Msg, m *Model) (*Model, tea.Cmd) {

m.sslRootcertInput, cmd = m.sslRootcertInput.Update(msg)
cmds = append(cmds, cmd)
case drivers.Oracle:
m.traceFileInput, cmd = m.traceFileInput.Update(msg)
cmds = append(cmds, cmd)

m.sslVerifyInput, cmd = m.sslVerifyInput.Update(msg)
cmds = append(cmds, cmd)

m.walletInput, cmd = m.walletInput.Update(msg)
cmds = append(cmds, cmd)
}

return m, tea.Batch(cmds...)
Expand Down
22 changes: 16 additions & 6 deletions pkg/form/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func sslView(m *Model) string {
sslModes = m.postgreSQLSSLModes
case drivers.MySQL:
sslModes = m.mySQLSSLModes
case drivers.Oracle:
sslModes = m.oracleSSLModes
case drivers.SQLite:
sslModes = m.sqliteSSLModes
default:
Expand Down Expand Up @@ -100,13 +102,21 @@ func sslConnView(m *Model) string {

func sslConnViewInputs(m *Model) []string {
var inputs []string

if m.driver == drivers.Postgres && (m.sslMode == "require" || m.sslMode == "verify-full" || m.sslMode == "verify-ca") {
switch m.driver {
case drivers.Postgres:
if m.sslMode == "require" || m.sslMode == "verify-full" || m.sslMode == "verify-ca" {
inputs = []string{
m.sslCertInput.View(),
m.sslKeyInput.View(),
m.sslPasswordInput.View(),
m.sslRootcertInput.View(),
}
}
case drivers.Oracle:
inputs = []string{
m.sslCertInput.View(),
m.sslKeyInput.View(),
m.sslPasswordInput.View(),
m.sslRootcertInput.View(),
m.traceFileInput.View(),
m.sslVerifyInput.View(),
m.walletInput.View(),
}
}

Expand Down

0 comments on commit bbdb414

Please sign in to comment.