private setOutputObject()

in DevSkim-VSCode-Plugin/server/src/devskimCLI.ts [96:204]


    private setOutputObject()
    {
        let format : OutputFormats;
        let fileExt : string = "t";

        //First, lets figure out what sort of format everything is supposed to be
        //if they didn't specify a format with the -f/--format switch, see if they
        //specified a file to output to.  If so, hint from the file extension.  If it
        //doesn't have one, then lets just assume text, because ¯\_(ツ)_/¯
        if(this.options === undefined || this.options.format === undefined)
        {
            if(this.options !== undefined && this.options.output_file !== undefined )
            {
                fileExt = this.options.output_file.toLowerCase();
                if(fileExt.indexOf(".") > -1)
                {
                    fileExt = fileExt.substring(fileExt.lastIndexOf(".")+1);       
                }
            }   
                 
        }
        else //hey, they specified a format, lets go with that
        {
            fileExt = this.options.format.toLowerCase();
        }

        //the format specified with -f, or the file extension if missing
        //could be in all sorts of forms.  Instead of being pedantic and 
        //hard to use, lets see if it matches against a loose set of possibilities
        switch(fileExt)
        {
            case "s":
            case "sarif":
            case "sarif2.1":
            case "sarif21": format = OutputFormats.SARIF21;
                break;
            
            case "h":
            case "htm":
            case "html": format = OutputFormats.HTML;
                break;
            
            case "c":
            case "csv": format = OutputFormats.CSV;
                break;

            case "j":
            case "jsn":
            case "json": format = OutputFormats.JSON;
                break;
            default: format = OutputFormats.Text;
        }
    

        //now we know what format, lets create the correct object
        //Unfortunately the correct output object is the union of what command we
        //are executing and the specified format, so ugly nested switch statements
        switch(this.command)
        {
            case CLIcommands.Analyze:
            {
                switch(format)
                {
                    case OutputFormats.SARIF21: this.outputObject = new SARIF21ResultWriter();
                        break;                
                    case OutputFormats.HTML: this.outputObject = new HTMLResultWriter();
                        break;                
                    case OutputFormats.CSV: this.outputObject = new CSVResultWriter();
                        break;                
                    default: this.outputObject = new TextResultWriter;
                }
                this.outputObject.initialize(this.settings, this.workingDirectory );
                break;
            }   

            case CLIcommands.showSettings:
            {
                switch(format)
                {
                    case OutputFormats.JSON: this.outputObject = new JSONSettingsWriter();
                        break;
                    default: this.outputObject = new JSONSettingsWriter();
                }
                this.outputObject.initialize(this.settings);
                break;
            }
            
            default: throw new Error('Method not implemented.');
        }

        //now we need to determine where the actual output goes.  If -o isn't used
        //its going to the console.  Otherwise, it will either use a default file
        //name if they used -o but didn't pass a file name argument, or it will
        //go to the file name they specified
        if(this.options === undefined || this.options.output_file === undefined )
        {
            this.outputFilePath = "";
        }
        else if(this.options.output_file === true)
        {
            this.outputFilePath = this.outputObject.getDefaultFileName(); 
        }
        else
        {
            this.outputFilePath =  this.options.output_file;   
        }  

        this.outputObject.setOutputLocale(this.outputFilePath);  
    }