From 52870dda32879cc0d6c880cee9ebad7ca6da74d1 Mon Sep 17 00:00:00 2001 From: Jeremy Tregunna Date: Wed, 29 Aug 2018 11:06:19 -0600 Subject: [PATCH 1/5] wip create project with org --- src/dispatch/dispatch.cpp | 10 ++++++++-- src/env/env.cpp | 18 ++++++++++++++++++ src/env/env.h | 2 ++ src/project/project.cpp | 30 +++++++++++++++++++++++------- src/project/project.h | 6 +++--- src/request/request.cpp | 16 ++++++++++++++++ src/request/request.h | 2 ++ 7 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/dispatch/dispatch.cpp b/src/dispatch/dispatch.cpp index 7a1ae06..93e800e 100644 --- a/src/dispatch/dispatch.cpp +++ b/src/dispatch/dispatch.cpp @@ -48,7 +48,7 @@ struct FunctionCaller { }; struct command setup_cmd = {"setup", {}, false, true}; -struct command init_cmd = {"init", {"--template", "--master"}, true, true}; +struct command init_cmd = {"init", {"--org", "--template", "--master"}, true, true}; struct command auth_cmd = {"auth", {}, false, false}; struct command templates_cmd = {"templates", {"--update", "--search"}, true, true}; struct command env_cmd = {"env", {"--set"}, true, true}; @@ -122,6 +122,7 @@ void init(int argc, char **argv, int cmd_index) { map init_flags = find_flags(argc, argv, cmd_index); string name = "."; string tpl = "base"; + string org_name = ""; bool master = false; if(argc > 2) { @@ -130,6 +131,11 @@ void init(int argc, char **argv, int cmd_index) { } } + if(init_flags.find("--org")->second != -1) { + int idx = init_flags.find("--org")->second; + org_name = argv[idx + 1]; + } + if(init_flags.find("--template")->second != -1) { int tpl_index = init_flags.find("--template")->second; @@ -142,7 +148,7 @@ void init(int argc, char **argv, int cmd_index) { master = true; } - Project::init(name, tpl, master); + Project::init(name, org_name, tpl, master); } void auth() { diff --git a/src/env/env.cpp b/src/env/env.cpp index 779ebbc..cf25964 100644 --- a/src/env/env.cpp +++ b/src/env/env.cpp @@ -81,6 +81,24 @@ bool Env::load_credentials() { return false; } +bool Env::load_defaults() { + if(FileManager::file_exists(paths.defaults)) { + string defaults = FileManager::read(paths.credentials); + string err; + Json json = Json::parse(defaults, err); + + if(err.length() > 0) { + console::error("Unable to load or read defaults: " + err); + return false; + } + + string org_name = json["org_name"].string_value(); + this->set(METIS_DEFAULT_ORG, org_name); + return true; + } + return false; +} + void Env::write_credentials(Json object) { FileManager::create_path(0755, paths.home, paths.env); diff --git a/src/env/env.h b/src/env/env.h index 8109b96..c2cd0fd 100644 --- a/src/env/env.h +++ b/src/env/env.h @@ -12,6 +12,7 @@ const std::string METIS_API_TOKEN = "METIS_API_TOKEN"; const std::string METIS_AUTH_TOKEN = "METIS_OAUTH"; +const std::string METIS_DEFAULT_ORG = "METIS_DEFAULT_ORG"; const std::string METIS_CREDENTIALS = "credentials.json"; const std::string METIS_DEFAULTS = "defaults.json"; const std::string METIS_TEMPLATE_DIR = "templates"; @@ -37,6 +38,7 @@ class Env { void set(std::string key, std::string val); void setup(); bool load_credentials(); + bool load_defaults(); void write_credentials(json11::Json object); void write_default_org(std::string org_name); void verify_auth(); diff --git a/src/project/project.cpp b/src/project/project.cpp index b8ada42..a61abed 100644 --- a/src/project/project.cpp +++ b/src/project/project.cpp @@ -17,9 +17,16 @@ using namespace json11; FileManager::resolve_path(name) : \ FileManager::cwd() + "/" + name -void Project::init(string name, string tpl, bool master) { +void Project::init(string name, string org_name, string tpl, bool master) { string directory = project_directory(name); + Env::instance()->load_defaults(); + string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG); + if(org_name == "") { + org_name = default_org_name; + } + + // -- begin create project without org string project_name = directory.substr(directory.find_last_of("/")); if(exists(directory)) { @@ -30,10 +37,19 @@ void Project::init(string name, string tpl, bool master) { exit(EXIT_FAILURE); } - existing_init(name, master); + existing_init(name, org_name, master); } else { - template_init(name, tpl, master); + template_init(name, org_name, tpl, master); } + // -- end create project without org + + // -- begin create project with org name + // * Check for org name argument, use that if present, if not next bullet point + // * if defaults.json exists and contains "org_name" as a key, use that + // * ^--- if it doesn't, use existing without org behaviour + // * ^--- if errors, show error + // * support templates + // -- end create project with org name } void Project::create_job(string name, string project_token){ @@ -58,7 +74,7 @@ void Project::create_job(string name, string project_token){ } } -void Project::template_init(string name, string tpl, bool master) { +void Project::template_init(string name, string org_name, string tpl, bool master) { Template::update(); string directory = project_directory(name); @@ -99,7 +115,7 @@ void Project::template_init(string name, string tpl, bool master) { string err; string proj = replace(project_name, "/", ""); - Json json = Json::parse(Request::create_project(proj).body, err); + Json json = Json::parse(Request::create_project(proj, org_name).body, err); string token = json["token"].string_value(); string job_id = json["jobs"][0]["id"].string_value(); string job_name = json["jobs"][0]["name"].string_value(); @@ -113,7 +129,7 @@ void Project::template_init(string name, string tpl, bool master) { } -void Project::existing_init(string name, bool master) { +void Project::existing_init(string name, string org_name, bool master) { const string source = "project_token: {{token}}\n" "name: {{name}}\n" "jobs:\n" @@ -127,7 +143,7 @@ void Project::existing_init(string name, bool master) { string config_path = directory + "/metis.config.yml"; string project_name = directory.substr(directory.find_last_of("/")); string proj = replace(project_name, "/", ""); - Json json = Json::parse(Request::create_project(proj).body, err); + Json json = Json::parse(Request::create_project(proj, org_name).body, err); string token = json["token"].string_value(); string job_id = json["jobs"][0]["id"].string_value(); string job_name = json["jobs"][0]["name"].string_value(); diff --git a/src/project/project.h b/src/project/project.h index 3d27361..fad38de 100644 --- a/src/project/project.h +++ b/src/project/project.h @@ -7,7 +7,7 @@ class Project { public: - static void init(std::string name, std::string tpl, bool master); + static void init(std::string name, std::string org_name, std::string tpl, bool master); static void create_job(std::string name, std::string project_token); static void kill(std::string project_token); static void kill(std::string jobs, std::string deployments); @@ -18,8 +18,8 @@ class Project { private: static bool exists(std::string directory); - static void template_init(std::string name, std::string tpl, bool master); - static void existing_init(std::string name, bool master); + static void template_init(std::string name, std::string org_name, std::string tpl, bool master); + static void existing_init(std::string name, std::string org_name, bool master); }; #endif diff --git a/src/request/request.cpp b/src/request/request.cpp index 3bf479e..d233a28 100644 --- a/src/request/request.cpp +++ b/src/request/request.cpp @@ -120,6 +120,18 @@ RestClient::Response Request::_create_project(string name) { return this->connection->post(PROJECT_URL, body.dump()); } +RestClient::Response Request::_create_project(string name, string org_name) { + API_HEADERS(); + + Json body = Json::object{ + {"name", name} + }; + + console::warn("Hitting an endpoint that doesn't exist yet!"); + + return this->connection->post(ORGANIZATIONS_URL + "/" + org_name + "/projects", body.dump()); +} + RestClient::Response Request::_create_job(string name, string project_token="") { API_HEADERS(); @@ -392,6 +404,10 @@ RestClient::Response Request::create_project(string name) { return instance()->_create_project(name); } +RestClient::Response Request::create_project(string name, string org_name) { + return instance()->_create_project(name, org_name); +} + RestClient::Response Request::create_job(string name, string project_token) { return instance()->_create_job(name, project_token); } diff --git a/src/request/request.h b/src/request/request.h index 436008f..42d129d 100644 --- a/src/request/request.h +++ b/src/request/request.h @@ -13,6 +13,7 @@ class Request { static RestClient::Response tokens(); static RestClient::Response generate_token(); static RestClient::Response create_project(std::string name); + static RestClient::Response create_project(std::string name, std::string org_name); static RestClient::Response create_job(std::string name, std::string project_token); static RestClient::Response env_vars(std::string project_id); static RestClient::Response env_var(std::string project_id, std::string key); @@ -49,6 +50,7 @@ class Request { RestClient::Response _tokens(); RestClient::Response _generate_token(); RestClient::Response _create_project(std::string name); + RestClient::Response _create_project(std::string name, std::string org_name); RestClient::Response _create_job(std::string name, std::string project_token); RestClient::Response _env_vars(std::string project_id); RestClient::Response _env_var(std::string project_id, std::string key); From 7d358547988fb3ff5a1ee52e4981856bcd2798ff Mon Sep 17 00:00:00 2001 From: Jeremy Tregunna Date: Wed, 29 Aug 2018 12:12:58 -0600 Subject: [PATCH 2/5] finalizes all corner cases --- src/env/env.cpp | 2 +- src/project/project.cpp | 49 +++++++++++++++++++++++------------------ src/request/request.cpp | 6 ++--- src/usage.h | 2 +- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/env/env.cpp b/src/env/env.cpp index cf25964..4c3cdf2 100644 --- a/src/env/env.cpp +++ b/src/env/env.cpp @@ -83,7 +83,7 @@ bool Env::load_credentials() { bool Env::load_defaults() { if(FileManager::file_exists(paths.defaults)) { - string defaults = FileManager::read(paths.credentials); + string defaults = FileManager::read(paths.defaults); string err; Json json = Json::parse(defaults, err); diff --git a/src/project/project.cpp b/src/project/project.cpp index a61abed..bf7cbb2 100644 --- a/src/project/project.cpp +++ b/src/project/project.cpp @@ -22,11 +22,10 @@ void Project::init(string name, string org_name, string tpl, bool master) { Env::instance()->load_defaults(); string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG); - if(org_name == "") { + if(org_name.size() == 0) { org_name = default_org_name; } - // -- begin create project without org string project_name = directory.substr(directory.find_last_of("/")); if(exists(directory)) { @@ -41,15 +40,6 @@ void Project::init(string name, string org_name, string tpl, bool master) { } else { template_init(name, org_name, tpl, master); } - // -- end create project without org - - // -- begin create project with org name - // * Check for org name argument, use that if present, if not next bullet point - // * if defaults.json exists and contains "org_name" as a key, use that - // * ^--- if it doesn't, use existing without org behaviour - // * ^--- if errors, show error - // * support templates - // -- end create project with org name } void Project::create_job(string name, string project_token){ @@ -115,18 +105,28 @@ void Project::template_init(string name, string org_name, string tpl, bool maste string err; string proj = replace(project_name, "/", ""); - Json json = Json::parse(Request::create_project(proj, org_name).body, err); - string token = json["token"].string_value(); - string job_id = json["jobs"][0]["id"].string_value(); - string job_name = json["jobs"][0]["name"].string_value(); + Json json; + if (org_name.size() == 0) { + json = Json::parse(Request::create_project(proj).body, err); + } else { + console::info("Creating project " + project_name + " under organization " + org_name); + json = Json::parse(Request::create_project(proj, org_name).body, err); + } - config_template.setValue("token", token); - config_template.setValue("name", proj); - config_template.setValue("job_id", job_id); - config_template.setValue("job_name", job_name); + if(json["error"].string_value().size() > 0) { + console::error("Unable to create your project: " + json["error"].string_value()); + } else { + string token = json["token"].string_value(); + string job_id = json["jobs"][0]["id"].string_value(); + string job_name = json["jobs"][0]["name"].string_value(); - FileManager::write(template_path, config_template.render()); + config_template.setValue("token", token); + config_template.setValue("name", proj); + config_template.setValue("job_id", job_id); + config_template.setValue("job_name", job_name); + FileManager::write(template_path, config_template.render()); + } } void Project::existing_init(string name, string org_name, bool master) { @@ -143,7 +143,14 @@ void Project::existing_init(string name, string org_name, bool master) { string config_path = directory + "/metis.config.yml"; string project_name = directory.substr(directory.find_last_of("/")); string proj = replace(project_name, "/", ""); - Json json = Json::parse(Request::create_project(proj, org_name).body, err); + Json json; + if (org_name.size() == 0) { + json = Json::parse(Request::create_project(proj).body, err); + } else { + console::info("Creating project " + project_name + " under organization " + org_name); + json = Json::parse(Request::create_project(proj, org_name).body, err); + } + string token = json["token"].string_value(); string job_id = json["jobs"][0]["id"].string_value(); string job_name = json["jobs"][0]["name"].string_value(); diff --git a/src/request/request.cpp b/src/request/request.cpp index d233a28..024bb0e 100644 --- a/src/request/request.cpp +++ b/src/request/request.cpp @@ -127,9 +127,9 @@ RestClient::Response Request::_create_project(string name, string org_name) { {"name", name} }; - console::warn("Hitting an endpoint that doesn't exist yet!"); - - return this->connection->post(ORGANIZATIONS_URL + "/" + org_name + "/projects", body.dump()); + auto uri = ORGANIZATIONS_URL + "/" + org_name + "/projects"; + console::debug("Organizations url: " + uri); + return this->connection->post(uri, body.dump()); } RestClient::Response Request::_create_job(string name, string project_token="") { diff --git a/src/usage.h b/src/usage.h index 0c0cbfd..841ddb6 100644 --- a/src/usage.h +++ b/src/usage.h @@ -5,7 +5,7 @@ static const char USAGE[] = R"( Usage: skafos (setup|init|auth)... - skafos init [] [--template ] [--master] + skafos init [] [--org ] [--template ] [--master] skafos templates [--update] [--search ] skafos env [] [--set ] [--delete] skafos create [] [] [--project ] From b9e5fba90b0146eb8cf01fb03108faf2031c2c55 Mon Sep 17 00:00:00 2001 From: Jeremy Tregunna Date: Wed, 29 Aug 2018 12:43:28 -0600 Subject: [PATCH 3/5] add command help details for org --- src/usage.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/usage.h b/src/usage.h index 841ddb6..9026b9a 100644 --- a/src/usage.h +++ b/src/usage.h @@ -27,6 +27,7 @@ R"( fetch Fetch results from a given table. kill Kill an entire project or specific jobs/deployments. remote info Print command to add a new remote. + org List or set your default organization. Options: -v --version Shows version. From 30591eab250da56e86e3f71017394713980f1f32 Mon Sep 17 00:00:00 2001 From: Jeremy Tregunna Date: Wed, 29 Aug 2018 12:45:26 -0600 Subject: [PATCH 4/5] change default arg to set-default --- src/dispatch/dispatch.cpp | 10 +++++----- src/usage.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/dispatch/dispatch.cpp b/src/dispatch/dispatch.cpp index 93e800e..f6a0056 100644 --- a/src/dispatch/dispatch.cpp +++ b/src/dispatch/dispatch.cpp @@ -56,8 +56,8 @@ struct command create_cmd = {"create", {"--project"}, true, true}; struct command logs_cmd = {"logs", {"-n", "--tail"}, true, true}; struct command fetch_cmd = {"fetch", {"--table"}, true, true}; struct command kill_deployment_cmd = {"kill", {"--deployments", "--job_ids"}, true, true}; -struct command remote_cmd = {"remote", {"--default"}, true, true}; -struct command organizations_cmd = {"orgs", {}, true, true}; +struct command remote_cmd = {"remote", {}, true, true}; +struct command organizations_cmd = {"orgs", {"--set-default"}, true, true}; struct command command_list[11] = { setup_cmd, @@ -363,15 +363,15 @@ void remote(int argc, char **argv, int cmd_index){ void organizations(int argc, char **argv, int cmd_index) { switch(argc) { case 4: - if (string(argv[3]) == "--default") { + if (string(argv[3]) == "--set-default") { std::string name = std::string(argv[2]); Organization::set_default(name); } else { - console::error("You must supply an organization name and the --default argument to switch default organizations."); + console::error("You must supply an organization name and the --set-default argument to switch default organizations."); } break; case 3: - console::error("You must supply an organization name and the --default argument to switch default organizations."); + console::error("You must supply an organization name and the --set-default argument to switch default organizations."); break; case 2: console::info("All your organizations:\n"); diff --git a/src/usage.h b/src/usage.h index 9026b9a..12fa805 100644 --- a/src/usage.h +++ b/src/usage.h @@ -13,7 +13,7 @@ R"( skafos fetch --table skafos kill [] [--deployments ] [--job_ids ] skafos remote info [] - skafos orgs [] [--default] + skafos orgs [] [--set-default] skafos -h | --help skafos -v | --version Commands: From 1ac69a95755d80765d2e682acefc38e302f0ced1 Mon Sep 17 00:00:00 2001 From: Caroline Twigg Date: Wed, 29 Aug 2018 14:50:06 -0400 Subject: [PATCH 5/5] add s to org to match skafos command --- src/usage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/usage.h b/src/usage.h index 12fa805..57cbc77 100644 --- a/src/usage.h +++ b/src/usage.h @@ -27,7 +27,7 @@ R"( fetch Fetch results from a given table. kill Kill an entire project or specific jobs/deployments. remote info Print command to add a new remote. - org List or set your default organization. + orgs List or set your default organization. Options: -v --version Shows version.