Skip to content

Commit a93fd68

Browse files
committed
initial commit
0 parents  commit a93fd68

File tree

13 files changed

+271
-0
lines changed

13 files changed

+271
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**/.DS_Store
2+
*-debug.log
3+
*-error.log
4+
/.idea
5+
/.nyc_output
6+
/dist
7+
/lib
8+
/package-lock.json
9+
/tmp
10+
/yarn.lock
11+
node_modules
12+
oclif.manifest.json
13+
.editorconfig
14+
.eslintignore
15+
.eslintrc
16+
.mocharc.json
17+
.vscode/

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Example CLI
2+
=================
3+
4+
This repo contains the source code for the [Build a custom CLI](https://docs.airbotics.io/guides/cli) guide in the Airbotics documentation.
5+
6+
7+
## Prerequisites
8+
1. [Node.js](https://nodejs.org/) installed.
9+
2. An Airbotics account with:
10+
- At least 1 robot created.
11+
- At least 1 API key created.
12+
3. Set the `AIR_API_KEY` with a valid API key.
13+
14+
## Installation
15+
```
16+
npm i
17+
npm run build
18+
npm link
19+
export API_KEY="<your_api_key>"
20+
```
21+
22+
## Usage
23+
```
24+
acme [COMMAND]
25+
```
26+
27+
28+
## Implemented commands
29+
30+
```
31+
# Print a list of robots with:
32+
acme robots list
33+
34+
# Configure logs for a robot with:
35+
acme robots config-logs --enabled=true --robot_id=dev_bot_1
36+
37+
# Print a list of airbotics commands:
38+
acme commands list
39+
```
40+
41+
## Uninstall
42+
```
43+
npm unlink -g
44+
```
45+
46+

bin/dev

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
3+
const oclif = require('@oclif/core')
4+
5+
const path = require('path')
6+
const project = path.join(__dirname, '..', 'tsconfig.json')
7+
8+
// In dev mode -> use ts-node and dev plugins
9+
process.env.NODE_ENV = 'development'
10+
11+
require('ts-node').register({project})
12+
13+
// In dev mode, always show stack traces
14+
oclif.settings.debug = true;
15+
16+
// Start the CLI
17+
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)

bin/dev.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
3+
node "%~dp0\dev" %*

bin/run

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
const oclif = require('@oclif/core')
4+
5+
oclif.run().then(require('@oclif/core/flush')).catch(require('@oclif/core/handle'))

bin/run.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
3+
node "%~dp0\run" %*

package.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"name": "acme",
3+
"version": "0.0.0",
4+
"description": "oclif example Hello World CLI",
5+
"author": " @RobAirbotics",
6+
"bin": {
7+
"acme": "./bin/run"
8+
},
9+
"homepage": "https://github.com/RobAirbotics/acme",
10+
"license": "MIT",
11+
"main": "dist/index.js",
12+
"repository": "RobAirbotics/acme",
13+
"files": [
14+
"/bin",
15+
"/dist",
16+
"/npm-shrinkwrap.json",
17+
"/oclif.manifest.json"
18+
],
19+
"dependencies": {
20+
"@oclif/core": "^2",
21+
"@oclif/plugin-help": "^5",
22+
"@oclif/plugin-plugins": "^3.2.0",
23+
"axios": "^1.4.0"
24+
},
25+
"devDependencies": {
26+
"@oclif/test": "^2.4.4",
27+
"@types/chai": "^4",
28+
"@types/mocha": "^9.0.0",
29+
"@types/node": "^16.18.40",
30+
"chai": "^4",
31+
"eslint": "^7.32.0",
32+
"eslint-config-oclif": "^4",
33+
"eslint-config-oclif-typescript": "^1.0.3",
34+
"mocha": "^9",
35+
"oclif": "^3.11.3",
36+
"shx": "^0.3.3",
37+
"ts-node": "^10.9.1",
38+
"tslib": "^2.6.1",
39+
"typescript": "^4.9.5"
40+
},
41+
"oclif": {
42+
"bin": "acme",
43+
"dirname": "acme",
44+
"commands": "./dist/commands",
45+
"plugins": [
46+
"@oclif/plugin-help",
47+
"@oclif/plugin-plugins"
48+
],
49+
"topicSeparator": " ",
50+
"topics": {
51+
"robots": {
52+
"description": "Robot operations"
53+
},
54+
"list": {
55+
"description": "List resources"
56+
},
57+
"config": {
58+
"description": "Configure resources"
59+
}
60+
},
61+
"hooks": {
62+
"prerun": "./dist/hooks/prerun/key_check"
63+
}
64+
},
65+
"scripts": {
66+
"build": "shx rm -rf dist && tsc -b",
67+
"lint": "eslint . --ext .ts --config .eslintrc",
68+
"postpack": "shx rm -f oclif.manifest.json",
69+
"posttest": "npm run lint",
70+
"prepack": "npm run build && oclif manifest && oclif readme",
71+
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
72+
"version": "oclif readme && git add README.md"
73+
},
74+
"engines": {
75+
"node": ">=12.0.0"
76+
},
77+
"bugs": "https://github.com/RobAirbotics/acme/issues",
78+
"keywords": [
79+
"oclif"
80+
],
81+
"types": "dist/index.d.ts"
82+
}

src/commands/commands/list.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Args, Command, Flags } from '@oclif/core'
2+
import axios from 'axios';
3+
4+
export default class CommandsList extends Command {
5+
6+
static description = 'list your commands'
7+
8+
public async run(): Promise<void> {
9+
try {
10+
const apiKey = process.env.AIR_API_KEY;
11+
const authHeader = { 'air-api-key': apiKey};
12+
const response = await axios.get('https://api.airbotics.io/commands', { 'headers': authHeader })
13+
this.log(response.data);
14+
} catch (e) {
15+
this.log('unable to get a list of robots');
16+
this.log(e as string)
17+
this.exit(1);
18+
}
19+
}
20+
21+
}

src/commands/robots/config-logs.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Args, Command, Flags } from '@oclif/core';
2+
import axios from 'axios';
3+
4+
export default class ConfigLogs extends Command {
5+
6+
static description = 'configure logs on a robot';
7+
8+
static flags = {
9+
enabled: Flags.string({char: 'e', description: 'enable logs', required: true, options: ['true', 'false']}),
10+
robot_id: Flags.string({char: 'r', description: 'robot id', required: true})
11+
}
12+
13+
public async run(): Promise<void> {
14+
15+
const { flags} = await this.parse(ConfigLogs);
16+
17+
const enabled: boolean = flags.enabled === 'true' ? true: false;
18+
const robotId: string = flags.robot_id
19+
20+
try {
21+
const apiKey = process.env.AIR_API_KEY;
22+
const authHeader = { 'air-api-key': apiKey };
23+
const url = `https://api.airbotics.io/robots/${robotId}/logs/config`;
24+
const body = { enabled: enabled };
25+
const response = await axios.patch(url, body, { 'headers': authHeader });
26+
this.log(response.data);
27+
} catch (e) {
28+
this.log('unable to configure logs for robot');
29+
this.exit(1);
30+
}
31+
}
32+
33+
}

src/commands/robots/list.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Args, Command, Flags } from '@oclif/core'
2+
import axios from 'axios';
3+
4+
export default class RobotsList extends Command {
5+
6+
static description = 'list your robots'
7+
8+
public async run(): Promise<void> {
9+
try {
10+
const apiKey = process.env.AIR_API_KEY;
11+
const authHeader = { 'air-api-key': apiKey};
12+
const response = await axios.get('https://api.airbotics.io/robots', { 'headers': authHeader })
13+
this.log(response.data);
14+
} catch (e) {
15+
this.log('unable to get a list of robots');
16+
this.log(e as string)
17+
this.exit(1);
18+
}
19+
}
20+
21+
}

src/hooks/prerun/key_check.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {Hook} from '@oclif/core'
2+
3+
export const hook: Hook<'prerun'> = async function (opts) {
4+
if(process.env.AIR_API_KEY === undefined) {
5+
process.stderr.write('AIR_API_KEY was not found\n')
6+
process.exit(1);
7+
}
8+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {run} from '@oclif/core'

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"importHelpers": true,
5+
"module": "commonjs",
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"strict": true,
9+
"target": "es2019"
10+
},
11+
"include": [
12+
"src/**/*"
13+
]
14+
}

0 commit comments

Comments
 (0)