From 0820acb54154205b748bd678edf714840f4ca009 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 24 Sep 2023 02:45:02 +0700 Subject: all: call loadEnvFromPaths inside newSession Since loadEnvFromPaths always called after NewSession, and it is part of session initialization, we can move the call inside the NewSession to minimize duplicate code. --- awwan.go | 10 ---------- awwan_test.go | 4 ++-- session.go | 7 ++++++- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/awwan.go b/awwan.go index 5680fb1..1ba5853 100644 --- a/awwan.go +++ b/awwan.go @@ -207,11 +207,6 @@ func (aww *Awwan) Local(req *Request) (err error) { return fmt.Errorf("%s: %w", logp, err) } - err = ses.loadEnvFromPaths() - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } - if len(req.Content) == 0 { req.script, err = NewScript(ses, req.scriptPath) } else { @@ -276,11 +271,6 @@ func (aww *Awwan) Play(req *Request) (err error) { return fmt.Errorf("%s: %w", logp, err) } - err = ses.loadEnvFromPaths() - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } - if aww.sshConfig == nil { err = aww.loadSshConfig() if err != nil { diff --git a/awwan_test.go b/awwan_test.go index 6438ea2..7973325 100644 --- a/awwan_test.go +++ b/awwan_test.go @@ -265,12 +265,12 @@ func TestAwwanLocalPut_withEncryption(t *testing.T) { tdataOut: `local.aww:3:exp_file_content`, }, { desc: `WithEmptyPrivateKey`, - expError: `Local: loadEnvFromPaths: private key is missing or not loaded`, + expError: `Local: NewSession: loadEnvFromPaths: private key is missing or not loaded`, resetPrivateKey: true, }, { desc: `WithInvalidPassphrase`, passphrase: "invalid\r", - expError: `Local: loadEnvFromPaths: LoadPrivateKeyInteractive: x509: decryption password incorrect`, + expError: `Local: NewSession: loadEnvFromPaths: LoadPrivateKeyInteractive: x509: decryption password incorrect`, resetPrivateKey: true, }} diff --git a/session.go b/session.go index cff495a..9589bbe 100644 --- a/session.go +++ b/session.go @@ -49,7 +49,7 @@ type Session struct { // directory and the session directory. func NewSession(aww *Awwan, sessionDir string) (ses *Session, err error) { var ( - logp = "newSession" + logp = `NewSession` randomString string ) @@ -67,6 +67,11 @@ func NewSession(aww *Awwan, sessionDir string) (ses *Session, err error) { return nil, fmt.Errorf("%s: %w", logp, err) } + err = ses.loadEnvFromPaths() + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + randomString = string(ascii.Random([]byte(ascii.LettersNumber), 16)) ses.tmpDir = filepath.Join(defTmpDir, randomString) -- cgit v1.3