-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnvCommand.cpp
More file actions
150 lines (124 loc) · 3.45 KB
/
Copy pathEnvCommand.cpp
File metadata and controls
150 lines (124 loc) · 3.45 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
/**
*
* @file EnvCommand.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/EnvCommand.hpp>
#include <vix/cli/commands/env/EnvChecker.hpp>
#include <vix/cli/commands/env/EnvConfig.hpp>
#include <vix/cli/commands/env/EnvOutput.hpp>
#include <vix/cli/commands/env/EnvTypes.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace vix::commands
{
namespace
{
bool consume_flag(
std::vector<std::string> &args,
const std::string &flag)
{
for (auto it = args.begin(); it != args.end(); ++it)
{
if (*it == flag)
{
args.erase(it);
return true;
}
}
return false;
}
env::EnvOptions parse_options(
std::vector<std::string> &args)
{
env::EnvOptions options;
options.production = consume_flag(args, "--production");
if (consume_flag(args, "--show-values"))
options.showValues = true;
if (consume_flag(args, "--masked"))
options.masked = true;
if (consume_flag(args, "--no-mask"))
options.masked = false;
return options;
}
int run_check(std::vector<std::string> args)
{
env::EnvOptions options = parse_options(args);
if (!args.empty())
{
env::output::error(
std::cerr,
"unknown env check argument: " + args[0]);
env::output::fix(
std::cerr,
"vix env check --help");
return 1;
}
const env::EnvConfig cfg =
env::load_env_config(options);
return env::checker::check(cfg, options);
}
}
int EnvCommand::run(const std::vector<std::string> &argsIn)
{
if (argsIn.empty() ||
argsIn[0] == "-h" ||
argsIn[0] == "--help")
{
return help();
}
const std::string action = argsIn[0];
if (action == "check")
{
std::vector<std::string> args;
if (argsIn.size() > 1)
args.assign(argsIn.begin() + 1, argsIn.end());
if (!args.empty() &&
(args[0] == "-h" || args[0] == "--help"))
{
return help();
}
return run_check(std::move(args));
}
env::output::error(
std::cerr,
"unknown env command: " + action);
env::output::fix(
std::cerr,
"vix env --help");
return 1;
}
int EnvCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix env <command> [options]\n\n"
<< "Commands:\n"
<< " check Check project environment files\n\n"
<< "Options:\n"
<< " --production Validate production env requirements and systemd env\n"
<< " --masked Mask printed values\n"
<< " --no-mask Do not mask non-secret values when using --show-values\n"
<< " --show-values Print env values; secrets are always masked\n"
<< " -h, --help Show this help message\n\n"
<< "Examples:\n"
<< " vix env check\n"
<< " vix env check --production\n"
<< " vix env check --production --show-values\n\n"
<< "Config:\n"
<< " .env\n"
<< " .env.example\n"
<< " production.env.required\n"
<< " production.service.name\n"
<< " production.deploy.service\n";
return 0;
}
}