-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
229 lines (204 loc) · 7.08 KB
/
main.cpp
File metadata and controls
229 lines (204 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// main.cpp
// venv
//
// Created by Daher Alfawares on 11/27/17.
// Copyright © 2017 Daher Alfawares. All rights reserved.
//
#include <iostream>
#include <sstream>
#include "x_options.hpp"
#include "x_shell.hpp"
#include "repository.hpp"
namespace arg {
std::string help = "--help";
std::string init = "--init";
std::string deinit = "--deinit";
std::string list = "--ls";
std::string create = "--create";
std::string add = "--add";
std::string to = "--to";
std::string from = "--from";
std::string push = "--push";
std::string pull = "--pull";
std::string diff = "--diff";
std::string select = "--select";
std::string verbose = "--verbose";
std::string rename = "--rename";
std::string remove = "--remove";
}
int main(int argc, const char * argv[]) try {
using std::cout;
using std::cerr;
using std::endl;
auto repo = v::repository();
switch(argc){
case 1:
{
auto venvs = repo.all_venvs();
if(venvs.empty()){
std::cout << "venv is empty" << std::endl;
} else {
cout << repo << std::endl;
}
break;
}
case 2:
// implicit select..
auto name = argv[1];
if(repo.exists(name)){
repo.current().push();
repo.current(name).pull();
return 0;
}
break;
}
auto arguments = x::options(argc,argv);
arguments.map_to({
{arg::help, x::option("this help message")},
{arg::init, x::option("initializes the current directory for venv use")},
{arg::list, x::option("lists all files in a venv")},
{arg::deinit, x::option("removes all venv-related files")},
{arg::create, x::option("creates a new venv")},
{arg::add, x::option("adds a file to a venv")},
{arg::push, x::option("push all changes to the active venv")},
{arg::to, x::option("specifies a destination")},
{arg::from, x::option("used with --pull switch to provide a source venv")},
{arg::pull, x::option("pulls new copies and replaces working copy")},
{arg::push, x::option("pushes current changes to a venv")},
{arg::diff, x::option("shows diffs of a specific venv")},
{arg::select, x::option("switches the active venv. this command will push & pull current changes automatically")},
{arg::verbose, x::option("prints the executed command")},
{arg::rename, x::option("renames a venv")},
{arg::remove, x::option("removes a venv and all of its content")},
});
if(arguments[arg::verbose]){
std::cout << argv[0] << std::endl;
}
if(arguments[arg::help]){
cout << "venv 1.0 by Daher Alfawares" << std::endl;
cout << arguments.print() << std::endl;
return 0;
}
if(arguments[arg::deinit]){
cout << "removing all venvs" << std::endl;
::system(x::shell::remove(v::root_folder).c_str());
}
if(arguments[arg::init]){
cout << "initializing venv" << endl;
::system(x::shell::mkdir(v::root_folder).c_str());
}
if(arguments[arg::create]){
auto venv_names = arguments[arg::create].values();
if(venv_names.empty()){
cerr << "usage: venv " << arg::create << " <name>" << std::endl;
return -1;
}
for(auto name:venv_names){
auto venv = v::venv(name);
if(!repo.exists(venv.name)){
repo.create(venv);
cout << "creating venv [" << venv.name << "]" << endl;
::system(x::shell::mkdir(v::root_folder + "/" + venv.name).c_str());
} else {
cerr << "[" << venv.name << "]" << " venv already exists." << std::endl;
}
}
}
if(arguments[arg::add]){
if(!arguments[arg::to]){
cerr << "missing " << arg::to << " <venv> in command line" << std::endl;
return -1;
}
auto file_names = arguments[arg::add].values();
auto venv_names = arguments[arg::to].values();
repo.assert_available(venv_names);
for(auto name:venv_names){
v::venv& venv = repo[name];
for(auto file:file_names){
auto item = v::item(file);
venv.add(item);
venv.push(item);
}
}
}
if(arguments[arg::list]){
auto venv_names = arguments[arg::add].values();
if(venv_names.empty()){
cerr << "usage: venv " << arg::list << " venv_name" << std::endl;
return -1;
}
repo.assert_available(venv_names);
for(auto name:venv_names){
auto venv = repo[name];
std::cout << venv << std::endl;
}
}
if(arguments[arg::pull]){
auto venv_names = arguments[arg::pull].values();
if(venv_names.empty()){
cerr << "usage: venv " << arg::pull << " venv_name" << std::endl;
return -1;
}
repo.assert_available(venv_names);
for(auto name:venv_names){
repo[name].pull();
}
}
if(arguments[arg::push]){
auto venv_names = arguments[arg::push].values();
if(venv_names.empty()){
cerr << "usage: venv " << arg::push << " venv_name" << std::endl;
return -1;
}
repo.assert_available(venv_names);
for(auto name:venv_names){
repo[name].push();
}
}
if(arguments[arg::diff]){
auto venv = repo.current();
for(auto item:venv.items){
venv.diff(item);
}
return 0;
}
if(arguments[arg::rename]){
auto names = arguments[arg::rename].values();
if(names.size()!= 2){
cerr << "usage: venv " << arg::rename << " <old name> <new name>" << std::endl;
return -1;
}
repo.rename(names[0],names[1]);
auto from = v::root_folder + "/" + names[0];
auto to = v::root_folder + "/" + names[1];
::system(x::shell::move(from,to).c_str());
return 0;
}
if(arguments[arg::remove]){
auto names = arguments[arg::remove].values();
if(names.empty()){
cerr << "usage: venv " << arg::remove << " <name> <name>" << std::endl;
return -1;
}
for(auto name: names){
repo.remove(name);
::system(x::shell::remove(v::root_folder + "/" + name).c_str());
}
return 0;
}
if(arguments[arg::select]){
auto name = arguments[arg::select].value();
if(name.empty()){
cerr << "usage: $ venv " << arg::select << " <venv name>" << std::endl;
cerr << "usage: $ venv <venv name>" << std::endl;
return -1;
}
repo.assert_available(name);
repo.current().push();
repo.current(name).pull();
return 0;
}
return 0;
}
catch(...){}