From d39740a09abd7dc8a7ba092d946cfcebcb7e0c9f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 07:41:24 +0000 Subject: [PATCH 1/2] Edit ssh cli help text --- cmd/ssh/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0227616..0b22216 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -37,8 +37,8 @@ func main() { flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") - flag.StringVar(&host, "h", "127.0.0.1", "http host") - flag.UintVar(&port, "p", 7122, "http port") + flag.StringVar(&host, "h", "127.0.0.1", "socket host") + flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() sshKeyFile := flag.Arg(0) From 52fd1eced24fc499e5b9bf04151df7a78973f62f Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 6 Jan 2025 08:11:37 +0000 Subject: [PATCH 2/2] Enable env, config, db in ssh --- cmd/main.go | 3 ++- cmd/ssh/main.go | 28 +++++++++++++++++++++++----- internal/ssh/ssh.go | 35 +++++++++++++++-------------------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ac680fc..01a352c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,8 +35,10 @@ func main() { var sessionId string var database string var engineDebug bool + var resourceDir string var err error + flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") @@ -67,7 +69,6 @@ func main() { MenuSeparator: menuSeparator, } - resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(connData, resourceDir) rs, err := menuStorageService.GetResource(ctx) diff --git a/cmd/ssh/main.go b/cmd/ssh/main.go index 0b22216..58f2b0b 100644 --- a/cmd/ssh/main.go +++ b/cmd/ssh/main.go @@ -14,7 +14,10 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" + "git.grassecon.net/urdt/ussd/config" + "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/ssh" + "git.grassecon.net/urdt/ussd/internal/storage" ) var ( @@ -24,7 +27,14 @@ var ( scriptDir = path.Join("services", "registration") ) +func init() { + initializers.LoadEnvVariables() +} + func main() { + config.LoadConfig() + + var connStr string var dbDir string var resourceDir string var size uint @@ -32,17 +42,25 @@ func main() { var stateDebug bool var host string var port uint - flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.StringVar(&connStr, "c", "", "connection string") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") - flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output") - flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output") + flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", "127.0.0.1", "socket host") flag.UintVar(&port, "p", 7122, "socket port") flag.Parse() + if connStr != "" { + connStr = config.DbConn + } + connData, err := storage.ToConnData(config.DbConn) + if err != nil { + fmt.Fprintf(os.Stderr, "connstr err: %v", err) + os.Exit(1) + } + sshKeyFile := flag.Arg(0) - _, err := os.Stat(sshKeyFile) + _, err = os.Stat(sshKeyFile) if err != nil { fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err) os.Exit(1) @@ -93,7 +111,7 @@ func main() { Cfg: cfg, Debug: engineDebug, FlagFile: pfp, - DbDir: dbDir, + Conn: connData, ResourceDir: resourceDir, SrvKeyFile: sshKeyFile, Host: host, diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index 1831ebe..c3ec725 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -71,6 +71,20 @@ func(a *auther) Get(k []byte) (string, error) { return v, nil } +type SshRunner struct { + Ctx context.Context + Cfg engine.Config + FlagFile string + Conn storage.ConnData + ResourceDir string + Debug bool + SrvKeyFile string + Host string + Port uint + wg sync.WaitGroup + lst net.Listener +} + func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error { if ch == nil { return errors.New("nil channel") @@ -128,32 +142,13 @@ func(s *SshRunner) serve(ctx context.Context, sessionId string, ch ssh.NewChanne return nil } -type SshRunner struct { - Ctx context.Context - Cfg engine.Config - FlagFile string - DbDir string - ResourceDir string - Debug bool - SrvKeyFile string - Host string - Port uint - wg sync.WaitGroup - lst net.Listener -} - func(s *SshRunner) Stop() error { return s.lst.Close() } func(s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { ctx := s.Ctx - menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir) - - err := menuStorageService.EnsureDbDir() - if err != nil { - return nil, nil, err - } + menuStorageService := storage.NewMenuStorageService(s.Conn, s.ResourceDir) rs, err := menuStorageService.GetResource(ctx) if err != nil {