Files
wish-serve/main.go
Yves Biener c6d270fc36
Some checks failed
Go Project Action / Spell-check and test go project (push) Failing after 54s
feat: accept arguments through ssh command arguments
Configured default arguments are replaced with provided arguments
through the `ssh` command. Without any arguments the default arguments
are used instead.
2025-11-08 12:04:17 +01:00

55 lines
1.5 KiB
Go

package main
import (
"fmt"
"github.com/spf13/viper"
"yves-biener.de/wish-serve/internal/serve"
)
func createDefaultConfig() {
viper.SetDefault("host", "127.0.0.1")
viper.SetDefault("port", "8022")
viper.SetDefault("users", map[string]string{})
viper.SetDefault("key.dir", ".ssh")
viper.SetDefault("key.name", "ssh_ed25519_key")
viper.SetDefault("app.name", "echo")
viper.SetDefault("app.args", []string{"Hello", "World"})
}
// Load the configuration file from any of the following locations (loading the
// first one with an existing configuration):
// - `$HOME/.config/serve/config.yml`
// - `./config.yml`
//
// If no configuration is found in any of the defined locations the default
// values of `createDefaultConfig` will remain unchanged.
//
// Invalid configuration files will cause a fatal error causing the application
// to panic.
func loadConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yml")
viper.AddConfigPath("$HOME/.config/serve")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
}
func main() {
createDefaultConfig()
loadConfig()
serve.Serve(
viper.GetString("host"),
viper.GetString("port"),
fmt.Sprintf("%s/%s", viper.GetString("key.dir"), viper.GetString("key.name")),
viper.GetStringMapString("users"),
viper.GetString("app.name"),
viper.GetStringSlice("app.args"),
)
}