Skip to content

Commit

Permalink
fix bug when use default path
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiim committed Dec 9, 2023
1 parent d81a005 commit 845e7a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
63 changes: 41 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ std::string venvPathDefault = std::string(std::getenv("LOCALAPPDATA")) + "/venvt
std::string venvPathDefault = std::string(std::getenv("HOME")) + "/.venvtool";
#endif

// path of venvtool
#if defined(_WIN32) || defined(__MINGW32__)
std::string venvtoolPath = std::string(std::getenv("appdata")) + "/venvtool";
#else
std::string venvtoolPath = std::string(std::getenv("HOME")) + "/.local/bin/venvtool";
#endif

std::string getVenvPath() {
std::string venvPath;

// if already initiated, there will be conf file
// so read venv path from conf file
if (std::filesystem::exists(venvtoolPath + "/.conf")) {
std::ifstream confFile(venvtoolPath + "/.conf");
std::getline(confFile, venvPath);
// else use the default path
} else {
venvPath = venvPathDefault;
// init whith default path
VenvTool::venvInit(venvPath);
}

return venvPath;
}

int main(int argc, char** argv) {
cxxopts::Options options("venvtool", "a tool to manage venvs like conda");

Expand All @@ -34,52 +59,46 @@ int main(int argc, char** argv) {

if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(0);
}

// init venvtool, set root dir for venvs
if (result.count("init")) {
std::string venvPath = result["init"].as<std::string>();
VenvTool::venvInit(venvPath);
exit(0);
}

// read venvPath from conf file
#if defined(_WIN32) || defined(__MINGW32__)
std::string venvtoolPath = std::string(std::getenv("appdata")) + "/venvtool";
#else
std::string venvtoolPath = std::string(std::getenv("HOME")) + "/.local/bin/venvtool";
#endif

std::string venvPath;
// if already initiated, there will be conf file
// so read venv path from conf file
if (std::filesystem::exists(venvtoolPath + "/.conf")) {
std::ifstream confFile(venvtoolPath + "/.conf");
std::getline(confFile, venvPath);
// else use the default path
} else {
venvPath = venvPathDefault;
}

VenvTool venvTool(venvPath);

if (result.count("create")) {
std::string venvName = result["create"].as<std::string>();
// read venvPath from conf file
std::string venvPath = getVenvPath();

VenvTool venvTool(venvPath);
venvTool.venvCreate(venvName);
}
if (result.count("list")) {
std::string venvPath = getVenvPath();

VenvTool venvTool(venvPath);
venvTool.venvList();
}
if (result.count("activate")) {
std::string venvName = result["activate"].as<std::string>();
std::string venvPath = getVenvPath();

VenvTool venvTool(venvPath);
venvTool.venvActivate(venvName);
}
if (result.count("deactivate")) {
std::string venvPath = getVenvPath();

VenvTool venvTool(venvPath);
venvTool.venvDeactive();
}
if (result.count("remove")) {
std::string venvName = result["remove"].as<std::string>();
std::string venvPath = getVenvPath();

VenvTool venvTool(venvPath);
venvTool.venvRemove(venvName);
}

Expand Down
8 changes: 6 additions & 2 deletions src/venvtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ void VenvTool::venvList() {
std::cout << "# Python venvs:" << std::endl;
std::cout << "#" << std::endl;

std::filesystem::path venvRootPath(venvPath_);
for (auto const& entry : std::filesystem::directory_iterator(venvRootPath))
// creat venv path if it not exist
if (!std::filesystem::exists(venvPath_)) {
std::filesystem::create_directory(venvPath_);
}

for (auto const& entry : std::filesystem::directory_iterator(venvPath_))
{
// print venv name and path
std::cout << std::setw(24) << std::left << entry.path().filename()\
Expand Down

0 comments on commit 845e7a0

Please sign in to comment.