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

Commit 9e6ac2c

Browse files
Merge pull request #45 from MetisMachine/ct-defaults-check-auth
check if defaults.json exists on auth and check for success status codes
2 parents 0e0241e + 03c9824 commit 9e6ac2c

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/env/env.cpp

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

4040
bool Env::authenticated() {
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));
41+
if (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") && (std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), Request::org_by_name(default_org_name).code) != SUCCESS_CODES.end()));
44+
}
45+
return false;
4446
}
4547

4648
string Env::get(string key) {
@@ -95,6 +97,10 @@ bool Env::load_defaults() {
9597
}
9698

9799
string org_name = json["org_name"].string_value();
100+
if (org_name.length() < 1){
101+
console::error("Defaults error: no default organization found.");
102+
return false;
103+
}
98104
this->set(METIS_DEFAULT_ORG, org_name);
99105
return true;
100106
}

src/env/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define VERIFY_AUTH Env::instance()->verify_auth
1111
#define ENV_PATHS Env::instance()->paths
1212

13+
const std::list<int> SUCCESS_CODES = {200, 201, 202, 203, 204};
1314
const std::string METIS_API_TOKEN = "METIS_API_TOKEN";
1415
const std::string METIS_AUTH_TOKEN = "METIS_OAUTH";
1516
const std::string METIS_DEFAULT_ORG = "METIS_DEFAULT_ORG";

src/organization/organization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "file/file.h"
22
#include "organization.h"
3+
#include "env/env.h"
34

45
using namespace json11;
56

@@ -11,7 +12,7 @@ void Organization::list() {
1112
int status_code = resp.code;
1213
std::string error_message = json["message"].string_value();
1314

14-
if (status_code != 200) {
15+
if (not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) {
1516
console::error("There was an error listing your organizations: " + std::to_string(status_code) + " - " + error_message + "\n");
1617
} else if (json.is_array()) {
1718
console::info("Your organizations: ");
@@ -32,7 +33,7 @@ void Organization::set_default(std::string org_name) {
3233
int status_code = resp.code;
3334
std::string error_message = json["message"].string_value();
3435

35-
if (status_code != 201) {
36+
if (not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) {
3637
console::error("There was an error setting your default organization: " + std::to_string(status_code) + " - " + error_message + "\n");
3738
} else {
3839
Env::instance()->write_default_org(org_name);

src/project/project.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void Project::template_init(string name, string org_name, string tpl, bool maste
8787
status_code = resp.code;
8888
error_message = json["message"].string_value();
8989

90-
if(status_code != 201) {
90+
if(not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) {
9191
if (org_name.size() == 0) {
9292
console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n");
9393
} else {
@@ -173,7 +173,7 @@ void Project::existing_init(string name, string org_name, bool master) {
173173
status_code = resp.code;
174174
error_message = json["message"].string_value();
175175

176-
if(status_code != 201) {
176+
if(not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) {
177177
if (org_name.size() == 0) {
178178
console::error("Unable to create your project: " + std::to_string(status_code) + " - " + error_message + "\n");
179179
} else {

0 commit comments

Comments
 (0)