Skip to content

Commit

Permalink
Resolve lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
khattori committed Oct 26, 2024
1 parent 730f095 commit 7fc196e
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 262 deletions.
170 changes: 90 additions & 80 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,113 @@
import * as rm from 'typed-rest-client/RestClient.js'

import Config from './config.js'


export namespace KeClient {
export type ObjectResponse = {
id: number
abspath: string
owner: string
display_name: string
description: string
type_object: string
parent_object: string
created: string
updated: string
fields: {
[key: string]: any
}
extra_properties: {
[key: string]: any
}
user_permissions: {
[key: string]: any
}
group_permissions: {
[key: string]: any
}
export type ObjectResponse = {
abspath: string
created: string
description: string
display_name: string
extra_properties: {
[key: string]: object
}
export type ObjectsResponse = {
count: number,
results: [ObjectResponse]
fields: {
[key: string]: object
}
group_permissions: {
[key: string]: object
}
id: number
owner: string
parent_object: string
type_object: string
updated: string
user_permissions: {
[key: string]: object
}
}

function request_options(token?: string): rm.IRequestOptions {
return {
'additionalHeaders': {
'Authorization': `Token ${token}`
}
export type ObjectsResponse = {
count: number,
results: [ObjectResponse]
}

function requestOpts(token?: string): rm.IRequestOptions {
return {
'additionalHeaders': {
'Authorization': `Token ${token}`
}
}
}

export async function get(config: Config, path: string): Promise<ObjectResponse | null> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.get<ObjectResponse>(path, request_options(config.token))
if (resp.statusCode == 404) {
return null
} else if (resp.statusCode != 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return resp.result
export async function get(config: Config, path: string): Promise<ObjectResponse | null> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectResponse> = await rest.get<ObjectResponse>(path, requestOpts(config.token))
if (resp.statusCode === 404) {
return null
}

export async function get_all(config: Config, path: string): Promise<ObjectsResponse | null> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(path, request_options(config.token))
if (resp.statusCode == 404) {
return null
} else if (resp.statusCode != 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return resp.result
if (resp.statusCode !== 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

export async function create(config: Config, path: string, data: any): Promise<ObjectResponse> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.create<ObjectResponse>(path, data, request_options(config.token))
if (resp.statusCode != 201) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return resp.result as ObjectResponse
return resp.result
}

export async function getAll(config: Config, path: string): Promise<ObjectsResponse | null> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(path, requestOpts(config.token))
if (resp.statusCode === 404) {
return null
}

export async function update(config: Config, path: string, data: any): Promise<ObjectResponse> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.update<ObjectResponse>(path, data, request_options(config.token))
if (resp.statusCode != 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return resp.result as ObjectResponse
if (resp.statusCode !== 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

export async function replace(config: Config, path: string, data: any): Promise<ObjectResponse> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.replace<ObjectResponse>(path, data, request_options(config.token))
if (resp.statusCode != 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return resp.result as ObjectResponse
return resp.result
}

export async function create(config: Config, path: string, data: object): Promise<ObjectResponse> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectResponse> = await rest.create<ObjectResponse>(path, data, requestOpts(config.token))
if (resp.statusCode !== 201) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

export async function del(config: Config, path: string, force: boolean = false): Promise<boolean> {
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
let resp: rm.IRestResponse<ObjectResponse> = await rest.del<ObjectResponse>(path, request_options(config.token))
if (force && resp.statusCode == 404) {
return false
} else if (resp.statusCode != 204) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}
return true
return resp.result as ObjectResponse
}

export async function update(config: Config, path: string, data: object): Promise<ObjectResponse> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectResponse> = await rest.update<ObjectResponse>(path, data, requestOpts(config.token))
if (resp.statusCode !== 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

return resp.result as ObjectResponse
}

export async function replace(config: Config, path: string, data: object): Promise<ObjectResponse> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectResponse> = await rest.replace<ObjectResponse>(path, data, requestOpts(config.token))
if (resp.statusCode !== 200) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

return resp.result as ObjectResponse
}

export default KeClient
export async function del(config: Config, path: string, force: boolean = false): Promise<boolean> {
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl)
const resp: rm.IRestResponse<ObjectResponse> = await rest.del<ObjectResponse>(path, requestOpts(config.token))
if (force && resp.statusCode === 404) {
return false
}

if (resp.statusCode !== 204) {
throw new Error(`StatusCode: ${resp.statusCode}`)
}

return true
}
66 changes: 40 additions & 26 deletions src/commands/mkdir.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {Args, Command, Flags} from '@oclif/core'
import path from 'path'
import path from 'node:path'

import {common as common_flags} from '../flags.js'
import * as KeClient from '../client.js'
import {checkCwd, checkInsecure} from '../common.js'
import Config from '../config.js'
import KeClient from '../client.js'
import {check_cwd, check_insecure} from '../common.js'
import {DIRECTORY_TYPE} from '../const.js'
import {common as commonFlags} from '../flags.js'


type MkdirOptions = {
parent?: boolean
verbose?: boolean
}


export default class Mkdir extends Command {
Expand All @@ -15,7 +21,6 @@ export default class Mkdir extends Command {
required: true,
}),
}
static strict = false

static override description = 'Create the directories, if they do not already exist.'

Expand All @@ -24,44 +29,53 @@ export default class Mkdir extends Command {
]

static override flags = {
...common_flags,
...commonFlags,
// flag with a value (-c, --cwd=VALUE)
cwd: Flags.string({char: 'c', description: 'set current working directory to VALUE'}),
parent: Flags.boolean({char: 'p', description: 'no error if existing, make parent directories as needed'}),
verbose: Flags.boolean({char: 'v', description: 'print a message for each created directory'}),
}

static strict = false

public async run(): Promise<void> {
const {argv, flags} = await this.parse(Mkdir)
const config = new Config(flags)
check_insecure(flags.insecure)
let cwd = await check_cwd(config, flags.cwd)
for (let i = 0; i < argv.length; i++) {
const {dir, base} = path.parse(path.resolve(cwd, argv[i] as string))
await this.make_directory(config, dir, base, flags.parent, flags.verbose)
}
checkInsecure(flags.insecure)
const cwd = await checkCwd(config, flags.cwd)
await Promise.all(
argv.map((arg) => {
const {base, dir} = path.parse(path.resolve(cwd, arg as string))
return this.makeDir(config, dir, base, flags)
})
)
}
private async make_directory(config: Config, parent_dir: string, name: string, parent: boolean = false, verbose: boolean = false) {
// parent_dir の存在チェック
let result = await KeClient.get(config, parent_dir)
if (result == null) {
if (!parent) {
throw new Error(`cannot create directory: parent directory '${parent_dir}' is not found`)

private async makeDir(config: Config, parentDir: string, name: string, options: MkdirOptions) {
// parentDir の存在チェック
let result = await KeClient.get(config, parentDir)
if (result === null) {
if (!options.parent) {
throw new Error(`cannot create directory: parent directory '${parentDir}' is not found`)
}
const {dir, base} = path.parse(parent_dir)
result = await this.make_directory(config, dir, base, parent, verbose)

const {base, dir} = path.parse(parentDir)
result = await this.makeDir(config, dir, base, options)
}
if (result!.type_object != DIRECTORY_TYPE) {
throw new Error(`cannot create directory: parent directory '${parent_dir}' is not directory`)

if (result!.type_object !== DIRECTORY_TYPE) {
throw new Error(`cannot create directory: parent directory '${parentDir}' is not directory`)
}
let data = {

const data = {
name,
'type_object': DIRECTORY_TYPE,
'name': name
}
result = await KeClient.create(config, parent_dir, data)
if (result && verbose) {
result = await KeClient.create(config, parentDir, data)
if (result && options.verbose) {
this.log(`created directory: ${result.abspath}`)
}

return result
}
}
Loading

0 comments on commit 7fc196e

Please sign in to comment.