From cd2196b08981a96cf607ad4a8f2f0dfa8cdf2add Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 9 Feb 2016 21:28:29 +0100 Subject: [PATCH] Start of new Nix command-line interface --- .gitignore | 3 - Makefile | 2 +- src/libmain/common-args.cc | 1 + src/libmain/common-args.hh | 1 + src/libutil/args.hh | 2 + src/nix-hash/local.mk | 7 -- src/nix-hash/nix-hash.cc | 63 ----------------- src/nix/command.cc | 65 +++++++++++++++++ src/nix/command.hh | 59 ++++++++++++++++ src/nix/hash.cc | 140 +++++++++++++++++++++++++++++++++++++ src/nix/legacy.cc | 7 ++ src/nix/legacy.hh | 22 ++++++ src/nix/local.mk | 9 +++ src/nix/main.cc | 55 +++++++++++++++ 14 files changed, 362 insertions(+), 74 deletions(-) delete mode 100644 src/nix-hash/local.mk delete mode 100644 src/nix-hash/nix-hash.cc create mode 100644 src/nix/command.cc create mode 100644 src/nix/command.hh create mode 100644 src/nix/hash.cc create mode 100644 src/nix/legacy.cc create mode 100644 src/nix/legacy.hh create mode 100644 src/nix/local.mk create mode 100644 src/nix/main.cc diff --git a/.gitignore b/.gitignore index 94067256b..de8e9354f 100644 --- a/.gitignore +++ b/.gitignore @@ -73,9 +73,6 @@ Makefile.config # /src/nix-env/ /src/nix-env/nix-env -# /src/nix-hash/ -/src/nix-hash/nix-hash - # /src/nix-instantiate/ /src/nix-instantiate/nix-instantiate diff --git a/Makefile b/Makefile index 3a204de88..39870af75 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ makefiles = \ src/libstore/local.mk \ src/libmain/local.mk \ src/libexpr/local.mk \ - src/nix-hash/local.mk \ + src/nix/local.mk \ src/nix-store/local.mk \ src/nix-instantiate/local.mk \ src/nix-env/local.mk \ diff --git a/src/libmain/common-args.cc b/src/libmain/common-args.cc index 54dbb1da3..9219f380c 100644 --- a/src/libmain/common-args.cc +++ b/src/libmain/common-args.cc @@ -4,6 +4,7 @@ namespace nix { MixCommonArgs::MixCommonArgs(const string & programName) + : programName(programName) { mkFlag('v', "verbose", "increase verbosity level", []() { verbosity = (Verbosity) (verbosity + 1); diff --git a/src/libmain/common-args.hh b/src/libmain/common-args.hh index 62657e0d0..2c0d71edd 100644 --- a/src/libmain/common-args.hh +++ b/src/libmain/common-args.hh @@ -6,6 +6,7 @@ namespace nix { struct MixCommonArgs : virtual Args { + string programName; MixCommonArgs(const string & programName); }; diff --git a/src/libutil/args.hh b/src/libutil/args.hh index c61a2b02a..0c7729d0b 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -145,6 +145,8 @@ public: *dest = ss; }}); } + + friend class MultiCommand; }; Strings argvToStrings(int argc, char * * argv); diff --git a/src/nix-hash/local.mk b/src/nix-hash/local.mk deleted file mode 100644 index 7c290ca84..000000000 --- a/src/nix-hash/local.mk +++ /dev/null @@ -1,7 +0,0 @@ -programs += nix-hash - -nix-hash_DIR := $(d) - -nix-hash_SOURCES := $(d)/nix-hash.cc - -nix-hash_LIBS = libmain libstore libutil libformat diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc deleted file mode 100644 index 8035162ae..000000000 --- a/src/nix-hash/nix-hash.cc +++ /dev/null @@ -1,63 +0,0 @@ -#include "hash.hh" -#include "shared.hh" - -#include - -using namespace nix; - - -int main(int argc, char * * argv) -{ - HashType ht = htMD5; - bool flat = false; - bool base32 = false; - bool truncate = false; - enum { opHash, opTo32, opTo16 } op = opHash; - - Strings ss; - - return handleExceptions(argv[0], [&]() { - initNix(); - - parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) { - if (*arg == "--help") - showManPage("nix-hash"); - else if (*arg == "--version") - printVersion("nix-hash"); - else if (*arg == "--flat") flat = true; - else if (*arg == "--base32") base32 = true; - else if (*arg == "--truncate") truncate = true; - else if (*arg == "--type") { - string s = getArg(*arg, arg, end); - ht = parseHashType(s); - if (ht == htUnknown) - throw UsageError(format("unknown hash type ‘%1%’") % s); - } - else if (*arg == "--to-base16") op = opTo16; - else if (*arg == "--to-base32") op = opTo32; - else if (*arg != "" && arg->at(0) == '-') - return false; - else - ss.push_back(*arg); - return true; - }); - - if (op == opHash) { - for (auto & i : ss) { - Hash h = flat ? hashFile(ht, i) : hashPath(ht, i).first; - if (truncate && h.hashSize > 20) h = compressHash(h, 20); - std::cout << format("%1%\n") % - (base32 ? printHash32(h) : printHash(h)); - } - } - - else { - for (auto & i : ss) { - Hash h = parseHash16or32(ht, i); - std::cout << format("%1%\n") % - (op == opTo16 ? printHash(h) : printHash32(h)); - } - } - }); -} - diff --git a/src/nix/command.cc b/src/nix/command.cc new file mode 100644 index 000000000..698863349 --- /dev/null +++ b/src/nix/command.cc @@ -0,0 +1,65 @@ +#include "command.hh" +#include "store-api.hh" + +namespace nix { + +Commands * RegisterCommand::commands = 0; + +MultiCommand::MultiCommand(const Commands & _commands) + : commands(_commands) +{ + expectedArgs.push_back(ExpectedArg{"command", 1, [=](Strings ss) { + assert(!command); + auto i = commands.find(ss.front()); + if (i == commands.end()) + throw UsageError(format("‘%1%’ is not a recognised command") % ss.front()); + command = i->second; + }}); +} + +void MultiCommand::printHelp(const string & programName, std::ostream & out) +{ + if (command) { + command->printHelp(programName + " " + command->name(), out); + return; + } + + out << "Usage: " << programName << " ... ...\n"; + + out << "\n"; + out << "Common flags:\n"; + printFlags(out); + + out << "\n"; + out << "Available commands:\n"; + + Table2 table; + for (auto & command : commands) + table.push_back(std::make_pair(command.second->name(), command.second->description())); + printTable(out, table); + + out << "\n"; + out << "For full documentation, run ‘man " << programName << "’ or ‘man " << programName << "-’.\n"; +} + +bool MultiCommand::processFlag(Strings::iterator & pos, Strings::iterator end) +{ + if (Args::processFlag(pos, end)) return true; + if (command && command->processFlag(pos, end)) return true; + return false; +} + +bool MultiCommand::processArgs(const Strings & args, bool finish) +{ + if (command) + return command->processArgs(args, finish); + else + return Args::processArgs(args, finish); +} + +void StoreCommand::run() +{ + run(openStore(reserveSpace)); +} + +} diff --git a/src/nix/command.hh b/src/nix/command.hh new file mode 100644 index 000000000..a84721ccf --- /dev/null +++ b/src/nix/command.hh @@ -0,0 +1,59 @@ +#pragma once + +#include "args.hh" + +namespace nix { + +/* A command is an argument parser that can be executed by calling its + run() method. */ +struct Command : virtual Args +{ + virtual std::string name() = 0; + virtual void prepare() { }; + virtual void run() = 0; +}; + +class Store; + +/* A command that require a Nix store. */ +struct StoreCommand : virtual Command +{ + bool reserveSpace; + StoreCommand(bool reserveSpace = true) + : reserveSpace(reserveSpace) { }; + void run() override; + virtual void run(ref) = 0; +}; + +typedef std::map> Commands; + +/* An argument parser that supports multiple subcommands, + i.e. ‘ ’. */ +struct MultiCommand : virtual Args +{ + Commands commands; + + std::shared_ptr command; + + MultiCommand(const Commands & commands); + + void printHelp(const string & programName, std::ostream & out) override; + + bool processFlag(Strings::iterator & pos, Strings::iterator end) override; + + bool processArgs(const Strings & args, bool finish) override; +}; + +/* A helper class for registering commands globally. */ +struct RegisterCommand +{ + static Commands * commands; + + RegisterCommand(ref command) + { + if (!commands) commands = new Commands; + commands->emplace(command->name(), command); + } +}; + +} diff --git a/src/nix/hash.cc b/src/nix/hash.cc new file mode 100644 index 000000000..fff945bd3 --- /dev/null +++ b/src/nix/hash.cc @@ -0,0 +1,140 @@ +#include "command.hh" +#include "hash.hh" +#include "legacy.hh" +#include "shared.hh" + +using namespace nix; + +struct CmdHash : Command +{ + enum Mode { mFile, mPath }; + Mode mode; + bool base32 = false; + bool truncate = false; + HashType ht = htSHA512; + Strings paths; + + CmdHash(Mode mode) : mode(mode) + { + mkFlag(0, "base32", "print hash in base-32", &base32); + mkFlag(0, "base16", "print hash in base-16", &base32, false); + mkHashTypeFlag("type", &ht); + expectArgs("paths", &paths); + } + + std::string name() override + { + return mode == mFile ? "hash-file" : "hash-path"; + } + + std::string description() override + { + return mode == mFile + ? "print cryptographic hash of a regular file" + : "print cryptographic hash of the NAR serialisation of a path"; + } + + void run() override + { + for (auto path : paths) { + Hash h = mode == mFile ? hashFile(ht, path) : hashPath(ht, path).first; + if (truncate && h.hashSize > 20) h = compressHash(h, 20); + std::cout << format("%1%\n") % + (base32 ? printHash32(h) : printHash(h)); + } + } +}; + +static RegisterCommand r1(make_ref(CmdHash::mFile)); +static RegisterCommand r2(make_ref(CmdHash::mPath)); + +struct CmdToBase : Command +{ + bool toBase32; + HashType ht = htSHA512; + Strings args; + + CmdToBase(bool toBase32) : toBase32(toBase32) + { + mkHashTypeFlag("type", &ht); + expectArgs("strings", &args); + } + + std::string name() override + { + return toBase32 ? "to-base32" : "to-base16"; + } + + std::string description() override + { + return toBase32 + ? "convert a hash to base-32 representation" + : "convert a hash to base-32 representation"; + } + + void run() override + { + for (auto s : args) { + Hash h = parseHash16or32(ht, s); + std::cout << format("%1%\n") % + (toBase32 ? printHash32(h) : printHash(h)); + } + } +}; + +static RegisterCommand r3(make_ref(false)); +static RegisterCommand r4(make_ref(true)); + +/* Legacy nix-hash command. */ +static int compatNixHash(int argc, char * * argv) +{ + HashType ht = htMD5; + bool flat = false; + bool base32 = false; + bool truncate = false; + enum { opHash, opTo32, opTo16 } op = opHash; + Strings ss; + + parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) { + if (*arg == "--help") + showManPage("nix-hash"); + else if (*arg == "--version") + printVersion("nix-hash"); + else if (*arg == "--flat") flat = true; + else if (*arg == "--base32") base32 = true; + else if (*arg == "--truncate") truncate = true; + else if (*arg == "--type") { + string s = getArg(*arg, arg, end); + ht = parseHashType(s); + if (ht == htUnknown) + throw UsageError(format("unknown hash type ‘%1%’") % s); + } + else if (*arg == "--to-base16") op = opTo16; + else if (*arg == "--to-base32") op = opTo32; + else if (*arg != "" && arg->at(0) == '-') + return false; + else + ss.push_back(*arg); + return true; + }); + + if (op == opHash) { + CmdHash cmd(flat ? CmdHash::mFile : CmdHash::mPath); + cmd.ht = ht; + cmd.base32 = base32; + cmd.truncate = truncate; + cmd.paths = ss; + cmd.run(); + } + + else { + CmdToBase cmd(op == opTo32); + cmd.args = ss; + cmd.ht = ht; + cmd.run(); + } + + return 0; +} + +static RegisterLegacyCommand s1("nix-hash", compatNixHash); diff --git a/src/nix/legacy.cc b/src/nix/legacy.cc new file mode 100644 index 000000000..6df09ee37 --- /dev/null +++ b/src/nix/legacy.cc @@ -0,0 +1,7 @@ +#include "legacy.hh" + +namespace nix { + +RegisterLegacyCommand::Commands * RegisterLegacyCommand::commands = 0; + +} diff --git a/src/nix/legacy.hh b/src/nix/legacy.hh new file mode 100644 index 000000000..b67b70eb5 --- /dev/null +++ b/src/nix/legacy.hh @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +namespace nix { + +typedef std::function MainFunction; + +struct RegisterLegacyCommand +{ + typedef std::map Commands; + static Commands * commands; + + RegisterLegacyCommand(const std::string & name, MainFunction fun) + { + if (!commands) commands = new Commands; + (*commands)[name] = fun; + } +}; + +} diff --git a/src/nix/local.mk b/src/nix/local.mk new file mode 100644 index 000000000..f6e7073b6 --- /dev/null +++ b/src/nix/local.mk @@ -0,0 +1,9 @@ +programs += nix + +nix_DIR := $(d) + +nix_SOURCES := $(wildcard $(d)/*.cc) + +nix_LIBS = libexpr libmain libstore libutil libformat + +$(eval $(call install-symlink, nix, $(bindir)/nix-hash)) diff --git a/src/nix/main.cc b/src/nix/main.cc new file mode 100644 index 000000000..20d3ea5c2 --- /dev/null +++ b/src/nix/main.cc @@ -0,0 +1,55 @@ +#include + +#include "command.hh" +#include "common-args.hh" +#include "eval.hh" +#include "globals.hh" +#include "legacy.hh" +#include "shared.hh" +#include "store-api.hh" + +namespace nix { + +struct NixArgs : virtual MultiCommand, virtual MixCommonArgs +{ + NixArgs() : MultiCommand(*RegisterCommand::commands), MixCommonArgs("nix") + { + mkFlag('h', "help", "show usage information", [=]() { + printHelp(programName, std::cout); + throw Exit(); + }); + + mkFlag(0, "version", "show version information", std::bind(printVersion, programName)); + } +}; + +void mainWrapped(int argc, char * * argv) +{ + initNix(); + initGC(); + + string programName = baseNameOf(argv[0]); + + { + auto legacy = (*RegisterLegacyCommand::commands)[programName]; + if (legacy) return legacy(argc, argv); + } + + NixArgs args; + + args.parseCmdline(argvToStrings(argc, argv)); + + assert(args.command); + + args.command->prepare(); + args.command->run(); +} + +} + +int main(int argc, char * * argv) +{ + return nix::handleExceptions(argv[0], [&]() { + nix::mainWrapped(argc, argv); + }); +}