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

Commit

Permalink
Merge pull request #45 from MetisMachine/ct-defaults-check-auth
Browse files Browse the repository at this point in the history
check if defaults.json exists on auth and check for success status codes
  • Loading branch information
jeremytregunna authored Oct 3, 2018
2 parents 0e0241e + 03c9824 commit 9e6ac2c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/env/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ void Env::setup() {
}

bool Env::authenticated() {
Env::instance()->load_defaults();
string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG);
return (FileManager::file_exists(paths.credentials) && (Request::ping().body == "pong") && (Request::org_by_name(default_org_name).code == 200));
if (Env::instance()->load_defaults()){
string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG);
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()));
}
return false;
}

string Env::get(string key) {
Expand Down Expand Up @@ -95,6 +97,10 @@ bool Env::load_defaults() {
}

string org_name = json["org_name"].string_value();
if (org_name.length() < 1){
console::error("Defaults error: no default organization found.");
return false;
}
this->set(METIS_DEFAULT_ORG, org_name);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/env/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define VERIFY_AUTH Env::instance()->verify_auth
#define ENV_PATHS Env::instance()->paths

const std::list<int> SUCCESS_CODES = {200, 201, 202, 203, 204};
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";
Expand Down
5 changes: 3 additions & 2 deletions src/organization/organization.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "file/file.h"
#include "organization.h"
#include "env/env.h"

using namespace json11;

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

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

if (status_code != 201) {
if (not(std::find(SUCCESS_CODES.begin(), SUCCESS_CODES.end(), status_code) != SUCCESS_CODES.end())) {
console::error("There was an error setting your default organization: " + std::to_string(status_code) + " - " + error_message + "\n");
} else {
Env::instance()->write_default_org(org_name);
Expand Down
4 changes: 2 additions & 2 deletions src/project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void Project::template_init(string name, string org_name, string tpl, bool maste
status_code = resp.code;
error_message = json["message"].string_value();

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

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

0 comments on commit 9e6ac2c

Please sign in to comment.