Skip to content

Commit

Permalink
feat(cli): login and logout & update work dir (labring#587)
Browse files Browse the repository at this point in the history
* feat(cli): login and logout

* fix: update work dir
  • Loading branch information
skyoct authored Dec 27, 2022
1 parent caabd27 commit 792a0f3
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 11 deletions.
11 changes: 6 additions & 5 deletions cli/src/actions/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getHomeDir, writeYamlFile, ensureDirectory } from '../../utils/path';
import { getWorkDir, writeYamlFile, ensureDirectory } from '../../utils/path';
import * as path from 'node:path'
import * as fs from 'node:fs'
import { getApplicationByAppid, listApplication } from '../../apis/application';
Expand All @@ -8,7 +8,7 @@ import { APPLICATION_METADATA_FILE_NAME, FUNCTIONS_DIRECTORY_NAME } from '../../


export async function handleInitApplication(appid: string, options: { sync: boolean }) {
const applicationYamlPath = path.join(getHomeDir(), APPLICATION_METADATA_FILE_NAME)
const applicationYamlPath = path.join(getWorkDir(), APPLICATION_METADATA_FILE_NAME)
if (fs.existsSync(applicationYamlPath)) {
console.log('The application configuration file already exists in the current directory, unable to initialize the application')
return
Expand All @@ -19,11 +19,12 @@ export async function handleInitApplication(appid: string, options: { sync: bool
name: res.data.name,
regionName: res.data.regionName,
bundleName: res.data.bundleName,
runtimeName: res.data.runtimeName,
}
writeYamlFile(applicationYamlPath, applicationMetadata);

// init directory
ensureDirectory(path.join(getHomeDir(), FUNCTIONS_DIRECTORY_NAME))
ensureDirectory(path.join(getWorkDir(), FUNCTIONS_DIRECTORY_NAME))


// if sync is true, load remote data in local
Expand All @@ -35,11 +36,11 @@ export async function handleInitApplication(appid: string, options: { sync: bool

export async function handleListApplication() {
const table = new Table({
head: ['appid', 'name', 'region', 'bundle', 'runtime'],
head: ['appid', 'name', 'region', 'bundle', 'runtime', 'phase'],
})
const res = await listApplication();
for (let app of res.data) {
table.push([app.appid, app.name, app.regionName, app.bundleName, app.runtimeName])
table.push([app.appid, app.name, app.regionName, app.bundleName, app.runtimeName, app.phase])
}
console.log(table.toString());
}
26 changes: 26 additions & 0 deletions cli/src/actions/auth/auth.ts
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")
}
21 changes: 21 additions & 0 deletions cli/src/commands/auth/auth.ts
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
}
3 changes: 3 additions & 0 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from 'commander'
import { applicationCommand } from './commands/application/application'
import { loginCommand, logoutCommand } from './commands/auth/auth'


const program = new Command()
Expand All @@ -11,5 +12,7 @@ program
})

program.addCommand(applicationCommand())
program.addCommand(loginCommand())
program.addCommand(logoutCommand())

program.parse(process.argv)
8 changes: 8 additions & 0 deletions cli/src/templates/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export interface ConfigMetadata {

remoteServer: string;

accessToken: string;

}
6 changes: 5 additions & 1 deletion cli/src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

export const CONFIG_FILE_NAME = 'config.yaml';

export const APPLICATION_METADATA_FILE_NAME = 'laf.yaml';

export const FUNCTIONS_DIRECTORY_NAME = 'functions';
Expand All @@ -7,4 +9,6 @@ export const FUNCTIONS_METADATA_FILE_SUFFIX_NAME = '.meta.yaml';

export const COLLECTIONS_DIRECTORY_NAME = 'collections';

export const COLLECTIONS_METADATA_FILE_SUFFIX_NAME = '.meta.yaml';
export const COLLECTIONS_METADATA_FILE_SUFFIX_NAME = '.meta.yaml';

export const DEFAULT_REMOTE_SERVER = 'http://api.dev.laf.run';
21 changes: 19 additions & 2 deletions cli/src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
import { stringify, parse } from 'yaml'
import { ConfigMetadata } from '../templates/config';
import { CONFIG_FILE_NAME, DEFAULT_REMOTE_SERVER } from './constant';

export function getHomeDir() {
return "/Users/mac/Work/laf-cli-test/";
export function getWorkDir() {
return process.cwd()
}

export function getConfigDir() {
return path.join(process.env.HOME || process.env.USERPROFILE, '/.laf')
}

export function ensureDirectory(dir: string) {
Expand All @@ -13,6 +20,16 @@ export function ensureDirectory(dir: string) {
}
}

export function ensureSystemConfig() {
const configDir = getConfigDir();
ensureDirectory(configDir);
const configData: ConfigMetadata = {
remoteServer: DEFAULT_REMOTE_SERVER,
accessToken: null,
}
writeYamlFile(path.join(configDir, CONFIG_FILE_NAME), configData);
}

export function exist(fp: string): boolean {
return fs.existsSync(fp);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ request.interceptors.request.use(
Authorization: `Bearer ${token}`
};
} else {
console.error("please login first: `laf login -u username -p password`")
console.error("please login first: `laf login <token>`")
process.exit(1)
}
return config
Expand Down
19 changes: 17 additions & 2 deletions cli/src/utils/token.ts
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
}

0 comments on commit 792a0f3

Please sign in to comment.