11import { Command , flags } from '@oclif/command'
2- import * as avsc from 'avsc'
3- import * as CryptoJS from 'crypto-js'
2+ import * as avro from 'avsc' // includes all from avro-js and some more
43
54import Logger from '../utilities/logger'
65import Utilities from '../utilities/utilities'
76
87export default class Avro extends Command {
98 static description = 'Avro Utility command'
9+ static SupportedCommands = [ 'get_schema' , 'to_json' , 'to_avro' ]
1010 static flags = {
1111 help : flags . help ( { char : 'h' } ) ,
1212 file : flags . string ( { char : 'f' , description : 'input file path' } ) ,
@@ -16,36 +16,58 @@ export default class Avro extends Command {
1616 }
1717
1818 static args = [ { name : 'command' } ] // operation type
19-
19+ /*
20+ * input,output, and operation are all must
21+ * */
2022 async run ( ) {
2123 const { args, flags} = this . parse ( Avro )
2224
23- args . string = Utilities . getInputString ( this , flags , args ) // from either -s,-f or args
2425 this . checkParameters ( flags , args )
2526 args . commandFunction = this . getCommandCaller ( args )
27+ args . commandFunction ( flags , args )
2628 }
2729
2830 // to check required parameters passed or not
2931 private checkParameters ( flags : any , args : any ) {
30- if ( args . string === undefined || args . string === '' )
31- Logger . error ( this , 'Input is empty or not provided' )
32+ if ( flags . file === undefined || flags . file === '' )
33+ Logger . error ( this , 'Input file is not provided' )
34+ if ( flags . output === undefined || args . output === '' )
35+ Logger . error ( this , 'Output file is not provided' )
3236 if ( args . command === undefined || args . command === '' )
33- Logger . error ( this , 'command is empty or not provided' )
37+ Logger . error ( this , 'Command is empty or not provided, supported:' + Avro . SupportedCommands )
3438 }
3539
3640 private getCommandCaller ( args : any ) {
37- let supportedCommands = [ 'get_schema' , 'to_json' , 'to_avro' ]
38-
3941 switch ( args . command . toLowerCase ( ) ) {
40- case supportedCommands [ 0 ] :
41- return 'supported'
42- case supportedCommands [ 1 ] :
43- return 'supported'
44- case supportedCommands [ 2 ] :
45- return 'supported'
42+ case Avro . SupportedCommands [ 0 ] :
43+ return this . getSchema
44+ case Avro . SupportedCommands [ 1 ] :
45+ return this . toJson
46+ case Avro . SupportedCommands [ 2 ] :
47+ return this . toAvro
4648 default :
47- Logger . error ( this , 'Unsupported Commands Mode , supported: ' + supportedCommands )
49+ Logger . error ( this , 'Unsupported Command , supported: ' + Avro . SupportedCommands )
4850 }
4951 }
5052
53+ private getSchema ( flags : any , args : any ) {
54+ avro . createFileDecoder ( flags . file )
55+ . on ( 'metadata' , function ( type ) {
56+ let output = type . schema ( )
57+ let schemaStr = JSON . stringify ( output , null , '\t' )
58+ Utilities . writeStringToFile ( this , flags . output , schemaStr )
59+ } )
60+
61+ }
62+ private toJson ( flags : any , args : any ) {
63+ avro . createFileDecoder ( flags . file )
64+ . on ( 'data' , function ( data ) {
65+ return data
66+ } )
67+
68+ }
69+ private toAvro ( flags : any , args : any ) {
70+
71+ }
72+
5173}
0 commit comments