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

Commit 8b24fe2

Browse files
authored
Merge pull request #42 from MetisMachine/ct-skafos-whoami
skafos whoami command
2 parents 51f8b9a + 9d69a76 commit 8b24fe2

11 files changed

Lines changed: 77 additions & 6 deletions

File tree

src/auth/auth.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void Auth::authenticate() {
3030
} else {
3131
string err;
3232

33+
Env::instance()->delete_defaults();
3334
Env::instance()->write_credentials(Json::parse(oauth.body, err));
3435
Env::instance()->load_credentials();
3536

src/dispatch/dispatch.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "project_env/project_env.h"
2020
#include "data/data.h"
2121
#include "organization/organization.h"
22+
#include "whoami/whoami.h"
2223

2324
using namespace std;
2425

@@ -58,8 +59,9 @@ struct command fetch_cmd = {"fetch", {"--table"}, true, true};
5859
struct command kill_deployment_cmd = {"kill", {"--deployments", "--job_ids"}, true, true};
5960
struct command remote_cmd = {"remote", {}, true, true};
6061
struct command organizations_cmd = {"orgs", {"--set-default"}, true, true};
62+
struct command whoami_cmd = {"whoami", {}, false, true};
6163

62-
struct command command_list[11] = {
64+
struct command command_list[12] = {
6365
setup_cmd,
6466
init_cmd,
6567
auth_cmd,
@@ -70,7 +72,8 @@ struct command command_list[11] = {
7072
fetch_cmd,
7173
kill_deployment_cmd,
7274
remote_cmd,
73-
organizations_cmd
75+
organizations_cmd,
76+
whoami_cmd
7477
};
7578

7679

@@ -374,12 +377,15 @@ void organizations(int argc, char **argv, int cmd_index) {
374377
console::error("You must supply an organization name and the --set-default argument to switch default organizations.");
375378
break;
376379
case 2:
377-
console::info("All your organizations:\n");
378380
Organization::list();
379381
break;
380382
}
381383
}
382384

385+
void whoami() {
386+
Whoami::information();
387+
}
388+
383389

384390
int Dispatch::dispatch(int argc, char **argv, int cmd_index) {
385391
FunctionCaller disp;
@@ -395,6 +401,7 @@ int Dispatch::dispatch(int argc, char **argv, int cmd_index) {
395401
disp.insert("kill", kill_deployment);
396402
disp.insert("remote", remote);
397403
disp.insert("orgs", organizations);
404+
disp.insert("whoami", whoami);
398405

399406
if(command_list[cmd_index].needs_auth) {
400407
VERIFY_AUTH();

src/env/env.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ void Env::write_default_org(std::string org_name) {
121121
FileManager::write(paths.defaults, str);
122122
}
123123

124+
void Env::delete_defaults() {
125+
if(FileManager::file_exists(paths.defaults)){
126+
FileManager::delete_file(paths.defaults);
127+
}
128+
}
129+
124130
void Env::verify_auth() {
125131
if(Env::instance()->authenticated() == false) {
126132
Auth::authenticate();

src/env/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Env {
4141
bool load_defaults();
4242
void write_credentials(json11::Json object);
4343
void write_default_org(std::string org_name);
44+
void delete_defaults();
4445
void verify_auth();
4546

4647
private:

src/organization/organization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void Organization::list() {
1111
if (error_message.size() > 0) {
1212
console::error("There was an error listing your organizations: " + error_message + "\n");
1313
} else if (json.is_array()) {
14-
console::info("Your organizations:\n");
14+
console::info("Your organizations: ");
1515
auto orgs = json.array_items();
1616
for (int i = 0; i < orgs.size(); i++) {
1717
console::info(" " + orgs[i]["display_name"].string_value());
@@ -25,7 +25,7 @@ void Organization::set_default(std::string org_name) {
2525
std::string error_message = json["error"].string_value();
2626

2727
if (error_message.size() > 0) {
28-
console::error("There was an error listing your organizations: " + error_message + "\n");
28+
console::error("There was an error setting your default organization: " + error_message + "\n");
2929
} else {
3030
Env::instance()->write_default_org(org_name);
3131
console::info(org_name + " is now your default organization");

src/request/request.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const string KILL_DEPLOYMENT_URL = "/kill";
2121
const string DEPLOYMENTS_URL = "/deployments";
2222
const string OLD_ORGANIZATIONS_URL = "/old/organizations";
2323
const string ORGANIZATIONS_URL = "/organizations";
24+
const string ME_URL = "/users/me";
2425

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

345+
RestClient::Response Request::_whoami() {
346+
API_HEADERS();
347+
348+
return this->connection->get(ME_URL);
349+
}
350+
344351
// DOWNLOAD
345352
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
346353
{
@@ -472,6 +479,10 @@ RestClient::Response Request::org_by_name(std::string name) {
472479
return instance()->_org_by_name(name);
473480
}
474481

482+
RestClient::Response Request::whoami() {
483+
return instance()->_whoami();
484+
}
485+
475486
// DOWNLOAD
476487
void Request::download(string url, string save_path) {
477488
instance()->_download(url, save_path);

src/request/request.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ 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 whoami();
3334

3435
static void download(std::string url, std::string save_path);
3536
static std::vector<std::string> string_split(const std::string& s, char delimiter);
@@ -67,6 +68,7 @@ class Request {
6768
RestClient::Response _organization_info();
6869
RestClient::Response _my_organizations();
6970
RestClient::Response _org_by_name(std::string name);
71+
RestClient::Response _whoami();
7072

7173
static void _download(std::string url, std::string save_path);
7274
};

src/usage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ R"(
1414
skafos kill [<project_token>] [--deployments <deployment_ids>] [--job_ids <job_ids>]
1515
skafos remote info [<project_token>]
1616
skafos orgs [<name>] [--set-default]
17+
skafos whoami
1718
skafos -h | --help
1819
skafos -v | --version
1920
Commands:
@@ -28,6 +29,7 @@ R"(
2829
kill Kill an entire project or specific jobs/deployments.
2930
remote info Print command to add a new remote.
3031
orgs List or set your default organization.
32+
whoami List your current user info and settings.
3133
Options:
3234
-v --version Shows version.
3335

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.5.6-dev"
4+
#define VERSION "1.6.0-dev"
55

66
#endif
77

src/whoami/whoami.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "env/env.h"
2+
#include "whoami.h"
3+
#include "organization/organization.h"
4+
5+
using namespace json11;
6+
7+
void Whoami::information() {
8+
Env::instance()->load_defaults();
9+
std::string default_org_name = Env::instance()->get(METIS_DEFAULT_ORG);
10+
11+
std::string err;
12+
Json json = Json::parse(Request::whoami().body, err);
13+
std::string error_message = json["error"].string_value();
14+
15+
if (error_message.size() > 0) {
16+
console::error("There was an error listing your user information. \n");
17+
} else {
18+
console::info("Current Skafos user: ");
19+
console::info(" " + json["email"].string_value() + "\n");
20+
21+
console::info("Default organization: ");
22+
if (default_org_name.size() == 0) {
23+
console::info(" You don't have a default organization set. \n You can set one using the skafos orgs command. \n");
24+
} else {
25+
console::info(" " + default_org_name + "\n");
26+
}
27+
28+
Organization::list();
29+
}
30+
}

0 commit comments

Comments
 (0)