Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI init organization #912

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions templates/cli/lib/commands/init.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
questionsCreateCollection,
questionsInitProject,
questionsInitProjectAutopull,
questionsCreateOrganization,
questionsInitResources,
questionsCreateTeam
} = require("../questions");
Expand Down Expand Up @@ -119,6 +120,27 @@ const initProject = async ({ organizationId, projectId, projectName } = {}) => {
hint("Next you can use 'appwrite init' to create resources in your project, or use 'appwrite pull' and 'appwrite push' to synchronize your project.")
}

const initOrganization = async () => {
const answers = await inquirer.prompt(questionsCreateOrganization)

const sdk = await sdkForConsole();

localConfig.addTeam({
$id: answers.id === 'unique()' ? ID.unique() : answers.id,
name: answers.bucket,
});

await sdk.forConsole.billing.createOrganization(
organizationId: answers.id === 'unique()' ? ID.unique() : answers.id,
name: answers.name,
billingPlan: answers.plan,
paymentMethodId: ID.unique(),
billingAddressId: ID.unique(),
);
success();
log("Next you can create projects, teams, collections, functions, messaging, and buckets.");
};

const initBucket = async () => {
const answers = await inquirer.prompt(questionsCreateBucket)

Expand Down Expand Up @@ -338,6 +360,13 @@ init
.option("--projectName <projectName>", "{{ spec.title|caseUcfirst }} project ID")
.action(actionRunner(initProject));

init
.command("organization")
.alias("organizations")
.description("Init a new {{ spec.title|caseUcfirst }} organization")
.option("--organizationId <organizationId>", "{{ spec.title|caseUcfirst }} organization ID")
.action(actionRunner(initOrganization));

init
.command("function")
.alias("functions")
Expand Down
4 changes: 4 additions & 0 deletions templates/cli/lib/config.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ class Global extends Config {
this.setTo(Global.PREFERENCE_PROJECT, project);
}

setOrganization(organization) {
this.setTo(Global.PREFERENCE_ORGANIZATION, organization);
}

getKey() {
if (!this.hasFrom(Global.PREFERENCE_KEY)) {
return "";
Expand Down
70 changes: 70 additions & 0 deletions templates/cli/lib/questions.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,75 @@ const questionsInitProjectAutopull = [
`Would you like to pull all resources from project you just linked?`
},
];
const questionsInitOrganization = [
{
type: "list",
name: "start",
message: "How would you like to start?",
choices: [
{
name: "Create new organization",
value: "new"
},
{
name: "Link directory to an existing organization",
value: "existing"
}
]
},
{
type: "search-list",
name: "organization",
message: "Choose your organization",
choices: async () => {
let client = await sdkForConsole(true);
const { teams } = await paginate(teamsList, { parseOutput: false, sdk: client }, 100, 'teams');

let choices = teams.map((team, idx) => {
return {
name: `${team.name} (${team['$id']})`,
value: team['$id']
}
})

if (choices.length == 0) {
throw new Error(`No organizations found. Please create a new organization at ${globalConfig.getEndpoint().replace('/v1', '/console/onboarding')}`)
}

return choices;
},
when: (answer) => answer.start === 'existing'
},
{
type: "list",
name: "plan",
message: "What's the billing plan you want for your organization?",
choices: [
{
name: "Free plan",
value: "free"
},
{
name: "Pro plan",
value: "pro"
}
]
},
{
type: "input",
name: "name",
message: "What would you like to name your organization?",
default: "My Awesome Organization",
when: (answer) => answer.start !== 'existing'
},
{
type: "input",
name: "id",
message: "What ID would you like to have for your organization?",
default: "unique()",
when: (answer) => answer.start !== 'existing'
}
];
const questionsPullResources = [
{
type: "list",
Expand Down Expand Up @@ -843,6 +912,7 @@ const questionsRunFunctions = [
module.exports = {
questionsInitProject,
questionsInitProjectAutopull,
questionsInitOrganization,
questionsCreateFunction,
questionsCreateFunctionSelectTemplate,
questionsCreateBucket,
Expand Down