Skip to content
This repository was archived by the owner on Jun 12, 2019. It is now read-only.

Commit fc5b0d0

Browse files
Merge pull request #44 from MetisMachine/ct-default-org-bug
default org bug - load default org on skafos auth
2 parents bf427f7 + b75cc8f commit fc5b0d0

8 files changed

Lines changed: 155 additions & 69 deletions

File tree

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,31 @@ You can install [https://brew.sh/](Homebrew), or use the same process as Linux.
3838

3939
## Usage:
4040
skafos (setup|init|auth)...
41-
skafos init [<name>] [--template <template_name>] [--master]
41+
skafos init [<name>] [--org <org_name>] [--template <template_name>] [--master]
4242
skafos templates [--update] [--search <search_term>]
4343
skafos env [<key>] [--set <value>] [--delete]
44+
skafos create [<kind>] [<name>] [--project <token>]
4445
skafos logs [<project_token>] [-n <num>] [--tail]
4546
skafos fetch --table <table_name>
4647
skafos kill [<project_token>] [--deployments <deployment_ids>] [--job_ids <job_ids>]
48+
skafos remote info [<project_token>]
49+
skafos orgs [<name>] [--set-default]
50+
skafos whoami
4751
skafos -h | --help
4852
skafos -v | --version
4953
## Commands:
50-
setup Setup development environment.
51-
init Create a new project.
52-
auth Authenticate request.
53-
templates Manage templates.
54-
env Get or set environment variables.
55-
logs Get logs for a project.
56-
fetch Fetch results from a given table.
57-
kill Kill an entire project or specific jobs/deployments.
54+
setup Setup development environment.
55+
init Create a new project.
56+
auth Authenticate request.
57+
templates Manage templates.
58+
create Create a Job for a project.
59+
env Get or set environment variables.
60+
logs Get logs for a project.
61+
fetch Fetch results from a given table.
62+
kill Kill an entire project or specific jobs/deployments.
63+
remote info Print command to add a new remote.
64+
orgs List or set your default organization.
65+
whoami List your current user info and settings.
5866
## Options:
5967
-V --version Shows version.
6068

src/auth/auth.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ void Auth::authenticate() {
4646

4747
console::debug("Loading credentials...");
4848
Env::instance()->load_credentials();
49+
50+
console::info("Loading defaults...");
51+
52+
auto default_org = Json::parse(Request::my_default_org().body, err);
53+
54+
Env::instance()->write_default_org(default_org["display_name"].string_value());
55+
56+
Env::instance()->load_defaults();
4957
}
5058
}
5159

src/env/env.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ void Env::setup() {
3838
}
3939

4040
bool Env::authenticated() {
41-
return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong"));
41+
Env::instance()->load_defaults();
42+
string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG);
43+
return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong") && (Request::org_by_name(default_org_name).code == 200));
4244
}
4345

4446
string Env::get(string key) {

src/organization/organization.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,36 @@
44
using namespace json11;
55

66
void Organization::list() {
7+
RestClient::Response resp = Request::my_organizations();
78
std::string err;
8-
Json json = Json::parse(Request::my_organizations().body, err);
9-
std::string error_message = json["error"].string_value();
9+
Json json = Json::parse(resp.body, err);
1010

11-
if (error_message.size() > 0) {
12-
console::error("There was an error listing your organizations: " + error_message + "\n");
11+
int status_code = resp.code;
12+
std::string error_message = json["message"].string_value();
13+
14+
if (status_code != 200) {
15+
console::error("There was an error listing your organizations: " + std::to_string(status_code) + " - " + error_message + "\n");
1316
} else if (json.is_array()) {
1417
console::info("Your organizations: ");
1518
auto orgs = json.array_items();
1619
for (int i = 0; i < orgs.size(); i++) {
1720
console::info(" " + orgs[i]["display_name"].string_value());
1821
}
22+
} else {
23+
console::error("There was an error listing your organizations \n");
1924
}
2025
}
2126

2227
void Organization::set_default(std::string org_name) {
28+
RestClient::Response resp = Request::set_default_org(org_name);
2329
std::string err;
24-
Json json = Json::parse(Request::org_by_name(org_name).body, err);
25-
std::string error_message = json["error"].string_value();
30+
Json json = Json::parse(resp.body, err);
31+
32+
int status_code = resp.code;
33+
std::string error_message = json["message"].string_value();
2634

27-
if (error_message.size() > 0) {
28-
console::error("There was an error setting your default organization: " + error_message + "\n");
35+
if (status_code != 201) {
36+
console::error("There was an error setting your default organization: " + std::to_string(status_code) + " - " + error_message + "\n");
2937
} else {
3038
Env::instance()->write_default_org(org_name);
3139
console::info(org_name + " is now your default organization");

src/project/project.cpp

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,57 +65,72 @@ void Project::create_job(string name, string project_token){
6565
}
6666

6767
void Project::template_init(string name, string org_name, string tpl, bool master) {
68-
Template::update();
69-
68+
7069
string directory = project_directory(name);
7170
string project_name = directory.substr(directory.find_last_of("/"));
72-
73-
if(exists(directory)) {
74-
string config_path = directory + "metis.config.yml";
75-
76-
console::error("Not an empty directory, we don't want to overwrite any current projects");
77-
exit(EXIT_FAILURE);
71+
72+
string proj = replace(project_name, "/", "");
73+
74+
Json json;
75+
string err;
76+
int status_code;
77+
string error_message;
78+
RestClient::Response resp;
79+
if (org_name.size() == 0) {
80+
resp = Request::create_project(proj);
81+
json = Json::parse(resp.body, err);
82+
} else {
83+
resp = Request::create_project(proj, org_name);
84+
json = Json::parse(resp.body, err);
7885
}
7986

80-
FileManager::create_path(0755, directory);
87+
status_code = resp.code;
88+
error_message = json["message"].string_value();
8189

82-
TemplateDetails tpl_details = Template::find(tpl);
83-
if(tpl_details.repo.length() < 1) {
84-
console::error("Unable to find template: " + tpl);
90+
if(status_code != 201) {
91+
if (org_name.size() == 0) {
92+
console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n");
93+
} else {
94+
console::error("Unable to create your project under organization " + org_name + ": " + std::to_string(status_code) + " - " + error_message + "\n");
95+
}
96+
} else {
97+
Template::update();
98+
99+
console::info("Creating project " + project_name + " under organization " + org_name);
100+
101+
if(exists(directory)) {
102+
string config_path = directory + "metis.config.yml";
103+
104+
console::error("Not an empty directory, we don't want to overwrite any current projects");
105+
exit(EXIT_FAILURE);
106+
}
85107

86-
exit(EXIT_FAILURE);
87-
}
108+
FileManager::create_path(0755, directory);
88109

89-
string version = (!master && tpl_details.version.length() > 0)
90-
? tpl_details.version
91-
: "master";
110+
TemplateDetails tpl_details = Template::find(tpl);
111+
if(tpl_details.repo.length() < 1) {
112+
console::error("Unable to find template: " + tpl);
92113

93-
Template::download(tpl_details, version);
94-
95-
console::info("Setting up template in project...");
114+
exit(EXIT_FAILURE);
115+
}
96116

97-
string cache_path = ENV_PATHS.cache + "/" + tpl + ".zip";
98-
99-
FileManager::unzip(cache_path, directory);
117+
string version = (!master && tpl_details.version.length() > 0)
118+
? tpl_details.version
119+
: "master";
100120

101-
console::info("Creating configuration file");
121+
Template::download(tpl_details, version);
122+
123+
console::info("Setting up template in project...");
102124

103-
string template_path = directory + "/metis.config.yml";
104-
Jinja::Template config_template = FileManager::read(template_path);
125+
string cache_path = ENV_PATHS.cache + "/" + tpl + ".zip";
105126

106-
string err;
107-
string proj = replace(project_name, "/", "");
108-
Json json;
109-
if (org_name.size() == 0) {
110-
json = Json::parse(Request::create_project(proj).body, err);
111-
} else {
112-
console::info("Creating project " + project_name + " under organization " + org_name);
113-
json = Json::parse(Request::create_project(proj, org_name).body, err);
114-
}
127+
FileManager::unzip(cache_path, directory);
128+
129+
console::info("Creating configuration file");
130+
131+
string template_path = directory + "/metis.config.yml";
132+
Jinja::Template config_template = FileManager::read(template_path);
115133

116-
if(json["error"].string_value().size() > 0) {
117-
console::error("Unable to create your project: " + json["error"].string_value());
118-
} else {
119134
string token = json["token"].string_value();
120135
string job_id = json["jobs"][0]["id"].string_value();
121136
string job_name = json["jobs"][0]["name"].string_value();
@@ -144,25 +159,41 @@ void Project::existing_init(string name, string org_name, bool master) {
144159
string project_name = directory.substr(directory.find_last_of("/"));
145160
string proj = replace(project_name, "/", "");
146161
Json json;
162+
int status_code;
163+
string error_message;
164+
RestClient::Response resp;
147165
if (org_name.size() == 0) {
148-
json = Json::parse(Request::create_project(proj).body, err);
166+
resp = Request::create_project(proj);
167+
json = Json::parse(resp.body, err);
149168
} else {
150-
console::info("Creating project " + project_name + " under organization " + org_name);
151-
json = Json::parse(Request::create_project(proj, org_name).body, err);
169+
resp = Request::create_project(proj, org_name);
170+
json = Json::parse(resp.body, err);
152171
}
153172

154-
string token = json["token"].string_value();
155-
string job_id = json["jobs"][0]["id"].string_value();
156-
string job_name = json["jobs"][0]["name"].string_value();
173+
status_code = resp.code;
174+
error_message = json["message"].string_value();
175+
176+
if(status_code != 201) {
177+
if (org_name.size() == 0) {
178+
console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n");
179+
} else {
180+
console::error("Unable to create your project under organization " + org_name + ": " + std::to_string(status_code) + " - " + error_message + "\n");
181+
}
182+
} else {
157183

158-
Jinja::Template config_template(source);
184+
string token = json["token"].string_value();
185+
string job_id = json["jobs"][0]["id"].string_value();
186+
string job_name = json["jobs"][0]["name"].string_value();
159187

160-
config_template.setValue("token", token);
161-
config_template.setValue("name", proj);
162-
config_template.setValue("job_id", job_id);
163-
config_template.setValue("job_name", job_name);
188+
Jinja::Template config_template(source);
164189

165-
FileManager::write(config_path, config_template.render());
190+
config_template.setValue("token", token);
191+
config_template.setValue("name", proj);
192+
config_template.setValue("job_id", job_id);
193+
config_template.setValue("job_name", job_name);
194+
195+
FileManager::write(config_path, config_template.render());
196+
}
166197
}
167198

168199
void Project::remote_add(string project_token){

src/request/request.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const string DEPLOYMENTS_URL = "/deployments";
2222
const string OLD_ORGANIZATIONS_URL = "/old/organizations";
2323
const string ORGANIZATIONS_URL = "/organizations";
2424
const string ME_URL = "/users/me";
25+
const string DEFAULT_ORG_URL = "/organizations/default";
2526

2627
#define DEFAULT_HEADERS() \
2728
RestClient::HeaderFields headers = this->_default_headers(); \
@@ -342,6 +343,22 @@ RestClient::Response Request::_org_by_name(std::string name) {
342343
return this->connection->get(uri);
343344
}
344345

346+
RestClient::Response Request::_set_default_org(std::string name) {
347+
API_HEADERS();
348+
349+
Json body = Json::object{
350+
{"default_org", name}
351+
};
352+
353+
return this->connection->post(DEFAULT_ORG_URL, body.dump());
354+
}
355+
356+
RestClient::Response Request::_my_default_org() {
357+
API_HEADERS();
358+
359+
return this->connection->get(DEFAULT_ORG_URL);
360+
}
361+
345362
RestClient::Response Request::_whoami() {
346363
API_HEADERS();
347364

@@ -479,6 +496,14 @@ RestClient::Response Request::org_by_name(std::string name) {
479496
return instance()->_org_by_name(name);
480497
}
481498

499+
RestClient::Response Request::set_default_org(std::string name) {
500+
return instance()->_set_default_org(name);
501+
}
502+
503+
RestClient::Response Request::my_default_org() {
504+
return instance()->_my_default_org();
505+
}
506+
482507
RestClient::Response Request::whoami() {
483508
return instance()->_whoami();
484509
}

src/request/request.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Request {
3030
static RestClient::Response organization_info();
3131
static RestClient::Response my_organizations();
3232
static RestClient::Response org_by_name(std::string name);
33+
static RestClient::Response set_default_org(std::string name);
34+
static RestClient::Response my_default_org();
3335
static RestClient::Response whoami();
3436

3537
static void download(std::string url, std::string save_path);
@@ -68,6 +70,8 @@ class Request {
6870
RestClient::Response _organization_info();
6971
RestClient::Response _my_organizations();
7072
RestClient::Response _org_by_name(std::string name);
73+
RestClient::Response _set_default_org(std::string name);
74+
RestClient::Response _my_default_org();
7175
RestClient::Response _whoami();
7276

7377
static void _download(std::string url, std::string save_path);

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __CLI_VERSION__
22
#define __CLI_VERSION__
33

4-
#define VERSION "1.7.0"
4+
#define VERSION "1.7.1-dev"
55

66
#endif
77

0 commit comments

Comments
 (0)