From 9bb5a0858bbb1a6bde22e8c349507749fcfb9dd2 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 28 Nov 2019 20:08:50 +0100 Subject: [PATCH] Add variables to be added to the script --- go.mod | 1 + go.sum | 5 +++++ main.go | 33 ++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index c1e0672..481e8bf 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/Luzifer/shareport go 1.13 require ( + github.com/Luzifer/go_helpers/v2 v2.10.0 github.com/Luzifer/rconfig/v2 v2.2.1 github.com/pkg/errors v0.8.1 github.com/sirupsen/logrus v1.4.2 diff --git a/go.sum b/go.sum index 62b5e46..d8ce7c7 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,14 @@ +github.com/Luzifer/go_helpers v2.8.1+incompatible h1:9YvrAn7pU2viK5vRpAnI+0gyz+Tw8rxWHVIYHi642zk= +github.com/Luzifer/go_helpers/v2 v2.10.0 h1:rA3945P6tH1PKRdcVD+nAdAWojfgwX8wQm/jjUNPmfg= +github.com/Luzifer/go_helpers/v2 v2.10.0/go.mod h1:ZnWxPjyCdQ4rZP3kNiMSUW/7FigU1X9Rz8XopdJ5ZCU= github.com/Luzifer/rconfig v2.2.0+incompatible h1:Kle3+rshPM7LxciOheaR4EfHUzibkDDGws04sefQ5m8= github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg= github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b/go.mod h1:thNruaSwydMhkQ8dXzapABF9Sc1Tz08ZBcDdgott9RA= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/main.go b/main.go index 8204667..ea9175e 100644 --- a/main.go +++ b/main.go @@ -16,22 +16,24 @@ import ( log "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" + "github.com/Luzifer/go_helpers/v2/env" "github.com/Luzifer/rconfig/v2" ) var ( cfg = struct { - DebugRemote bool `flag:"debug-remote" default:"false" description:"Send remote stderr local terminal"` - IdentityFile string `flag:"identity-file,i" vardefault:"ssh_key" description:"Identity file to use for connecting to the remote"` - IdentityFilePassword string `flag:"identity-file-password" default:"" description:"Password for the identity file"` - LocalAddr string `flag:"local-addr,l" default:"" description:"Local address / port to forward" validate:"nonzero"` - LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` - RemoteHost string `flag:"remote-host" default:"" description:"Remote host and port in format host:port" validate:"nonzero"` - RemoteCommand string `flag:"remote-command" default:"" description:"Remote command to execute after connect"` - RemoteListen string `flag:"remote-listen" default:"localhost:0" description:"Address to listen on remote (port is available in script)"` - RemoteScript string `flag:"remote-script" default:"" description:"Bash script to push and execute (overwrites remote-command)"` - RemoteUser string `flag:"remote-user" vardefault:"remote_user" description:"User to use to connect to remote host"` - VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` + DebugRemote bool `flag:"debug-remote" default:"false" description:"Send remote stderr local terminal"` + IdentityFile string `flag:"identity-file,i" vardefault:"ssh_key" description:"Identity file to use for connecting to the remote"` + IdentityFilePassword string `flag:"identity-file-password" default:"" description:"Password for the identity file"` + LocalAddr string `flag:"local-addr,l" default:"" description:"Local address / port to forward" validate:"nonzero"` + LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` + RemoteHost string `flag:"remote-host" default:"" description:"Remote host and port in format host:port" validate:"nonzero"` + RemoteCommand string `flag:"remote-command" default:"" description:"Remote command to execute after connect"` + RemoteListen string `flag:"remote-listen" default:"localhost:0" description:"Address to listen on remote (port is available in script)"` + RemoteScript string `flag:"remote-script" default:"" description:"Bash script to push and execute (overwrites remote-command)"` + RemoteUser string `flag:"remote-user" vardefault:"remote_user" description:"User to use to connect to remote host"` + Vars []string `flag:"var,v" default:"" description:"Environment variables to pass to the script (Format VAR=value)"` + VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{} running = true @@ -167,10 +169,11 @@ func main() { } defer session.Close() - for k, v := range map[string]string{ - "PORT": port, - "LISTEN": remoteListener.Addr().String(), - } { + envVars := env.ListToMap(cfg.Vars) + envVars["PORT"] = port + envVars["LISTEN"] = remoteListener.Addr().String() + + for k, v := range envVars { fmt.Fprintf(scriptIn, "export %s=%q\n", k, v) }