Skip to content

Commit

Permalink
fix: JSON output for env export
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan committed Oct 22, 2024
1 parent 75b3ebd commit 360fe6b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions micromamba/src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,69 @@ set_env_command(CLI::App* com, Configuration& config)
std::cout << "\n";
}
}
else if (config.at("json").get_cli_config<bool>())
{
auto pd = PrefixData::create(ctx.prefix_params.target_prefix, channel_context).value();
History& hist = pd.history();

const auto& versions_map = pd.records();

std::cout << "{\n";
std::cout << " \"name\": \"" << get_env_name(ctx, ctx.prefix_params.target_prefix)
<< "\",\n";
std::cout << " \"channels\": [\n";

auto requested_specs_map = hist.get_requested_specs_map();
std::stringstream dependencies;
std::set<std::string> channels;

for (const auto& [k, v] : versions_map)
{
if (from_history && requested_specs_map.find(k) == requested_specs_map.end())
{
continue;
}

auto chans = channel_context.make_channel(v.channel);

if (from_history)
{
dependencies << " \"" << requested_specs_map[k].str() << "\",\n";
}
else
{
dependencies << " \"";
if (channel_subdir)
{
dependencies
// If the size is not one, it's a custom multi channel
<< ((chans.size() == 1) ? chans.front().display_name() : v.channel)
<< "/" << v.platform << "::";
}
dependencies << v.name << "=" << v.version;
if (!no_build)
{
dependencies << "=" << v.build_string;
}
if (no_md5 == -1)
{
dependencies << "[md5=" << v.md5 << "]";
}
dependencies << "\",\n";
}

for (const auto& chan : chans)
{
channels.insert(chan.display_name());
}
}

std::cout << " ],\n";
std::cout << " \"dependencies\": [\n" << dependencies.str() << " ]\n";
std::cout << "}\n";

std::cout.flush();
}
else
{
auto pd = PrefixData::create(ctx.prefix_params.target_prefix, channel_context).value();
Expand All @@ -176,6 +239,7 @@ set_env_command(CLI::App* com, Configuration& config)
auto requested_specs_map = hist.get_requested_specs_map();
std::stringstream dependencies;
std::set<std::string> channels;

for (const auto& [k, v] : versions_map)
{
if (from_history && requested_specs_map.find(k) == requested_specs_map.end())
Expand Down

0 comments on commit 360fe6b

Please sign in to comment.