forked from labring/laf
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): login and logout & update work dir (labring#587)
* feat(cli): login and logout * fix: update work dir
- Loading branch information
Showing
9 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ensureDirectory, getConfigDir, writeYamlFile } from "../../utils/path"; | ||
import * as path from 'node:path' | ||
import * as fs from 'node:fs' | ||
import { CONFIG_FILE_NAME } from "../../utils/constant"; | ||
import { getConfigData } from "../../utils/token"; | ||
|
||
export async function handleLogin(token: string, options: { remote: string }) { | ||
let configData = getConfigData() | ||
// TODO check token | ||
configData.accessToken = token; | ||
if (options.remote !== '') { | ||
configData.remoteServer = options.remote | ||
} | ||
const configDir = getConfigDir() | ||
ensureDirectory(configDir) | ||
const configPath = path.join(configDir, CONFIG_FILE_NAME); | ||
writeYamlFile(configPath, configData); | ||
console.log("login success") | ||
} | ||
|
||
export async function handleLogout() { | ||
const configDir = getConfigDir(); | ||
const configPath = path.join(configDir, CONFIG_FILE_NAME); | ||
fs.rmSync(configPath); | ||
console.log("logout success") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { program, Command } from 'commander' | ||
import { handleLogin, handleLogout } from '../../actions/auth/auth' | ||
|
||
export function loginCommand(): Command { | ||
const login = program.command('login <token>') | ||
.description('login client') | ||
.option('-r, --remote [value]', 'remote server address', '') | ||
.action((token, options) => { | ||
handleLogin(token, options) | ||
}) | ||
return login | ||
} | ||
|
||
export function logoutCommand(): Command { | ||
const logout = program.command('logout') | ||
.description('logout client') | ||
.action(() => { | ||
handleLogout() | ||
}) | ||
return logout | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
export interface ConfigMetadata { | ||
|
||
remoteServer: string; | ||
|
||
accessToken: string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
import { ConfigMetadata } from "../templates/config" | ||
import { CONFIG_FILE_NAME, DEFAULT_REMOTE_SERVER } from "./constant"; | ||
import { getConfigDir, loadYamlFile } from "./path"; | ||
import * as path from 'node:path' | ||
import * as fs from 'node:fs' | ||
|
||
export function getConfigData(): ConfigMetadata { | ||
const configPath = path.join(getConfigDir(), CONFIG_FILE_NAME); | ||
if (!fs.existsSync(configPath)) { | ||
return { | ||
remoteServer: DEFAULT_REMOTE_SERVER, | ||
accessToken: null, | ||
} | ||
} | ||
return loadYamlFile(configPath); | ||
} | ||
|
||
export function getAccessToken(): string { | ||
return 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MzhkZDdhNTBlZTVjMGViNjk2NWU2NzIiLCJpYXQiOjE2NzAzMTE2ODksImV4cCI6MTY3MDkxNjQ4OX0.2UtpV0WC5kvtoPynvhXCSnrh9vadKQ7crLBMSxnI3A8' | ||
return getConfigData()?.accessToken | ||
} | ||
|
||
export function getRemoteServer(): string { | ||
return 'http://localhost:3000' | ||
return getConfigData()?.remoteServer | ||
} |