2017-03-13 19:03:07 -07:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <fstream>
|
2017-03-22 19:02:14 -07:00
|
|
|
#include <map>
|
2017-03-13 19:03:07 -07:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include "json.hpp"
|
|
|
|
|
|
|
|
#include "bottles.hpp"
|
2017-03-23 00:02:38 -07:00
|
|
|
#include "commands.hpp"
|
2017-03-13 19:03:07 -07:00
|
|
|
#include "fs.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
2017-03-22 19:02:14 -07:00
|
|
|
using namespace cellar::bottles;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2017-03-22 19:02:14 -07:00
|
|
|
Bottle::Bottle() {
|
|
|
|
// define a null bottle
|
|
|
|
// strings handle themselves
|
|
|
|
config = json({});
|
|
|
|
type = bottle_anonymous;
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, Bottle> cellar::bottles::get_bottles() {
|
|
|
|
map<string, Bottle> result;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
string homepath = getenv("HOME");
|
|
|
|
vector<string> homedir = cellar::fs::listdir(homepath);
|
|
|
|
for (string item : homedir) {
|
2017-03-23 00:02:38 -07:00
|
|
|
if (item.substr(0,6) == ".wine.") {
|
2017-03-22 19:02:14 -07:00
|
|
|
Bottle output;
|
|
|
|
|
2017-03-22 18:12:12 -07:00
|
|
|
string fullitem = homepath + "/" + item;
|
2017-03-22 19:02:14 -07:00
|
|
|
output.path = fullitem;
|
|
|
|
|
2017-03-22 18:12:12 -07:00
|
|
|
boost::filesystem::file_status fullitem_status = boost::filesystem::symlink_status(fullitem);
|
|
|
|
bool symlink = boost::filesystem::is_symlink(fullitem_status);
|
2017-03-13 19:03:07 -07:00
|
|
|
|
2017-03-22 18:12:12 -07:00
|
|
|
if (symlink) {
|
|
|
|
boost::filesystem::path realpath = boost::filesystem::canonical(fullitem);
|
2017-03-22 19:02:14 -07:00
|
|
|
output.canonical_path = realpath.string();
|
|
|
|
output.type = bottle_symlink;
|
2017-03-22 18:12:12 -07:00
|
|
|
} else {
|
2017-03-22 19:02:14 -07:00
|
|
|
output.canonical_path = fullitem;
|
2017-03-22 18:12:12 -07:00
|
|
|
string jsonpath = fullitem + "/cellar.json";
|
|
|
|
if (boost::filesystem::exists(jsonpath)) {
|
|
|
|
try {
|
|
|
|
json config;
|
|
|
|
ifstream configstream(jsonpath);
|
|
|
|
stringstream sstr_config;
|
|
|
|
sstr_config << configstream.rdbuf();
|
2017-03-22 19:02:14 -07:00
|
|
|
config = json::parse(sstr_config.str());
|
|
|
|
|
|
|
|
output.config = config;
|
|
|
|
output.type = bottle_labelled;
|
2017-03-22 18:12:12 -07:00
|
|
|
}
|
|
|
|
catch (const exception &exc) {
|
2017-03-22 19:02:14 -07:00
|
|
|
output.type = bottle_error;
|
2017-03-22 18:12:12 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 19:02:14 -07:00
|
|
|
else {
|
|
|
|
output.type = bottle_anonymous;
|
2017-03-22 18:12:12 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-22 19:02:14 -07:00
|
|
|
result[item] = output;
|
|
|
|
}
|
2017-03-13 19:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2017-03-23 00:02:38 -07:00
|
|
|
|
|
|
|
void print_bottles(int argc, char** argv) {
|
|
|
|
map<string, Bottle> bottles = get_bottles();
|
|
|
|
|
|
|
|
for (auto item : bottles) {
|
|
|
|
Bottle bottle = item.second;
|
|
|
|
cout << item.first << " - ";
|
|
|
|
switch (bottle.type) {
|
|
|
|
case bottle_anonymous:
|
|
|
|
cout << "anonymous wine bottle";
|
|
|
|
break;
|
|
|
|
case bottle_symlink:
|
|
|
|
cout << "symlink to " << bottle.canonical_path;
|
|
|
|
break;
|
|
|
|
case bottle_labelled:
|
|
|
|
cout << bottle.config["name"];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cout << "broken or unsupported wine bottle";
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cellar::commands::CommandFunction listcmd = cellar::commands::command_map["list"] = &print_bottles;
|