Skip to content

Commit

Permalink
Add: examples to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
NobleMajo committed May 14, 2024
1 parent b49fd6e commit 4f42406
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
58 changes: 51 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
- [Requirements](#requirements)
- [Getting started](#getting-started)
- [Promisified](#promisified)
- [Execute and assets](#execute-and-assets)
- [Sftp](#sftp)
- [AbstractPackageManager](#abstractpackagemanager)
- [Technologies](#technologies)
- [Contributing](#contributing)
Expand Down Expand Up @@ -73,26 +75,68 @@ const myHost = await SshHost.connect({
Here are some using examples:

## Promisified
After connecting an `SshHost`, you can leverage the promised execution (and other asset features) directly on the `SshHost` instance.
### Execute and assets

After connecting an `SshHost`, you can use the promisified execution (and other asset features) directly on the `SshHost` instance.
```ts
// check files in user home dir
const result = await myHost.exec("ls -al")
console.log("Result: ", result.out)
const homeDirFiles = await myHost.exec("ls -al")
console.log("Home dir files:\n", homeDirFiles.out)
```

Get the hosts public ip address:
```ts
// check if curl command exists
const curlExists = await myHost.exists("curl ifconfig.me")
if(!curlExists){
myHost.close()
throw new Error("Curl is not installed on: " + myHost.settings.id)
}

const myIp = await myHost.exec("curl ifconfig.me")
console.log("Host public ip: " + myIp.out)
//other sources: `api.ipify.org`, `ipinfo.io/ip` or `ipecho.net/plain`
```

// check if a command exists
You can also execute commands on absolut path:
```ts
const homeDirFiles = await myHost.exec(
"ls -al",
{ pwd: "/etc" }
)
console.log("Etc files: ", homeDirFiles.out)
```

Also a git example:
```ts
// check if git command exists
const gitExist = await myHost.exists("git")
console.log("Git exists: ", gitExist)
if(!curlExists){
myHost.close()
throw new Error("Git is not installed on: " + myHost.settings.id)
}

// get git status
const gitStatus = await myHost.exec(
"git status",
{
pwd: "/home/tester/myrepo"
}
)

console.log("Git status:\n", gitStatus.out)
```

You can also use the promised SFTP features via `SshHost.sftp`.
### Sftp
You can also use the promisified SFTP features via `SshHost.sftp`.
```ts
const myBinary: Buffer = await myHost.sftp.readFile("/home/tester/my-binary")

const exampleConfig: string = await myHost.sftp.readFile("/etc/example/config.yml", "utf8")
```

## AbstractPackageManager
With the abstract package manager (`apm`) you can use apt, dnf, yum or a custom implemented package manager via one interface.
With the abstract package manager (`apm`) you can use `apt`, `dnf`, `yum` or a `custom implemented package manager` via one interface.
The apm features are limited and general, but you can update your system and install, delete and list your packages.

```ts
Expand Down
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.2.1",
"version": "0.2.2",
"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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export * from "./utils/base.js"

import { SshHost } from "./SshHost.js"

export default SshHost
export default SshHost

0 comments on commit 4f42406

Please sign in to comment.