Files
wish-serve/main.go
Yves Biener 1b12a22fc5
All checks were successful
Go Project Action / Spell-check and test go project (push) Successful in 34s
Release Go Application / Release go project (release) Successful in 30s
mod: add configuration for key.dir and key.name
2024-10-07 16:47:02 +02:00

69 lines
1.7 KiB
Go

package main
import (
"fmt"
"os"
"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 (`$HOME/.config/serve/config.yml` or
// `./config.yml`) if it exists. Otherwise use the default values from
// `createDefaultConfig()`.
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))
}
}
}
// Overwrite (default) configuration value for the application to serve with
// given command line arguments.
//
// NOTE: This will also overwrite the configuration for the `app` entry from the
// configuration file.
func overwriteServeCommand() {
if len(os.Args) >= 2 {
name := os.Args[1]
viper.Set("app.name", name)
if len(os.Args) >= 3 {
args := os.Args[2:]
viper.Set("app.args", args)
} else {
// no arguments provided -> clear app arguments
viper.Set("app.args", []string{})
}
}
}
func main() {
createDefaultConfig()
loadConfig()
overwriteServeCommand()
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"),
)
}