Skip to content

Commit

Permalink
Change: rename exists to cmdExists and reimplement exec^Cession
Browse files Browse the repository at this point in the history
  • Loading branch information
NobleMajo committed May 15, 2024
1 parent 95035ca commit 3741b06
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 206 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Execute and assets](#execute-and-assets)
- [Sftp](#sftp)
- [AbstractPackageManager](#abstractpackagemanager)
- [Exec Session](#exec-session)
- [Technologies](#technologies)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -155,6 +156,17 @@ await apm.upgradeAll()
await apm.install("git")
```

## Exec Session
Sessions are available so that the PWD (process working directory) and environment do not have to be specified for every single command.
These sessions store that persistent settings across multiple executions and can even resolve relative paths.

```ts
const session = host.session("/etc/example")

session.exec("ls -al") // is executed at /etc/example
session.exec("./myApp") // is using MY_APP_ENV_VAR
```

# Technologies
HiveSsh is built using the following technologies:
- **TypeScript**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hivessh",
"version": "0.3.2",
"version": "0.3.3",
"description": "HiveSsh simplifies SSH2 connections via promise-based task execution on Linux servers with built-in server utilities and powerful command execution functions",
"main": "dist/index.js",
"type": "module",
Expand Down
120 changes: 120 additions & 0 deletions src/ExecSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as path from "path";
import { CmdChannelOptions, CmdExecOptions, SshChannel, SshChannelExit } from "./SshExec.js";
import { SshHost } from "./SshHost.js";

export interface ProcessEnv {
TZ?: string,
PWD: string,
[key: string]: string | undefined
}

export class ExecSession {
timeoutMillis?: number
sudo?: boolean

constructor(
public host: SshHost,
public env: ProcessEnv,
) { }

clone(): ExecSession {
const session = new ExecSession(
this.host,
{
...this.env
}
)
session.timeoutMillis = this.timeoutMillis
session.sudo = this.sudo
return session
}

cd(
newPath: string,
): void {
if (!newPath.startsWith("/")) {
newPath = path.normalize(this.env.PWD + "/" + newPath)
}

this.env.PWD = newPath
}

setEnv(
key: string,
value: string,
): void {
this.env[key] = value
}

unsetEnv(
key: string,
): void {
if (key.toUpperCase() == "PWD") {
throw new Error("Cant unset process working directory (PWD) in a ExecSession")
}

delete this.env[key]
}

parseCmdChannelOptions(
options?: CmdChannelOptions
): CmdChannelOptions {
options = {
sudo: this.sudo,
timeoutMillis: this.timeoutMillis,
...options,
pwd: undefined,
env: {
...this.env,
...options?.env,
},
}

return options
}

execChannel(
cmd: string,
options?: CmdChannelOptions
): Promise<SshChannel> {
options = this.parseCmdExecOptions(
options
)

return this.host.execChannel(
cmd,
options,
)
}

parseCmdExecOptions(
options?: CmdExecOptions
): CmdExecOptions {
options = {
sudo: this.sudo,
timeoutMillis: this.timeoutMillis,
...options,
pwd: undefined,
env: {
...this.env,
...options?.env,
},
}

return options
}

async exec(
cmd: string,
options?: CmdExecOptions
): Promise<SshChannelExit> {
options = this.parseCmdExecOptions(
options
)

return this.host.exec(
cmd,
options,
)
}
}
32 changes: 23 additions & 9 deletions src/SshHost.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ClientErrorExtensions, SFTPWrapper, Client as SshClient } from "ssh2"
import { ExecSession } from "./ExecSession.js"
import { handleHops } from "./HostHop.js"
import { CmdChannelOptions, CmdExecOptions, SshChannel, SshChannelExit, execSshChannel } from "./SshExec.js"
import { SshHostOptions, SshHostSettings, loadSettings } from "./SshHostOptions.js"
import { SessionOptions, SshSession } from "./SshSession.js"
import { AbstractPackageManager, getApm } from "./apm/PackageManager.js"
import { OsRelease, fetchOsRelease } from "./essentials/OsRelease.js"
import { SFTPPromiseWrapper, createSFTPPromiseWrapper } from "./essentials/SftpPromiseWrapper.js"
import { Awaitable } from "./utils/base.js"
import { Awaitable, trimAll } from "./utils/base.js"

export class SshHost {
closeErr?: Error | string
Expand Down Expand Up @@ -146,22 +146,36 @@ export class SshHost {
}
}

sesssion(
options?: SessionOptions,
): SshSession {
return new SshSession(
session(
pwd: string,
sudo?: boolean,
timeoutMillis?: number,
): ExecSession {
const session = new ExecSession(
this,
options,
{
PWD: pwd,
}
)

session.sudo = sudo
session.timeoutMillis = timeoutMillis

return session
}

async homeDir(): Promise<string> {
const result = await this.exec("pwd")
return trimAll(result.out)
}

async exists(
async cmdExists(
cmd: string
): Promise<boolean> {
this.throwCloseError()

if (cmd.includes(" ")) {
throw new Error("Command can not contain a space: '" + cmd + "'")
throw new Error("Command cant contain a space: '" + cmd + "'")
}

const exit = await this.exec("command -v " + cmd, {
Expand Down
Loading

0 comments on commit 3741b06

Please sign in to comment.