-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from lumaxis/feature/api-tokens
Add commands for API token management
- Loading branch information
Showing
87 changed files
with
8,327 additions
and
6,840 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Commands to manage API tokens in Mobile Center |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// token create command | ||
|
||
import { Command, CommandArgs, CommandResult, help, success, failure, ErrorCodes, shortName, longName, hasArg, required } from "../../util/commandline"; | ||
import { out } from "../../util/interaction"; | ||
import { reportToken } from "./lib/format-token"; | ||
import { MobileCenterClient, models, clientRequest } from "../../util/apis"; | ||
|
||
const debug = require("debug")("mobile-center-cli:commands:apps:create"); | ||
import { inspect } from "util"; | ||
|
||
@help("Create a new API token") | ||
export default class TokenCreateCommand extends Command { | ||
constructor(args: CommandArgs) { | ||
super(args); | ||
} | ||
|
||
@help("Description of the API token") | ||
@shortName("d") | ||
@longName("description") | ||
@hasArg | ||
description: string; | ||
|
||
async run(client: MobileCenterClient): Promise<CommandResult> { | ||
const tokenAttributes: models.ApiTokensCreateRequest = { | ||
description: this.description, | ||
}; | ||
|
||
const createTokenResponse = await out.progress("Creating token ...", | ||
clientRequest<models.ApiTokensCreateResponse>(cb => client.account.createApiToken(tokenAttributes, cb)) | ||
); | ||
|
||
const statusCode = createTokenResponse.response.statusCode; | ||
if (statusCode >= 400) { | ||
switch (statusCode) { | ||
case 400: | ||
default: | ||
return failure(ErrorCodes.Exception, "invalid request"); | ||
case 403: | ||
return failure(ErrorCodes.InvalidParameter, "authorization to create an API token failed"); | ||
case 404: | ||
return failure(ErrorCodes.NotLoggedIn, "user could not be found"); | ||
} | ||
} | ||
|
||
reportToken(createTokenResponse.result); | ||
|
||
return success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// tokens delete command | ||
|
||
import { Command, CommandArgs, CommandResult, help, success, failure, ErrorCodes, shortName, longName, hasArg, required } from "../../util/commandline"; | ||
import { out, prompt } from "../../util/interaction"; | ||
import { MobileCenterClient, models, clientRequest } from "../../util/apis"; | ||
|
||
const debug = require("debug")("mobile-center-cli:commands:apps:create"); | ||
import { inspect } from "util"; | ||
|
||
@help("Delete an API token") | ||
export default class AppDeleteCommand extends Command { | ||
constructor(args: CommandArgs) { | ||
super(args); | ||
} | ||
|
||
@help("ID of the API token") | ||
@required | ||
@hasArg | ||
id: string; | ||
|
||
async run(client: MobileCenterClient): Promise<CommandResult> { | ||
const confirmation = await prompt.confirm(`Do you really want to delete the token with ID "${this.id}"`); | ||
|
||
if (confirmation) { | ||
const deleteTokenResponse = await out.progress("Deleting app ...", clientRequest<null>(cb => client.account.deleteApiToken(this.id, cb))); | ||
|
||
if (deleteTokenResponse.response.statusCode === 404) { | ||
return failure(ErrorCodes.InvalidParameter, `the token with ID "${this.id}" could not be found`); | ||
} | ||
} else { | ||
out.text(`Deletion of token with ID "${this.id}" canceled`); | ||
} | ||
|
||
return success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { out } from "../../../util/interaction"; | ||
import { models } from "../../../util/apis"; | ||
|
||
export function reportTokenInfo(token: models.ApiTokensGetResponse | models.ApiTokensCreateResponse): void { | ||
out.report( | ||
[ | ||
["ID", "id" ], | ||
[ "Description", "description"], | ||
[ "Created at", "createdAt"] | ||
], token); | ||
} | ||
|
||
export function reportToken(token: models.ApiTokensGetResponse | models.ApiTokensCreateResponse): void { | ||
out.report( | ||
[ | ||
["ID", "id" ], | ||
[ "API Token", "apiToken" ], | ||
[ "Description", "description"], | ||
[ "Created at", "createdAt"] | ||
], token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Command, CommandArgs, CommandResult, help, success } from "../../util/commandline"; | ||
import { out } from "../../util/interaction"; | ||
import { reportTokenInfo } from "./lib/format-token"; | ||
import { MobileCenterClient, models, clientRequest } from "../../util/apis"; | ||
|
||
const debug = require("debug")("mobile-center-cli:commands:apps:list"); | ||
import { inspect } from "util"; | ||
|
||
@help("Get a list of API tokens") | ||
export default class ApiTokenListCommand extends Command { | ||
constructor(args: CommandArgs) { | ||
super(args); | ||
} | ||
|
||
async run(client: MobileCenterClient): Promise<CommandResult> { | ||
const apiTokensResponse = await out.progress("Getting API tokens ...", | ||
clientRequest<models.ApiTokensGetResponse[]>(cb => client.account.getApiTokens(cb))); | ||
|
||
out.table({ head: ['ID', 'Description', 'Created At'], style: { head: [] } }, | ||
apiTokensResponse.result.map(apiToken => [apiToken.id, apiToken.description, apiToken.createdAt]) | ||
); | ||
|
||
return success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.