@@ -65,57 +65,72 @@ void Project::create_job(string name, string project_token){
6565}
6666
6767void 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
168199void Project::remote_add (string project_token){
0 commit comments