feat: accept arguments through ssh command arguments
Some checks failed
Go Project Action / Spell-check and test go project (push) Failing after 54s
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:
@@ -55,6 +55,10 @@ func setupSshServer(host string, port string, host_key_path string, users map[st
|
||||
wish.WithMiddleware(
|
||||
func(next ssh.Handler) ssh.Handler {
|
||||
return func(s ssh.Session) {
|
||||
provided_args := s.Command()
|
||||
if len(provided_args) > 0 {
|
||||
args = provided_args
|
||||
}
|
||||
cmd := wish.Command(s, name, args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
wish.Fatalln(s, err)
|
||||
|
||||
36
main.go
36
main.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"yves-biener.de/wish-serve/internal/serve"
|
||||
@@ -15,12 +14,19 @@ func createDefaultConfig() {
|
||||
viper.SetDefault("key.dir", ".ssh")
|
||||
viper.SetDefault("key.name", "ssh_ed25519_key")
|
||||
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
|
||||
// `./config.yml`) if it exists. Otherwise use the default values from
|
||||
// `createDefaultConfig()`.
|
||||
// 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")
|
||||
@@ -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() {
|
||||
createDefaultConfig()
|
||||
loadConfig()
|
||||
overwriteServeCommand()
|
||||
|
||||
serve.Serve(
|
||||
viper.GetString("host"),
|
||||
|
||||
Reference in New Issue
Block a user