🎉 Initial Commit of next version RoadSign CLI

This commit is contained in:
2024-10-01 22:19:27 +08:00
parent 28d3a3fa06
commit 632d37caf5
12 changed files with 415 additions and 1 deletions

26
cli/src/cmd/list.ts Normal file
View File

@@ -0,0 +1,26 @@
import { Command, type Usage } from "clipanion"
import { RsConfig } from "../utils/config.ts"
import chalk from "chalk"
export class ListServerCommand extends Command {
static paths = [[`list`], [`ls`]]
static usage: Usage = {
category: `Networking`,
description: `List all connected RoadSign Sideload Services`,
details: `Listing all servers that already saved in RoadSign CLI configuration file`,
examples: [["List all", `list`]]
}
async execute() {
const config = await RsConfig.getInstance()
for (let idx = 0; idx < config.config.servers.length; idx++) {
const server = config.config.servers[idx]
this.context.stdout.write(`${idx + 1}. ${chalk.bold(server.label)} ${chalk.gray(`(${server.url})`)}\n`)
}
this.context.stdout.write("\n" + chalk.cyan(`Connected ${config.config.servers.length} server(s) in total.`) + "\n")
process.exit(0)
}
}

55
cli/src/cmd/login.ts Normal file
View File

@@ -0,0 +1,55 @@
import { Command, Option, type Usage } from "clipanion"
import { createAuthHeader } from "../utils/auth.ts"
import { RsConfig } from "../utils/config.ts"
import ora, { oraPromise } from "ora"
export class LoginCommand extends Command {
static paths = [[`login`]]
static usage: Usage = {
category: `Networking`,
description: `Login to RoadSign Sideload Service`,
details: `Login to RoadSign Server`,
examples: [["Login with credentials", `login <label> <host> <password>`]]
}
label = Option.String({ required: true })
host = Option.String({ required: true })
credentials = Option.String({ required: true })
async execute() {
const config = await RsConfig.getInstance()
const spinner = ora(`Connecting to ${this.host}...`).start()
if (!this.host.includes(":")) {
this.host += ":81"
}
if (!this.host.startsWith("http")) {
this.host = "http://" + this.host
}
try {
const pingRes = await fetch(`${this.host}/cgi/metadata`, {
headers: {
Authorization: createAuthHeader("RoadSign CLI", this.credentials)
}
})
if (pingRes.status !== 200) {
throw new Error(await pingRes.text())
} else {
const info = await pingRes.json()
spinner.succeed(`Connected to ${this.host}, remote version ${info["version"]}`)
config.config.servers.push({
label: this.label,
url: this.host,
credential: this.credentials
})
await oraPromise(config.writeConfig(), { text: "Saving changes..." })
}
} catch (e) {
spinner.fail(`Unable connect to remote: ${e}`)
}
process.exit(0)
}
}

31
cli/src/cmd/logout.ts Normal file
View File

@@ -0,0 +1,31 @@
import { Command, Option, type Usage } from "clipanion"
import { RsConfig } from "../utils/config.ts"
import { oraPromise } from "ora"
import chalk from "chalk"
export class LogoutCommand extends Command {
static paths = [[`logout`]]
static usage: Usage = {
category: `Networking`,
description: `Logout from RoadSign Sideload Service`,
details: `Logout from RoadSign Server`,
examples: [["Logout with server label", `logout <label>`]]
}
label = Option.String({ required: true })
async execute() {
const config = await RsConfig.getInstance()
const server = config.config.servers.findIndex(item => item.label === this.label)
if (server === -1) {
this.context.stdout.write(chalk.red(`Server with label ${chalk.bold(this.label)} was not found.\n`))
} else {
config.config.servers.splice(server, 1)
this.context.stdout.write(chalk.green(`Server with label ${chalk.bold(this.label)} was successfully removed.\n`))
await oraPromise(config.writeConfig(), { text: "Saving changes..." })
}
process.exit(0)
}
}

4
cli/src/utils/auth.ts Normal file
View File

@@ -0,0 +1,4 @@
export function createAuthHeader(username: string, password: string) {
const credentials = Buffer.from(`${username}:${password}`).toString("base64")
return `Basic ${credentials}`
}

51
cli/src/utils/config.ts Normal file
View File

@@ -0,0 +1,51 @@
import * as os from "node:os"
import * as path from "node:path"
import * as fs from "node:fs/promises"
interface RsConfigData {
servers: RsConfigServerData[]
}
interface RsConfigServerData {
label: string
url: string
credential: string
}
class RsConfig {
private static instance: RsConfig
public config: RsConfigData = {
servers: []
}
private constructor() {
}
public static async getInstance(): Promise<RsConfig> {
if (!RsConfig.instance) {
RsConfig.instance = new RsConfig()
await RsConfig.instance.readConfig()
}
return RsConfig.instance
}
public async readConfig() {
const basepath = os.homedir()
const filepath = path.join(basepath, ".roadsignrc")
if (!await fs.exists(filepath)) {
await fs.writeFile(filepath, JSON.stringify(this.config))
}
const data = await fs.readFile(filepath, "utf8")
this.config = JSON.parse(data)
}
public async writeConfig() {
const basepath = os.homedir()
const filepath = path.join(basepath, ".roadsignrc")
await fs.writeFile(filepath, JSON.stringify(this.config))
}
}
export { RsConfig, type RsConfigData, type RsConfigServerData }