diff --git a/packages/apollo-cli/README.md b/packages/apollo-cli/README.md index a879780a6..18e3ea991 100644 --- a/packages/apollo-cli/README.md +++ b/packages/apollo-cli/README.md @@ -17,7 +17,7 @@ $ npm install -g @apollo-annotation/cli $ apollo COMMAND running command... $ apollo (--version) -@apollo-annotation/cli/0.1.19 linux-x64 node-v18.20.3 +@apollo-annotation/cli/0.1.19 linux-x64 node-v20.17.0 $ apollo --help [COMMAND] USAGE $ apollo COMMAND @@ -41,6 +41,8 @@ USAGE - [`apollo feature`](../website/docs/cli//feature.md) - Commands to handle features - [`apollo help`](../website/docs/cli//help.md) - Display help for apollo. +- [`apollo jbrowse`](../website/docs/cli//jbrowse.md) - Commands to manage the + JBrowse configuration - [`apollo login`](../website/docs/cli//login.md) - Login to Apollo - [`apollo logout`](../website/docs/cli//logout.md) - Logout of Apollo - [`apollo refseq`](../website/docs/cli//refseq.md) - Commands to handle diff --git a/packages/apollo-cli/package.json b/packages/apollo-cli/package.json index f80f39389..54d9e1078 100644 --- a/packages/apollo-cli/package.json +++ b/packages/apollo-cli/package.json @@ -53,6 +53,9 @@ "feature": { "description": "Commands to handle features" }, + "jbrowse": { + "description": "Commands to manage the JBrowse configuration" + }, "refseq": { "description": "Commands to handle reference sequences" }, diff --git a/packages/apollo-cli/src/commands/jbrowse/get-config.ts b/packages/apollo-cli/src/commands/jbrowse/get-config.ts new file mode 100644 index 000000000..2fd4e70f1 --- /dev/null +++ b/packages/apollo-cli/src/commands/jbrowse/get-config.ts @@ -0,0 +1,36 @@ +import { BaseCommand } from '../../baseCommand.js' +import { wrapLines, queryApollo } from '../../utils.js' + +export default class GetConfig extends BaseCommand { + static summary = 'Get JBrowse configuration from Apollo' + static description = wrapLines( + 'Print to stdout the JBrowse configuration from Apollo in JSON format', + ) + + static examples = [ + { + description: 'Get JBrowse configuration:', + command: '<%= config.bin %> <%= command.id %> > config.json', + }, + ] + + public async run(): Promise { + const { flags } = await this.parse(GetConfig) + + const access: { address: string; accessToken: string } = + await this.getAccess(flags['config-file'], flags.profile) + + const response = await queryApollo( + access.address, + access.accessToken, + 'jbrowse/config.json', + ) + + if (!response.ok) { + throw new Error('Failed to fetch JBrowse configuration') + } + + const json = (await response.json()) as object + this.log(JSON.stringify(json, null, 2)) + } +} diff --git a/packages/apollo-cli/src/commands/jbrowse/set-config.ts b/packages/apollo-cli/src/commands/jbrowse/set-config.ts new file mode 100644 index 000000000..8539ee42c --- /dev/null +++ b/packages/apollo-cli/src/commands/jbrowse/set-config.ts @@ -0,0 +1,73 @@ +import * as fs from 'node:fs' +import { + type JBrowseConfig, + type SerializedImportJBrowseConfigChange, +} from '@apollo-annotation/shared' +import { Args } from '@oclif/core' +import { Agent, RequestInit, fetch } from 'undici' +import { ConfigError } from '../../ApolloConf.js' +import { BaseCommand } from '../../baseCommand.js' +import { + wrapLines, + localhostToAddress, + createFetchErrorMessage, +} from '../../utils.js' + +export default class SetConfig extends BaseCommand { + static summary = 'Set JBrowse configuration' + static description = wrapLines( + 'Set JBrowse configuration in Apollo collaboration server', + ) + + static examples = [ + { + description: 'Add JBrowse configuration:', + command: '<%= config.bin %> <%= command.id %> config.json', + }, + ] + + static args = { + inputFile: Args.string({ + description: 'JBrowse configuration file', + required: true, + }), + } + + async run(): Promise { + const { args, flags } = await this.parse(SetConfig) + + if (!fs.existsSync(args.inputFile)) { + this.error(`File ${args.inputFile} does not exist`) + } + + const access: { address: string; accessToken: string } = + await this.getAccess(flags['config-file'], flags.profile) + const filehandle = await fs.promises.open(args.inputFile) + const fileContent = await filehandle.readFile({ encoding: 'utf8' }) + await filehandle.close() + + const change: SerializedImportJBrowseConfigChange = { + typeName: 'ImportJBrowseConfigChange', + newJBrowseConfig: JSON.parse(fileContent) as JBrowseConfig, + } + + const auth: RequestInit = { + method: 'POST', + body: JSON.stringify(change), + headers: { + Authorization: `Bearer ${access.accessToken}`, + 'Content-Type': 'application/json', + }, + dispatcher: new Agent({ headersTimeout: 60 * 60 * 1000 }), + } + const url = new URL(localhostToAddress(`${access.address}/changes`)) + const response = await fetch(url, auth) + if (!response.ok) { + const errorMessage = await createFetchErrorMessage( + response, + 'Failed to add JBrowse configuration', + ) + throw new ConfigError(errorMessage) + } + } +} diff --git a/packages/website/docs/cli/jbrowse.md b/packages/website/docs/cli/jbrowse.md new file mode 100644 index 000000000..781b981b0 --- /dev/null +++ b/packages/website/docs/cli/jbrowse.md @@ -0,0 +1,61 @@ +# `apollo jbrowse` + +Commands to manage the JBrowse configuration + +- [`apollo jbrowse get-config`](#apollo-jbrowse-get-config) +- [`apollo jbrowse set-config INPUTFILE`](#apollo-jbrowse-set-config-inputfile) + +## `apollo jbrowse get-config` + +Get JBrowse configuration from Apollo + +``` +USAGE + $ apollo jbrowse get-config [--profile ] [--config-file ] + +FLAGS + --config-file= Use this config file (mostly for testing) + --profile= Use credentials from this profile + +DESCRIPTION + Get JBrowse configuration from Apollo + + Print to stdout the JBrowse configuration from Apollo in JSON format + +EXAMPLES + Get JBrowse configuration: + + $ apollo jbrowse get-config > config.json +``` + +_See code: +[src/commands/jbrowse/get-config.ts](https://github.com/GMOD/Apollo3/blob/v0.1.19/packages/apollo-cli/src/commands/jbrowse/get-config.ts)_ + +## `apollo jbrowse set-config INPUTFILE` + +Set JBrowse configuration + +``` +USAGE + $ apollo jbrowse set-config INPUTFILE [--profile ] [--config-file ] + +ARGUMENTS + INPUTFILE JBrowse configuration file + +FLAGS + --config-file= Use this config file (mostly for testing) + --profile= Use credentials from this profile + +DESCRIPTION + Set JBrowse configuration + + Set JBrowse configuration in Apollo collaboration server + +EXAMPLES + Add JBrowse configuration: + + $ apollo jbrowse set-config config.json +``` + +_See code: +[src/commands/jbrowse/set-config.ts](https://github.com/GMOD/Apollo3/blob/v0.1.19/packages/apollo-cli/src/commands/jbrowse/set-config.ts)_