Implemented service edit command

This commit is contained in:
Philipp Wolfer 2023-12-08 08:38:17 +01:00
parent c6c0723e27
commit 58a47a43e7
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
15 changed files with 213 additions and 57 deletions

View file

@ -17,6 +17,7 @@ package config
import (
"fmt"
"sort"
"github.com/spf13/cast"
"github.com/spf13/viper"
@ -44,6 +45,10 @@ func NewServiceConfig(name string, config *viper.Viper) ServiceConfig {
return service
}
func (c ServiceConfig) String() string {
return c.Name
}
func (c *ServiceConfig) GetString(key string) string {
return cast.ToString(c.ConfigValues[key])
}
@ -66,13 +71,27 @@ func (c *ServiceConfig) Save() error {
return viper.WriteConfig()
}
type ServiceList []ServiceConfig
func (l ServiceList) Len() int {
return len(l)
}
func (l ServiceList) Less(i, j int) bool {
return l[i].Name < l[j].Name
}
func (l ServiceList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
func AllServices() map[string]ServiceConfig {
services := make(map[string]ServiceConfig)
config := viper.Sub("service")
if config != nil {
for k, v := range config.AllSettings() {
s, ok := v.(*viper.Viper)
if ok {
for k := range config.AllSettings() {
s := config.Sub(k)
if s != nil {
services[k] = NewServiceConfig(k, s)
}
}
@ -80,6 +99,16 @@ func AllServices() map[string]ServiceConfig {
return services
}
func AllServicesAsList() ServiceList {
services := AllServices()
list := make(ServiceList, 0, len(services))
for _, s := range services {
list = append(list, s)
}
sort.Sort(list)
return list
}
func GetService(name string) (*ServiceConfig, error) {
key := "service." + name
config := viper.Sub(key)