mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-15 02:36:16 +02:00
Clean up standard stream logic
Now we have enough portability stuff
This commit is contained in:
parent
0ed5af164f
commit
372353722e
9 changed files with 44 additions and 18 deletions
|
@ -543,7 +543,7 @@ public:
|
||||||
auto state(state_.lock());
|
auto state(state_.lock());
|
||||||
if (!state->active) return {};
|
if (!state->active) return {};
|
||||||
std::cerr << fmt("\r\e[K%s ", msg);
|
std::cerr << fmt("\r\e[K%s ", msg);
|
||||||
auto s = trim(readLine(STDIN_FILENO, true));
|
auto s = trim(readLine(getStandardInput(), true));
|
||||||
if (s.size() != 1) return {};
|
if (s.size() != 1) return {};
|
||||||
draw(*state);
|
draw(*state);
|
||||||
return s[0];
|
return s[0];
|
||||||
|
|
|
@ -106,8 +106,25 @@ void drainFD(
|
||||||
#endif
|
#endif
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get [Standard Input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin))
|
||||||
|
*/
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
inline Descriptor getStandardOut() {
|
inline Descriptor getStandardInput()
|
||||||
|
{
|
||||||
|
#ifndef _WIN32
|
||||||
|
return STDIN_FILENO;
|
||||||
|
#else
|
||||||
|
return GetStdHandle(STD_INPUT_HANDLE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get [Standard Output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout))
|
||||||
|
*/
|
||||||
|
[[gnu::always_inline]]
|
||||||
|
inline Descriptor getStandardOutput()
|
||||||
|
{
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
return STDOUT_FILENO;
|
return STDOUT_FILENO;
|
||||||
#else
|
#else
|
||||||
|
@ -115,6 +132,19 @@ inline Descriptor getStandardOut() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get [Standard Error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))
|
||||||
|
*/
|
||||||
|
[[gnu::always_inline]]
|
||||||
|
inline Descriptor getStandardError()
|
||||||
|
{
|
||||||
|
#ifndef _WIN32
|
||||||
|
return STDERR_FILENO;
|
||||||
|
#else
|
||||||
|
return GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatic cleanup of resources.
|
* Automatic cleanup of resources.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -38,7 +38,7 @@ void Logger::warn(const std::string & msg)
|
||||||
|
|
||||||
void Logger::writeToStdout(std::string_view s)
|
void Logger::writeToStdout(std::string_view s)
|
||||||
{
|
{
|
||||||
Descriptor standard_out = getStandardOut();
|
Descriptor standard_out = getStandardOutput();
|
||||||
writeFull(standard_out, s);
|
writeFull(standard_out, s);
|
||||||
writeFull(standard_out, "\n");
|
writeFull(standard_out, "\n");
|
||||||
}
|
}
|
||||||
|
@ -118,11 +118,7 @@ void writeToStderr(std::string_view s)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
writeFull(
|
writeFull(
|
||||||
#ifdef _WIN32
|
getStandardError(),
|
||||||
GetStdHandle(STD_ERROR_HANDLE),
|
|
||||||
#else
|
|
||||||
STDERR_FILENO,
|
|
||||||
#endif
|
|
||||||
s, false);
|
s, false);
|
||||||
} catch (SystemError & e) {
|
} catch (SystemError & e) {
|
||||||
/* Ignore failing writes to stderr. We need to ignore write
|
/* Ignore failing writes to stderr. We need to ignore write
|
||||||
|
|
|
@ -694,7 +694,7 @@ static void opDump(Strings opFlags, Strings opArgs)
|
||||||
if (!opFlags.empty()) throw UsageError("unknown flag");
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
||||||
if (opArgs.size() != 1) throw UsageError("only one argument allowed");
|
if (opArgs.size() != 1) throw UsageError("only one argument allowed");
|
||||||
|
|
||||||
FdSink sink(getStandardOut());
|
FdSink sink(getStandardOutput());
|
||||||
std::string path = *opArgs.begin();
|
std::string path = *opArgs.begin();
|
||||||
dumpPath(path, sink);
|
dumpPath(path, sink);
|
||||||
sink.flush();
|
sink.flush();
|
||||||
|
@ -722,7 +722,7 @@ static void opExport(Strings opFlags, Strings opArgs)
|
||||||
for (auto & i : opArgs)
|
for (auto & i : opArgs)
|
||||||
paths.insert(store->followLinksToStorePath(i));
|
paths.insert(store->followLinksToStorePath(i));
|
||||||
|
|
||||||
FdSink sink(getStandardOut());
|
FdSink sink(getStandardOutput());
|
||||||
store->exportPaths(paths, sink);
|
store->exportPaths(paths, sink);
|
||||||
sink.flush();
|
sink.flush();
|
||||||
}
|
}
|
||||||
|
@ -835,7 +835,7 @@ static void opServe(Strings opFlags, Strings opArgs)
|
||||||
if (!opArgs.empty()) throw UsageError("no arguments expected");
|
if (!opArgs.empty()) throw UsageError("no arguments expected");
|
||||||
|
|
||||||
FdSource in(STDIN_FILENO);
|
FdSource in(STDIN_FILENO);
|
||||||
FdSink out(getStandardOut());
|
FdSink out(getStandardOutput());
|
||||||
|
|
||||||
/* Exchange the greeting. */
|
/* Exchange the greeting. */
|
||||||
ServeProto::Version clientVersion =
|
ServeProto::Version clientVersion =
|
||||||
|
|
|
@ -16,7 +16,7 @@ struct MixCat : virtual Args
|
||||||
throw Error("path '%1%' is not a regular file", path);
|
throw Error("path '%1%' is not a regular file", path);
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
|
|
||||||
writeFull(getStandardOut(), accessor->readFile(CanonPath(path)));
|
writeFull(getStandardOutput(), accessor->readFile(CanonPath(path)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ struct CmdDumpPath : StorePathCommand
|
||||||
|
|
||||||
void run(ref<Store> store, const StorePath & storePath) override
|
void run(ref<Store> store, const StorePath & storePath) override
|
||||||
{
|
{
|
||||||
FdSink sink(getStandardOut());
|
FdSink sink(getStandardOutput());
|
||||||
store->narFromPath(storePath, sink);
|
store->narFromPath(storePath, sink);
|
||||||
sink.flush();
|
sink.flush();
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ struct CmdDumpPath2 : Command
|
||||||
|
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
FdSink sink(getStandardOut());
|
FdSink sink(getStandardOutput());
|
||||||
dumpPath(path, sink);
|
dumpPath(path, sink);
|
||||||
sink.flush();
|
sink.flush();
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
|
||||||
|
|
||||||
else if (raw) {
|
else if (raw) {
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
writeFull(getStandardOut(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
|
writeFull(getStandardOutput(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (json) {
|
else if (json) {
|
||||||
|
|
|
@ -57,7 +57,7 @@ struct CmdLog : InstallableCommand
|
||||||
if (!log) continue;
|
if (!log) continue;
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
|
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
|
||||||
writeFull(getStandardOut(), *log);
|
writeFull(getStandardOutput(), *log);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -177,7 +177,7 @@ struct CmdKeyGenerateSecret : Command
|
||||||
throw UsageError("required argument '--key-name' is missing");
|
throw UsageError("required argument '--key-name' is missing");
|
||||||
|
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
writeFull(getStandardOut(), SecretKey::generate(*keyName).to_string());
|
writeFull(getStandardOutput(), SecretKey::generate(*keyName).to_string());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ struct CmdKeyConvertSecretToPublic : Command
|
||||||
{
|
{
|
||||||
SecretKey secretKey(drainFD(STDIN_FILENO));
|
SecretKey secretKey(drainFD(STDIN_FILENO));
|
||||||
stopProgressBar();
|
stopProgressBar();
|
||||||
writeFull(getStandardOut(), secretKey.toPublicKey().to_string());
|
writeFull(getStandardOutput(), secretKey.toPublicKey().to_string());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue