File size: 5,120 Bytes
2492322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_UTIL_ENVIRONMENT_HPP
#define MAMBA_UTIL_ENVIRONMENT_HPP

#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#include "mamba/fs/filesystem.hpp"
#include "mamba/util/build.hpp"

namespace mamba::util
{
    /**
     * Get an environment variable encoded in UTF8.
     */
    [[nodiscard]] auto get_env(const std::string& key) -> std::optional<std::string>;

    /**
     * Set an environment variable encoded in UTF8.
     */
    void set_env(const std::string& key, const std::string& value);

    /**
     * Unset an environment variable encoded in UTF8.
     */
    void unset_env(const std::string& key);

    using environment_map = std::unordered_map<std::string, std::string>;

    /**
     * Return a map of all environment variables encoded in UTF8.
     *
     * This is useful if one is interested to do an operation over all environment variables
     * when their names are unknown.
     */
    [[nodiscard]] auto get_env_map() -> environment_map;

    /**
     * Equivalent to calling set_env in a loop.
     *
     * This leaves environment variables not referred to in the map unmodified.
     */
    void update_env_map(const environment_map& env);

    /**
     * Set the environment to be exactly the map given.
     *
     * This unsets all environment variables not referred to in the map unmodified.
     */
    void set_env_map(const environment_map& env);

    /*
     * Return the current user home directory.
     */
    [[nodiscard]] auto user_home_dir() -> std::string;

    /**
     * Return the current user config directory.
     *
     * On all platforms, the XDG_CONFIG_HOME environment variables are honored.
     * Otherwise, it returns the OS-specified config directory on Windows, and the XDG default
     * on Unix.
     */
    [[nodiscard]] auto user_config_dir() -> std::string;

    /**
     * Return the current user program data directory.
     *
     * On all platforms, the XDG_DATA_HOME environment variables are honored.
     * Otherwise, it returns the OS-specified config directory on Windows, and the XDG default
     * on Unix.
     */
    [[nodiscard]] auto user_data_dir() -> std::string;

    /**
     * Return the current user program dispensable cache directory.
     *
     * On all platforms, the XDG_CACHE_HOME environment variables are honored.
     * Otherwise, it returns the OS-specified config directory on Windows, and the XDG default
     * on Unix.
     */
    [[nodiscard]] auto user_cache_dir() -> std::string;

    /**
     * Return the character use to separate paths.
     */
    [[nodiscard]] constexpr auto pathsep() -> char;

    /**
     * Return directories of the given prefix path.
     */
    [[nodiscard]] auto get_path_dirs(const fs::u8path& prefix) -> std::vector<fs::u8path>;

    /**
     * Return the full path of a program from its name.
     */
    [[nodiscard]] auto which(std::string_view exe) -> fs::u8path;

    /**
     * Return the full path of a program from its name if found inside the given directories.
     */
    template <typename Iter>
    [[nodiscard]] auto which_in(std::string_view exe, Iter search_path_first, Iter search_path_last)
        -> fs::u8path;

    /**
     * Return the full path of a program from its name if found inside the given directories.
     *
     * The directories can be given as a range or as a @ref pathsep separated list.
     */
    template <typename Range>
    [[nodiscard]] auto which_in(std::string_view exe, const Range& search_paths) -> fs::u8path;

    /********************
     *  Implementation  *
     ********************/

    constexpr auto pathsep() -> char
    {
        if (on_win)
        {
            return ';';
        }
        else
        {
            return ':';
        }
    }

    namespace detail
    {
        [[nodiscard]] auto which_in_one(const fs::u8path& exe, const fs::u8path& dir) -> fs::u8path;

        [[nodiscard]] auto which_in_split(const fs::u8path& exe, std::string_view paths)
            -> fs::u8path;
    }

    template <typename Iter>
    auto which_in(std::string_view exe, Iter first, Iter last) -> fs::u8path
    {
        for (; first != last; ++first)
        {
            if (auto p = detail::which_in_one(exe, *first); !p.empty())
            {
                return p;
            }
        }
        return "";
    }

    template <typename Range>
    auto which_in(std::string_view exe, const Range& search_paths) -> fs::u8path
    {
        if constexpr (std::is_same_v<Range, fs::u8path>)
        {
            return detail::which_in_one(exe, search_paths);
        }
        else if constexpr (std::is_convertible_v<Range, std::string_view>)
        {
            return detail::which_in_split(exe, search_paths);
        }
        else
        {
            return which_in(exe, search_paths.cbegin(), search_paths.cend());
        }
    }
}
#endif