0.0.0 • Published 1 year ago

poplarml-cli v0.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Poplar Cli

Poplar Cli is a command line interface for PoplarML. It is a platform for deploying PoplarML models.

Getting Started

run npm install to install all dependencies.

run npm run build to build the project.

run npm link to link the project to your global node_modules.

run poplar to run the cli.

Usage

poplar

Run the cli.

Example: poplar deploy -i A10 -m Whisper

Deploy a PoplarML model to the A10. The model id is Whisper. This is a mock command which needs to be replaced with the actual command.


Login Class Docs

Config

  static description = 'Log in to PoplarML';

  static aliases: string[] = ['login'];

  static examples: string[] = [`$ poplar login`]

Functions

baseApiEndpoint()

  • This function help provide the appropriate prefix api link when connecting to the backend.
  • This is a public function and can be used where login class is called.
  public baseApiEndpoint(): string {
    const env = process.env.NODE_ENV
    if (env === 'production') {
      return 'prod'
    }

    return 'dev.'
  }

initConfigDir()

  • This function checks if config files exist and will initialize the file if it does not exist.
  • The config file will be used to store user credential tokens.
  private async initConfigDir(): Promise<void> {
    const configDir = this.config.configDir
    const configFile = path.join(configDir, 'config.json')
    try {
      await fs.ensureDir(configDir)
    } catch {
      await fs.mkdirp(configDir)
    }

    try {
      await fs.access(configFile)
    } catch {
      const initialConfig = {
        // eslint-disable-next-line camelcase
        access_token: '',
        // eslint-disable-next-line camelcase
        refresh_token: '',
      }
      await fs.writeJson(configFile, initialConfig)
    }
  }

login()

  • This function handles the login logic.
  • User credentials are sent to the backend to authenticate. If the user is authenticated, the tokens are stored locally in the user's machine in the config file.
   private async login(email: string, password: string): Promise<{accessToken: string, refreshToken: string}> {
    const baseApiEndpoint = this.baseApiEndpoint()
    const loginAPI =
      baseApiEndpoint === 'prod' ?
        'https://auth.api.poplarml.com/sign-in' :
        `https://${baseApiEndpoint}auth.api.poplarml.com/sign-in`
    const data = (await axios.post(loginAPI, {
      email: email,
      password: password,
    })).data
    if (data.status >= 400) {
      this.error('Invalid Credentials')
    }

    const tokens = {
      accessToken: data.AuthenticationResult.AccessToken,
      refreshToken: data.AuthenticationResult.RefreshToken,
    }

    return tokens
  }

storeTokens()

  • Helper function that stores tokens locally in the user's machine.
  • The token is stored in the config file
  private async storeTokens({accessToken, refreshToken}:{accessToken: string, refreshToken: string}): Promise<void> {
    const configDir = this.config.configDir
    const configFile = path.join(configDir, 'config.json')
    const userConfig = await fs.readJSON(configFile)
    // eslint-disable-next-line camelcase
    userConfig.access_token = accessToken
    // eslint-disable-next-line camelcase
    userConfig.refresh_token = refreshToken
    await fs.writeJSON(configFile, userConfig)
  }

checkTokens()

  • This function checks if tokens are stored in the user's config file.
  • Used to check if stored tokens are valid and/or to re-authenticate the user.
  • This is a public function and can be used where login class is called.
  public async checkTokens(): Promise<boolean> {
    try {
      const configDir = this.config.configDir
      const configFile = path.join(configDir, 'config.json')
      const userConfig = await fs.readJSON(configFile)
      if (!userConfig) {
        return false
      }

      if (userConfig.refresh_token) {
        await this.getAccessToken(userConfig.refresh_token)
        return true
      }

      return false
    } catch (error:any)  {
      if (error.code === 'ENOENT') {
        this.error('Config directory does not exist. Please run `poplar init` first.')
      } else if (error instanceof SyntaxError) {
        console.error(error)
        this.error('Config file not a valid JSON')
      } else {
        console.error(error)
        this.error(error)
      }
    }
  }

getAccessToken()

  • Helper function that verifies uses the refresh token to refresh access token.
  • Can also be used to verify tokens.
  public async checkTokens(): Promise<boolean> {
    try {
      const configDir = this.config.configDir
      const configFile = path.join(configDir, 'config.json')
      const userConfig = await fs.readJSON(configFile)
      if (!userConfig) {
        return false
      }

      if (userConfig.refresh_token) {
        await this.getAccessToken(userConfig.refresh_token)
        return true
      }

      return false
    } catch (error:any)  {
      if (error.code === 'ENOENT') {
        this.error('Config directory does not exist. Please run `poplar init` first.')
      } else if (error instanceof SyntaxError) {
        console.error(error)
        this.error('Config file not a valid JSON')
      } else {
        console.error(error)
        this.error(error)
      }
    }
  }

Instructions to use public login functions

import {Command} from '@oclif/core'
import Login from 'path/to/login.ts'

export default class CommandNameHere extends Command{
    // Create login instance
  private login = new Login(this.argv, this.config)

  // Public functions now available
  this.login.checkToken() // return boolean  
  this.login.baseApiEndpoint() // return string ('prod', 'dev.')
}