import StorageHelper from './StorageHelper' ; import StdOutUtil from './StdOutUtil' ; import Constants from './Constants' ; import { getErrorForMachineName , getErrorForDomain , getErrorForPassword } from './ValidationsHandler' ; import { IMachine } from '../models/storage/StoredObjects' ; import { IAppDef } from '../models/AppDef' ; import CliApiManager from '../api/CliApiManager' ; import { IOption , IParams } from '../commands/Command' ; export default class CliHelper { static instance : CliHelper ; static get ( ) { if ( ! CliHelper . instance ) { CliHelper . instance = new CliHelper ( ) ; } return CliHelper . instance ; } getAppsAsOptions ( apps ) { return [ { name : Constants . CANCEL_STRING , value : '' , short : '' } , ... apps . map ( app => ( { name : `${ app . appName }` , value : `${ app . appName }` , short : `${ app . appName }` } ) ) ] ; } getMachinesAsOptions ( ) { return [ { name : Constants . CANCEL_STRING , value : '' , short : '' } , ... StorageHelper . get ( ) . getMachines ( ) . map ( machine => ( { name : `${ StdOutUtil . getColoredMachine ( machine ) }` , value : `${ machine . name }` , short : `${ machine . name }` } ) ) ] ; } getApiMethodsAsOptions ( ) { return [ { name : Constants . CANCEL_STRING , value : '' , short : '' } , ... Constants . API_METHODS . map ( method => ( { name : `${ method }` , value : `${ method }` , short : `${ method }` } ) ) ] ; } getApiMethodsDescription ( ) { return Constants . API_METHODS . reduce ( ( acc , method ) => ( acc ? `${ acc }, ` : '' ) + `"${ method }"` , '' ) ; } async loginMachine ( machine , password ) { try { const tokenToIgnore = await CliApiManager . get ( machine ) . getAuthToken ( password ) ; StdOutUtil . printGreenMessage ( `Logged in successfully.` ) ; StdOutUtil . printMessage ( `Authorization token is now saved as ${ StdOutUtil . getColoredMachine ( machine ) }.\n` ) ; } catch ( error ) { const errorMessage = error . message ? error . message : error ; StdOutUtil . printError ( `Something bad happened: cannot save ${ StdOutUtil . getColoredMachine ( machine ) }.\n${ errorMessage }\n` ) ; } } logoutMachine ( machineName ) { const removedMachine = StorageHelper . get ( ) . removeMachine ( machineName ) ; StdOutUtil . printMessage ( `You are now logged out from ${ StdOutUtil . getColoredMachine ( removedMachine ) }.\n` ) ; } findDefaultCaptainName ( ) { let currentSuffix = 1 ; const machines = StorageHelper . get ( ) . getMachines ( ) . map ( machine => machine . name ) ; while ( machines . includes ( this . getCaptainFullName ( currentSuffix ) ) ) { currentSuffix ++ ; } return this . getCaptainFullName ( currentSuffix ) ; } getCaptainFullName ( suffix ) { return `captain-${ suffix < 10 ? '0' : '' }${ suffix }` ; } async ensureAuthentication ( url ? , password ? , machineName ? ) { if ( url ) { const machine = { baseUrl : url , name : '' , authToken : '' } ; if ( machineName ) { let err = getErrorForDomain ( url ) ; if ( err !== true ) { StdOutUtil . printWarning ( `\nCan't store store login credentials: ${ err || 'error!' }\n` ) ; } else { err = getErrorForMachineName ( machineName ) ; if ( err !== true ) { StdOutUtil . printWarning ( `\nCan't store store login credentials: ${ err || 'error!' }\n` ) ; } else { machine . name = machineName ; } } } if ( password ) { await CliApiManager . get ( machine ) . getAuthToken ( password ) ; } return machine ; } else if ( machineName ) { const machine = StorageHelper . get ( ) . findMachine ( machineName ) ; if ( ! machine ) { throw new Error ( `Can't find stored machine "${ machineName }"` ) ; } try { await CliApiManager . get ( machine ) . getAllApps ( ) ; } catch ( e ) { StdOutUtil . printWarning ( `Your auth token for ${ StdOutUtil . getColoredMachine ( machine ) } is not valid anymore, try to login again...` ) ; machine . authToken = '' ; if ( password ) { await CliApiManager . get ( machine ) . getAuthToken ( password ) ; } } return machine ; } throw new Error ( 'Too few arguments, no url or machine name' ) ; } getEnsureAuthenticationOption ( appToken , url ? , password ? , name ? , done ? ) { let machine ; return { name : 'ensureAuthenticationPlaceholder' , message : 'CapRover machine password' , type : 'password' , hide : true , when : async ( ) => { StdOutUtil . printMessage ( 'Ensuring authentication...' ) ; type typeOfValue = string | ( ( ) => string | undefined ) ; type typeOfReturn = string | undefined ; const getVal = ( value ? ) : typeOfReturn => { return value && value instanceof Function ? value ( ) : value ; } ; const urlExtracted = getVal ( url ) ; const passwordExtracted = getVal ( password ) ; const nameExtracted = getVal ( name ) ; if ( ! ! appToken ) { machine = { baseUrl : urlExtracted || '' , authToken : '' , appToken , name : nameExtracted || '' } ; return false ; } try { machine = await CliHelper . get ( ) . ensureAuthentication ( urlExtracted , passwordExtracted , nameExtracted ) ; return ! machine . authToken ; } catch ( e ) { StdOutUtil . printError ( `\nSomething bad happened during authentication to ${ urlExtracted ? StdOutUtil . getColoredMachineUrl ( urlExtracted ) : StdOutUtil . getColoredMachineName ( nameExtracted || '' ) }.\n${ e . message || e }` , true ) ; } return true ; } , validate : async ( passwordToValidate ) => { const err = getErrorForPassword ( passwordToValidate ) ; if ( err !== true ) { return err ; } try { await CliApiManager . get ( machine ) . getAuthToken ( passwordToValidate ) ; } catch ( e ) { StdOutUtil . printError ( `\nSomething bad happened during authentication to ${ StdOutUtil . getColoredMachineUrl ( machine . baseUrl ) }.\n${ e . message || e }` , true ) ; } return true ; } , preProcessParam : async ( ) => done && ( await done ( machine ) ) } ; } }