Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tsci auth set-token $TOKEN, tsci auth print-token #39

Merged
merged 4 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cli/auth/print-token/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Command } from "commander"
import { cliConfig } from "lib/cli-config"

export const registerAuthPrintToken = (program: Command) => {
program.commands
.find((c) => c.name() === "auth")!
.command("print-token")
.description("Prints your auth token")
.action(() => {
const token = cliConfig.get("sessionToken")
if (!token) return console.log("You need to log in to access this.")
console.log("Your Token:\n", token)
})
}
25 changes: 25 additions & 0 deletions cli/auth/set-token/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Command } from "commander"
import { cliConfig } from "lib/cli-config"

function validateJWTLength(token: string) {
const parts = token.split(".")

if (parts.length === 3 && parts.every((part) => part.length > 0)) {
return true
} else {
return false
}
}
export const registerAuthSetToken = (program: Command) => {
program.commands
.find((c) => c.name() === "auth")!
.command("set-token")
.description("Explicitly set your auth token")
.argument("<token>", "New token to manually configure")
.action((token) => {
if (!validateJWTLength(token))
return console.log("Invalid token provided")
cliConfig.set("sessionToken", token)
console.log("Token manually updated.")
})
}
4 changes: 4 additions & 0 deletions cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { perfectCli } from "perfect-cli"
import pkg from "../package.json"
import semver from "semver"
import { registerExport } from "./export/register"
import { registerAuthPrintToken } from "./auth/print-token/register"
import { registerAuthSetToken } from "./auth/set-token/register"

const program = new Command()

Expand All @@ -30,6 +32,8 @@ registerClone(program)
registerAuth(program)
registerAuthLogin(program)
registerAuthLogout(program)
registerAuthPrintToken(program)
registerAuthSetToken(program)

registerConfig(program)
registerConfigPrint(program)
Expand Down
Loading