clean up form setting code

master
Joshua Herring 1 month ago
parent 14bdd60969
commit b5260b5c37

@ -124,19 +124,8 @@ func execCommand(choice int, m model) tea.Cmd {
} }
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func run_command_form(m *model) *huh.Form {
switch m.state { return huh.NewForm(
case stateList:
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetSize(msg.Width, msg.Height)
case tea.KeyMsg:
if msg.String() == "enter" {
m.choice = m.list.Index()
m.state = stateForm
switch m.choice {
case 0:
m.form = huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewInput().Title("Remote Command").Key("remote_command").Value(&m.RemoteCommand), huh.NewInput().Title("Remote Command").Key("remote_command").Value(&m.RemoteCommand),
huh.NewInput().Title("Private Key File").Key("private_key_file").Value(&m.PrivateKeyFile), huh.NewInput().Title("Private Key File").Key("private_key_file").Value(&m.PrivateKeyFile),
@ -144,8 +133,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
huh.NewInput().Title("Host").Key("host").Value(&m.Host), huh.NewInput().Title("Host").Key("host").Value(&m.Host),
), ),
) )
case 1: }
m.form = huh.NewForm(
func transfer_file_form(m *model) *huh.Form {
return huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewInput().Title("Local Filepath").Key("local_filepath").Value(&m.LocalFilepath), huh.NewInput().Title("Local Filepath").Key("local_filepath").Value(&m.LocalFilepath),
huh.NewInput().Title("Destination Filepath").Key("destination_filepath").Value(&m.DestinationFilepath), huh.NewInput().Title("Destination Filepath").Key("destination_filepath").Value(&m.DestinationFilepath),
@ -154,8 +145,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
huh.NewInput().Title("Host").Key("host").Value(&m.Host), huh.NewInput().Title("Host").Key("host").Value(&m.Host),
), ),
) )
default: }
m.form = huh.NewForm(
func acme_record_form(m *model) *huh.Form {
return huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewInput().Title("Cloudflare API Token").Key("cloudflare_api_token").Value(&m.CloudflareAPIToken), huh.NewInput().Title("Cloudflare API Token").Key("cloudflare_api_token").Value(&m.CloudflareAPIToken),
huh.NewInput().Title("Zone ID").Key("zone_id").Value(&m.ZoneID), huh.NewInput().Title("Zone ID").Key("zone_id").Value(&m.ZoneID),
@ -164,6 +157,31 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
), ),
) )
} }
func (m *model) SetForm() {
switch m.choice {
case 0:
m.form = run_command_form(m)
case 1:
m.form = transfer_file_form(m)
case 2:
m.form = acme_record_form(m)
default:
m.form = acme_record_form(m)
}
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.state {
case stateList:
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetSize(msg.Width, msg.Height)
case tea.KeyMsg:
if msg.String() == "enter" {
m.choice = m.list.Index()
m.state = stateForm
m.SetForm()
return m, m.form.Init() return m, m.form.Init()
} }
} }

Loading…
Cancel
Save