-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnvOutput.cpp
More file actions
173 lines (141 loc) · 3.82 KB
/
Copy pathEnvOutput.cpp
File metadata and controls
173 lines (141 loc) · 3.82 KB
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
/**
*
* @file EnvOutput.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/env/EnvOutput.hpp>
#include <vix/cli/util/Ui.hpp>
#include <ostream>
#include <string>
namespace vix::commands::env::output
{
namespace
{
std::string yes_no(bool value)
{
return value ? "yes" : "no";
}
std::string found_missing(bool value)
{
return value ? "found" : "missing";
}
std::string variable_status(const EnvVariable &variable)
{
if (variable.required && !variable.presentInEnv)
return "required missing";
if (!variable.presentInEnv && variable.presentInExample)
return "missing from .env";
if (variable.presentInEnv && !variable.presentInExample)
return "missing from .env.example";
if (variable.presentInEnv)
return "loaded";
return "missing";
}
}
std::string display_value(
const EnvVariable &variable,
const EnvOptions &options)
{
if (!options.showValues)
return "";
if (variable.secret)
return "********";
if (options.masked)
return variable.value.empty() ? "" : "********";
return variable.value;
}
void print_summary(
std::ostream &out,
const EnvConfig &cfg,
const EnvOptions &options)
{
vix::cli::util::section(out, "Env Check");
vix::cli::util::kv(out, "App", cfg.appName);
vix::cli::util::kv(out, "Project", cfg.projectDir.string());
vix::cli::util::kv(out, ".env", found_missing(cfg.env.exists));
vix::cli::util::kv(out, ".env.example", found_missing(cfg.example.exists));
vix::cli::util::kv(out, "Production", yes_no(options.production));
vix::cli::util::kv(out, "Masked", yes_no(options.masked));
vix::cli::util::kv(out, "Show values", yes_no(options.showValues));
if (options.production)
{
vix::cli::util::kv(
out,
"Service",
cfg.serviceName.empty() ? "(missing)" : cfg.serviceName);
vix::cli::util::kv(
out,
"Required vars",
std::to_string(cfg.requiredProductionKeys.size()));
vix::cli::util::kv(
out,
"Systemd vars",
std::to_string(cfg.systemdEnvironment.size()));
}
}
void print_variable(
std::ostream &out,
const EnvVariable &variable,
const EnvOptions &options)
{
std::string message = variable.name + " : " + variable_status(variable);
const std::string value = display_value(variable, options);
if (!value.empty())
message += " = " + value;
if (variable.secret)
message += " [secret]";
if (variable.required)
message += " [required]";
if (variable.presentInSystemd)
message += " [systemd]";
if (variable.required && !variable.presentInEnv)
{
vix::cli::util::err_line(out, message);
return;
}
if (!variable.presentInEnv ||
!variable.presentInExample)
{
vix::cli::util::warn_line(out, message);
return;
}
vix::cli::util::ok_line(out, message);
}
void section(
std::ostream &out,
const std::string &label)
{
vix::cli::util::section(out, label);
}
void ok(
std::ostream &out,
const std::string &message)
{
vix::cli::util::ok_line(out, message);
}
void warn(
std::ostream &out,
const std::string &message)
{
vix::cli::util::warn_line(out, message);
}
void error(
std::ostream &out,
const std::string &message)
{
vix::cli::util::err_line(out, message);
}
void fix(
std::ostream &out,
const std::string &message)
{
vix::cli::util::warn_line(out, "Fix: " + message);
}
}