feat: accept arguments through ssh command arguments
Some checks failed
Go Project Action / Spell-check and test go project (push) Failing after 54s

Configured default arguments are replaced with provided arguments
through the `ssh` command. Without any arguments the default arguments
are used instead.
This commit is contained in:
2025-11-08 12:04:17 +01:00
parent 7dc1f9c53e
commit c6d270fc36
2 changed files with 15 additions and 25 deletions

View File

@@ -55,6 +55,10 @@ func setupSshServer(host string, port string, host_key_path string, users map[st
wish.WithMiddleware( wish.WithMiddleware(
func(next ssh.Handler) ssh.Handler { func(next ssh.Handler) ssh.Handler {
return func(s ssh.Session) { return func(s ssh.Session) {
provided_args := s.Command()
if len(provided_args) > 0 {
args = provided_args
}
cmd := wish.Command(s, name, args...) cmd := wish.Command(s, name, args...)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
wish.Fatalln(s, err) wish.Fatalln(s, err)

36
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/spf13/viper" "github.com/spf13/viper"
"yves-biener.de/wish-serve/internal/serve" "yves-biener.de/wish-serve/internal/serve"
@@ -15,12 +14,19 @@ func createDefaultConfig() {
viper.SetDefault("key.dir", ".ssh") viper.SetDefault("key.dir", ".ssh")
viper.SetDefault("key.name", "ssh_ed25519_key") viper.SetDefault("key.name", "ssh_ed25519_key")
viper.SetDefault("app.name", "echo") viper.SetDefault("app.name", "echo")
viper.SetDefault("app.args", []string{"Hello World"}) viper.SetDefault("app.args", []string{"Hello", "World"})
} }
// Load the configuration file from (`$HOME/.config/serve/config.yml` or // Load the configuration file from any of the following locations (loading the
// `./config.yml`) if it exists. Otherwise use the default values from // first one with an existing configuration):
// `createDefaultConfig()`. // - `$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() { func loadConfig() {
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yml") viper.SetConfigType("yml")
@@ -33,29 +39,9 @@ func loadConfig() {
} }
} }
// 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() { func main() {
createDefaultConfig() createDefaultConfig()
loadConfig() loadConfig()
overwriteServeCommand()
serve.Serve( serve.Serve(
viper.GetString("host"), viper.GetString("host"),