Support multiline cmds in YAML configuration

Add support for multiline `cmd` configurations allowing for nicer looking configuration YAML files.
This commit is contained in:
Benson Wong
2024-10-19 20:06:59 -07:00
parent 6cf0962807
commit be82d1a6a0
6 changed files with 185 additions and 18 deletions

View File

@@ -1,7 +1,9 @@
package proxy
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
@@ -14,6 +16,10 @@ type ModelConfig struct {
CheckEndpoint string `yaml:"checkEndpoint"`
}
func (m *ModelConfig) SanitizedCommand() ([]string, error) {
return SanitizeCommand(m.Cmd)
}
type Config struct {
Models map[string]ModelConfig `yaml:"models"`
HealthCheckTimeout int `yaml:"healthCheckTimeout"`
@@ -55,3 +61,19 @@ func LoadConfig(path string) (*Config, error) {
return &config, nil
}
func SanitizeCommand(cmdStr string) ([]string, error) {
// Remove trailing backslashes
cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ")
cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ")
// Split the command into arguments
args := strings.Fields(cmdStr)
// Ensure the command is not empty
if len(args) == 0 {
return nil, fmt.Errorf("empty command")
}
return args, nil
}