list becomes get_bottles, which returns a map<string, Bottle>
This commit is contained in:
parent
05eeca49c9
commit
9cf038ebc0
@ -1,5 +1,6 @@
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
@ -12,31 +13,38 @@
|
||||
#include "fs.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cellar::bottles;
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
vector<string> cellar::bottles::list() {
|
||||
stringstream sstr_output;
|
||||
vector<string> result;
|
||||
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;
|
||||
|
||||
string homepath = getenv("HOME");
|
||||
vector<string> homedir = cellar::fs::listdir(homepath);
|
||||
for (string item : homedir) {
|
||||
if (item.substr(0,5) == ".wine") {
|
||||
sstr_output << item;
|
||||
sstr_output << " ";
|
||||
Bottle output;
|
||||
|
||||
string fullitem = homepath + "/" + item;
|
||||
output.path = fullitem;
|
||||
|
||||
boost::filesystem::file_status fullitem_status = boost::filesystem::symlink_status(fullitem);
|
||||
bool symlink = boost::filesystem::is_symlink(fullitem_status);
|
||||
|
||||
if (symlink) {
|
||||
sstr_output << "- symlink to ";
|
||||
boost::filesystem::path realpath = boost::filesystem::canonical(fullitem);
|
||||
sstr_output << realpath.string();
|
||||
result.push_back(sstr_output.str());
|
||||
sstr_output.str("");
|
||||
output.canonical_path = realpath.string();
|
||||
output.type = bottle_symlink;
|
||||
} else {
|
||||
output.canonical_path = fullitem;
|
||||
string jsonpath = fullitem + "/cellar.json";
|
||||
if (boost::filesystem::exists(jsonpath)) {
|
||||
try {
|
||||
@ -46,22 +54,18 @@ vector<string> cellar::bottles::list() {
|
||||
sstr_config << configstream.rdbuf();
|
||||
config = json::parse(sstr_config.str());
|
||||
|
||||
sstr_output << "- " << config["name"];
|
||||
result.push_back(sstr_output.str());
|
||||
sstr_output.str(""); // clear it for the next item
|
||||
output.config = config;
|
||||
output.type = bottle_labelled;
|
||||
}
|
||||
catch (const exception &exc) {
|
||||
sstr_output << "- bogus cellar.json file";
|
||||
result.push_back(sstr_output.str());
|
||||
sstr_output.str("");
|
||||
output.type = bottle_error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
sstr_output << "- anonymous wine bottle";
|
||||
result.push_back(sstr_output.str());
|
||||
sstr_output.str("");
|
||||
output.type = bottle_anonymous;
|
||||
}
|
||||
}
|
||||
result[item] = output;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,14 +2,32 @@
|
||||
#define __BOTTLES_HPP
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace cellar {
|
||||
namespace bottles {
|
||||
vector<string> list();
|
||||
enum bottle_type {
|
||||
bottle_error,
|
||||
bottle_anonymous,
|
||||
bottle_labelled,
|
||||
bottle_symlink
|
||||
};
|
||||
class Bottle {
|
||||
public:
|
||||
bottle_type type;
|
||||
json config;
|
||||
string path;
|
||||
string canonical_path;
|
||||
Bottle();
|
||||
};
|
||||
map<string, Bottle> get_bottles();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -7,12 +8,31 @@
|
||||
#include "bottles.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cellar;
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
vector<string> bottles = cellar::bottles::list();
|
||||
for (string item : bottles) {
|
||||
cout << item << endl;
|
||||
map<string, bottles::Bottle> bottles = bottles::get_bottles();
|
||||
for (auto& item : bottles) {
|
||||
cout << item.first << "- ";
|
||||
bottles::Bottle current = item.second;
|
||||
|
||||
switch (current.type) {
|
||||
case bottles::bottle_anonymous:
|
||||
cout << "anonymous wine bottle";
|
||||
break;
|
||||
case bottles::bottle_labelled:
|
||||
cout << current.config["name"];
|
||||
break;
|
||||
case bottles::bottle_symlink:
|
||||
cout << "symlink to ";
|
||||
cout << current.canonical_path;
|
||||
break;
|
||||
default:
|
||||
cout << "broken or unsupported wine bottle";
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user