mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 05:37:05 +02:00
Implemented service edit command
This commit is contained in:
parent
c6c0723e27
commit
58a47a43e7
15 changed files with 213 additions and 57 deletions
88
internal/cli/services.go
Normal file
88
internal/cli/services.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
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 (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/manifoldco/promptui"
|
||||
"go.uploadedlobster.com/scotty/internal/backends"
|
||||
"go.uploadedlobster.com/scotty/internal/config"
|
||||
)
|
||||
|
||||
func SelectService() (config.ServiceConfig, error) {
|
||||
services := config.AllServicesAsList()
|
||||
if len(services) == 0 {
|
||||
err := errors.New("no existing service configurations")
|
||||
return config.ServiceConfig{}, err
|
||||
}
|
||||
sel := promptui.Select{
|
||||
Label: "Service",
|
||||
Items: services,
|
||||
Size: 10,
|
||||
}
|
||||
i, _, err := sel.Run()
|
||||
if err != nil {
|
||||
return config.ServiceConfig{}, err
|
||||
}
|
||||
return services[i], nil
|
||||
}
|
||||
|
||||
func SelectBackend(selected string) (string, error) {
|
||||
backendList := backends.GetBackends()
|
||||
i := slices.IndexFunc(backendList, func(b backends.BackendInfo) bool {
|
||||
return b.Name == selected
|
||||
})
|
||||
sel := promptui.Select{
|
||||
Label: "Backend",
|
||||
Items: backendList,
|
||||
CursorPos: i,
|
||||
Size: 10,
|
||||
}
|
||||
_, backend, err := sel.Run()
|
||||
return backend, err
|
||||
}
|
||||
|
||||
func PromptExtraOptions(config config.ServiceConfig) (config.ServiceConfig, error) {
|
||||
backend, err := backends.BackendByName(config.Backend)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
opts := backend.Options()
|
||||
if opts == nil {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
values := make(map[string]any, len(opts))
|
||||
for _, opt := range opts {
|
||||
// Use current value as default
|
||||
current, exists := config.ConfigValues[opt.Name]
|
||||
if exists {
|
||||
opt.Default = fmt.Sprintf("%v", current)
|
||||
}
|
||||
|
||||
val, err := Prompt(opt)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
values[opt.Name] = val
|
||||
}
|
||||
|
||||
config.ConfigValues = values
|
||||
return config, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue