mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-22 20:59:27 +02:00
Moved prompt helpers into cli package
This commit is contained in:
parent
c9fa21be73
commit
8743a9d81c
2 changed files with 70 additions and 43 deletions
45
cmd/add.go
45
cmd/add.go
|
@ -27,8 +27,8 @@ import (
|
||||||
"github.com/manifoldco/promptui"
|
"github.com/manifoldco/promptui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go.uploadedlobster.com/scotty/internal/backends"
|
"go.uploadedlobster.com/scotty/internal/backends"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/cli"
|
||||||
"go.uploadedlobster.com/scotty/internal/config"
|
"go.uploadedlobster.com/scotty/internal/config"
|
||||||
"go.uploadedlobster.com/scotty/internal/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// addCmd represents the add command
|
// addCmd represents the add command
|
||||||
|
@ -98,16 +98,7 @@ func extraOptions(config *config.ServiceConfig) error {
|
||||||
|
|
||||||
values := make(map[string]any, len(*opts))
|
values := make(map[string]any, len(*opts))
|
||||||
for _, opt := range *opts {
|
for _, opt := range *opts {
|
||||||
var val any
|
val, err := cli.Prompt(opt)
|
||||||
var err error
|
|
||||||
switch opt.Type {
|
|
||||||
case models.Bool:
|
|
||||||
val, err = promptBool(opt)
|
|
||||||
case models.Secret:
|
|
||||||
val, err = promptSecret(opt)
|
|
||||||
case models.String:
|
|
||||||
val, err = promptString(opt)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -118,35 +109,3 @@ func extraOptions(config *config.ServiceConfig) error {
|
||||||
config.ConfigValues = values
|
config.ConfigValues = values
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func promptString(opt models.BackendOption) (string, error) {
|
|
||||||
prompt := promptui.Prompt{
|
|
||||||
Label: opt.Label,
|
|
||||||
Validate: opt.Validate,
|
|
||||||
Default: opt.Default,
|
|
||||||
}
|
|
||||||
|
|
||||||
val, err := prompt.Run()
|
|
||||||
return val, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func promptSecret(opt models.BackendOption) (string, error) {
|
|
||||||
prompt := promptui.Prompt{
|
|
||||||
Label: opt.Label,
|
|
||||||
Validate: opt.Validate,
|
|
||||||
Default: opt.Default,
|
|
||||||
Mask: '*',
|
|
||||||
}
|
|
||||||
|
|
||||||
val, err := prompt.Run()
|
|
||||||
return val, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func promptBool(opt models.BackendOption) (bool, error) {
|
|
||||||
sel := promptui.Select{
|
|
||||||
Label: opt.Label,
|
|
||||||
Items: []string{"Yes", "No"},
|
|
||||||
}
|
|
||||||
_, val, err := sel.Run()
|
|
||||||
return val == "Yes", err
|
|
||||||
}
|
|
||||||
|
|
68
internal/cli/prompt.go
Normal file
68
internal/cli/prompt.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||||
|
|
||||||
|
Scotty is free software: you can redistribute it and/or modify it under the
|
||||||
|
terms of the GNU General Public License as published by the Free Software
|
||||||
|
Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with
|
||||||
|
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/manifoldco/promptui"
|
||||||
|
"go.uploadedlobster.com/scotty/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Prompt(opt models.BackendOption) (any, error) {
|
||||||
|
switch opt.Type {
|
||||||
|
case models.Bool:
|
||||||
|
return PromptBool(opt)
|
||||||
|
case models.Secret:
|
||||||
|
return PromptSecret(opt)
|
||||||
|
case models.String:
|
||||||
|
return PromptString(opt)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown prompt type %v", opt.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PromptString(opt models.BackendOption) (string, error) {
|
||||||
|
prompt := promptui.Prompt{
|
||||||
|
Label: opt.Label,
|
||||||
|
Validate: opt.Validate,
|
||||||
|
Default: opt.Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err := prompt.Run()
|
||||||
|
return val, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func PromptSecret(opt models.BackendOption) (string, error) {
|
||||||
|
prompt := promptui.Prompt{
|
||||||
|
Label: opt.Label,
|
||||||
|
Validate: opt.Validate,
|
||||||
|
Default: opt.Default,
|
||||||
|
Mask: '*',
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err := prompt.Run()
|
||||||
|
return val, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func PromptBool(opt models.BackendOption) (bool, error) {
|
||||||
|
sel := promptui.Select{
|
||||||
|
Label: opt.Label,
|
||||||
|
Items: []string{"Yes", "No"},
|
||||||
|
}
|
||||||
|
_, val, err := sel.Run()
|
||||||
|
return val == "Yes", err
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue