about summary refs log tree commit diff
path: root/src/download-via-ssh
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T01·50+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-13T01·50+0200
commit47e185847e729d49e6aa376e8299fd66ef834a0a (patch)
tree6abfc9e9d5744e78b123c73182c30253c9a39681 /src/download-via-ssh
parent5bed74d1b0acd8d8083fb82a31d907ad2348a91b (diff)
Refactor option handling
Diffstat (limited to 'src/download-via-ssh')
-rw-r--r--src/download-via-ssh/download-via-ssh.cc80
1 files changed, 37 insertions, 43 deletions
diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc
index 688fb523a77a..ac0f5826dd28 100644
--- a/src/download-via-ssh/download-via-ssh.cc
+++ b/src/download-via-ssh/download-via-ssh.cc
@@ -91,55 +91,49 @@ static void query(std::pair<FdSink, FdSource> & pipes)
 }
 
 
-void run(Strings args)
+int main(int argc, char * * argv)
 {
-    if (args.empty())
-        throw UsageError("download-via-ssh requires an argument");
+    return handleExceptions(argv[0], [&]() {
+        initNix();
 
-    if (settings.sshSubstituterHosts.empty())
-        return;
+        if (argc < 2)
+            throw UsageError("download-via-ssh requires an argument");
 
-    std::cout << std::endl;
-
-    /* Pass on the location of the daemon client's SSH authentication
-       socket. */
-    string sshAuthSock = settings.get("ssh-auth-sock", "");
-    if (sshAuthSock != "") setenv("SSH_AUTH_SOCK", sshAuthSock.c_str(), 1);
+        if (settings.sshSubstituterHosts.empty())
+            return;
 
-    string host = settings.sshSubstituterHosts.front();
-    std::pair<FdSink, FdSource> pipes = connect(host);
-
-    /* Exchange the greeting */
-    writeInt(SERVE_MAGIC_1, pipes.first);
-    pipes.first.flush();
-    unsigned int magic = readInt(pipes.second);
-    if (magic != SERVE_MAGIC_2)
-        throw Error("protocol mismatch");
-    readInt(pipes.second); // Server version, unused for now
-    writeInt(SERVE_PROTOCOL_VERSION, pipes.first);
-    pipes.first.flush();
+        std::cout << std::endl;
 
-    Strings::iterator i = args.begin();
-    if (*i == "--query")
-        query(pipes);
-    else if (*i == "--substitute")
-        if (args.size() != 3)
-            throw UsageError("download-via-ssh: --substitute takes exactly two arguments");
-        else {
-            Path storePath = *++i;
-            Path destPath = *++i;
+        /* Pass on the location of the daemon client's SSH
+           authentication socket. */
+        string sshAuthSock = settings.get("ssh-auth-sock", "");
+        if (sshAuthSock != "") setenv("SSH_AUTH_SOCK", sshAuthSock.c_str(), 1);
+
+        string host = settings.sshSubstituterHosts.front();
+        std::pair<FdSink, FdSource> pipes = connect(host);
+
+        /* Exchange the greeting */
+        writeInt(SERVE_MAGIC_1, pipes.first);
+        pipes.first.flush();
+        unsigned int magic = readInt(pipes.second);
+        if (magic != SERVE_MAGIC_2)
+            throw Error("protocol mismatch");
+        readInt(pipes.second); // Server version, unused for now
+        writeInt(SERVE_PROTOCOL_VERSION, pipes.first);
+        pipes.first.flush();
+
+        string arg = argv[1];
+        if (arg == "--query")
+            query(pipes);
+        else if (arg == "--substitute") {
+            if (argc != 4)
+                throw UsageError("download-via-ssh: --substitute takes exactly two arguments");
+            Path storePath = argv[2];
+            Path destPath = argv[3];
             printMsg(lvlError, format("downloading `%1%' via SSH from `%2%'...") % storePath % host);
             substitute(pipes, storePath, destPath);
         }
-    else
-        throw UsageError(format("download-via-ssh: unknown command `%1%'") % *i);
-}
-
-
-void printHelp()
-{
-    std::cerr << "Usage: download-via-ssh --query|--substitute store-path dest-path" << std::endl;
+        else
+            throw UsageError(format("download-via-ssh: unknown command `%1%'") % arg);
+    });
 }
-
-
-string programId = "download-via-ssh";