Skip to content

Commit

Permalink
Merge pull request #7 from seonggukchoi/release
Browse files Browse the repository at this point in the history
  • Loading branch information
seonggukchoi authored Sep 9, 2023
2 parents be62037 + 5094b7a commit 9682dbc
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permissions:

jobs:
release:
name: release
name: Release
runs-on: ubuntu-latest

permissions:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tk random -s -l 32 -c
Also, if you want to decode a Base64 string, you can input like this.

```bash
tk decode -b VGhpcyBpcyB2ZXJ5IGVhc3kh
tk encode -b VGhpcyBpcyB2ZXJ5IGVhc3kh -d

# --- Result ---
# This is very easy!
Expand Down
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Module } from '@nestjs/common';

import { DateCommand } from './date/date.command.js';
import { EncodeModule } from './encode/encode.module.js';
import { HashModule } from './hash/hash.module.js';
import { JwtModule } from './jwt/jwt.module.js';
import { OtpModule } from './otp/otp.module.js';
import { RandomModule } from './random/random.module.js';
import { SerializationModule } from './serialization/serialization.module.js';

@Module({ imports: [RandomModule, SerializationModule, JwtModule, HashModule, OtpModule, DateCommand] })
@Module({ imports: [RandomModule, EncodeModule, JwtModule, HashModule, OtpModule, DateCommand] })
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CopyableCommandOptions } from '../copyable/index.js';

export interface SerializationCommandOptions extends CopyableCommandOptions {
export interface EncodeCommandOptions extends CopyableCommandOptions {
decode?: boolean;
base64?: boolean;
hex?: boolean;
}
61 changes: 61 additions & 0 deletions src/encode/encode.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Command, Option } from 'nest-commander';

import { CopyableCommandRunner } from '../copyable/index.js';

import { EncodeCommandOptions } from './encode-command-options.interface.js';

@Command({ name: 'encode', description: 'Encode or decode the input.' })
export class EncodeCommand extends CopyableCommandRunner<EncodeCommandOptions> {
public override async run(passedParams: string[], options?: EncodeCommandOptions | undefined): Promise<void> {
if (!this.validateOptions(options)) {
return;
}

if (options!.decode) {
if (options!.base64) {
this.print(Buffer.from(passedParams.at(0) ?? '', 'base64').toString(), options!.copy);
}

if (options!.hex) {
this.print(Buffer.from(passedParams.at(0) ?? '', 'hex').toString(), options!.copy);
}
} else {
if (options!.base64) {
this.print(Buffer.from(passedParams.at(0) ?? '').toString('base64'), options!.copy);
}

if (options!.hex) {
this.print(Buffer.from(passedParams.at(0) ?? '').toString('hex'), options!.copy);
}
}
}

@Option({ flags: '-d, --decode', description: 'Decode the input.' })
private applyDecodeOption(): boolean {
return true;
}

@Option({ flags: '-b, --base64', description: 'Encode the input as base64.' })
private applyBase64Option(): boolean {
return true;
}

@Option({ flags: '-x, --hex', description: 'Encode the input as hex.' })
private applyHexOption(): boolean {
return true;
}

private validateOptions(options?: EncodeCommandOptions | undefined): boolean {
if (!options) {
return false;
}

if (options.base64 && options.hex) {
this.print('The options cannot be used together.');

return false;
}

return true;
}
}
6 changes: 6 additions & 0 deletions src/encode/encode.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Module } from '@nestjs/common';

import { EncodeCommand } from './encode.command.js';

@Module({ providers: [EncodeCommand] })
export class EncodeModule {}
31 changes: 0 additions & 31 deletions src/serialization/decode.command.ts

This file was deleted.

31 changes: 0 additions & 31 deletions src/serialization/encode.command.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/serialization/serialization.command.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/serialization/serialization.module.ts

This file was deleted.

0 comments on commit 9682dbc

Please sign in to comment.