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 13:17:07 -07:00
|
|
|
#include "internal/bottles.hpp"
|
2017-03-23 12:06:29 -07:00
|
|
|
#include "dll.hpp"
|
2017-03-13 19:03:07 -07:00
|
|
|
#include "fs.hpp"
|
2017-03-23 18:51:11 -07:00
|
|
|
#include "output.hpp"
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
using namespace std;
|
2017-03-23 18:51:11 -07:00
|
|
|
using namespace cellar;
|
2017-03-22 19:02:14 -07:00
|
|
|
using namespace cellar::bottles;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
2017-03-23 12:06:29 -07:00
|
|
|
using CommandFunction = cellar::commands::CommandFunction;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-24 17:58:24 -07:00
|
|
|
Bottle::Bottle(string patharg) {
|
|
|
|
config = json({});
|
|
|
|
path = patharg;
|
|
|
|
|
|
|
|
boost::filesystem::file_status path_status = boost::filesystem::symlink_status(path);
|
|
|
|
bool symlink = boost::filesystem::is_symlink(path_status);
|
|
|
|
|
|
|
|
if (symlink) {
|
|
|
|
boost::filesystem::path realpath = boost::filesystem::canonical(path);
|
|
|
|
canonical_path = realpath.string();
|
|
|
|
type = bottle_symlink;
|
|
|
|
} else {
|
|
|
|
canonical_path = path;
|
|
|
|
try {
|
|
|
|
if (load_config()) {
|
|
|
|
type = bottle_labelled;
|
|
|
|
} else {
|
|
|
|
type = bottle_anonymous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const exception &exc) {
|
|
|
|
type = bottle_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 12:06:29 -07:00
|
|
|
DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
|
2017-03-22 19:02:14 -07:00
|
|
|
map<string, Bottle> result;
|
2017-03-13 19:03:07 -07:00
|
|
|
|
|
|
|
string homepath = getenv("HOME");
|
2017-03-23 18:51:11 -07:00
|
|
|
vector<string> homedir = fs::listdir(homepath);
|
2017-03-13 19:03:07 -07:00
|
|
|
for (string item : homedir) {
|
2017-03-23 13:17:07 -07:00
|
|
|
if (item.substr(0,5) == ".wine") {
|
2017-03-22 18:12:12 -07:00
|
|
|
string fullitem = homepath + "/" + item;
|
2017-03-24 17:58:24 -07:00
|
|
|
Bottle output(fullitem);
|
2017-03-13 19:03:07 -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
|
|
|
|
2017-03-23 15:13:52 -07:00
|
|
|
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
|
2017-03-23 00:02:38 -07:00
|
|
|
map<string, Bottle> bottles = get_bottles();
|
|
|
|
|
2017-03-23 18:51:11 -07:00
|
|
|
stringstream outstr;
|
|
|
|
|
2017-03-23 00:02:38 -07:00
|
|
|
for (auto item : bottles) {
|
2017-03-23 14:23:58 -07:00
|
|
|
if (item.first == ".wine" || item.first == ".wine.template") {
|
|
|
|
// .wine is considered to be "active", and .wine.template is used as a template
|
|
|
|
// and therefore treated specially
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-23 00:02:38 -07:00
|
|
|
Bottle bottle = item.second;
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << item.first << " - ";
|
2017-03-23 00:02:38 -07:00
|
|
|
switch (bottle.type) {
|
|
|
|
case bottle_anonymous:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "anonymous wine bottle";
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
|
|
|
case bottle_symlink:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "symlink to " << bottle.canonical_path;
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
|
|
|
case bottle_labelled:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << bottle.config["name"];
|
2017-03-23 00:02:38 -07:00
|
|
|
break;
|
|
|
|
default:
|
2017-03-23 18:51:11 -07:00
|
|
|
outstr << "broken or unsupported wine bottle";
|
2017-03-23 00:02:38 -07:00
|
|
|
}
|
2017-03-23 18:51:11 -07:00
|
|
|
output::statement(outstr.str());
|
|
|
|
outstr.str("");
|
2017-03-23 00:02:38 -07:00
|
|
|
}
|
|
|
|
}
|