UNPKG

masq

Version:

A simple local dns server extracted from Pow

327 lines (222 loc) 12.9 kB
<!DOCTYPE html> <html> <head> <title>command.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="configuration.html"> configuration.coffee </a> <a class="source" href="daemon.html"> daemon.coffee </a> <a class="source" href="dns_server.html"> dns_server.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="installer.html"> installer.coffee </a> <a class="source" href="logger.html"> logger.coffee </a> <a class="source" href="utils.html"> utils.coffee </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>command.coffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>The <code>command</code> module is loaded when the <code>masq</code> binary runs. It parses any command-line arguments and determines whether to install Pow’s configuration files or start the daemon itself.</p> </div> <div class="content"><div class='highlight'><pre> {Daemon, Configuration, Installer} = <span class="hljs-built_in">require</span> <span class="hljs-string">".."</span></pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Set the process’s title to <code>masq</code> so it’s easier to find in <code>ps</code>, <code>top</code>, Activity Monitor, and so on.</p> </div> <div class="content"><div class='highlight'><pre>process.title = <span class="hljs-string">"masq"</span></pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Print valid command-line arguments and exit with a non-zero exit code if invalid arguments are passed to the <code>masq</code> binary.</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">usage</span> = -&gt;</span> <span class="hljs-built_in">console</span>.error <span class="hljs-string">"usage: masq [--print-config | --install-local | --install-system [--dry-run]]"</span> process.exit <span class="hljs-number">-1</span></pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Start by loading the user configuration from <code>~/.masqconfig</code>, if it exists. The user configuration affects both the installer and the daemon.</p> </div> <div class="content"><div class='highlight'><pre>Configuration.getUserConfiguration (err, configuration) -&gt; <span class="hljs-keyword">throw</span> err <span class="hljs-keyword">if</span> err printConfig = <span class="hljs-literal">false</span> dryRun = <span class="hljs-literal">false</span> createInstaller = <span class="hljs-literal">null</span> <span class="hljs-keyword">for</span> arg <span class="hljs-keyword">in</span> process.argv.slice(<span class="hljs-number">2</span>)</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Set a flag if <code>--print-config</code> is requested.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> arg <span class="hljs-keyword">is</span> <span class="hljs-string">"--print-config"</span> printConfig = <span class="hljs-literal">true</span></pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Cache the factory method for creating a local or system installer if necessary.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> arg <span class="hljs-keyword">is</span> <span class="hljs-string">"--install-local"</span> createInstaller = Installer.getLocalInstaller <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> arg <span class="hljs-keyword">is</span> <span class="hljs-string">"--install-system"</span> createInstaller = Installer.getSystemInstaller</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Set a flag if a dry run is requested.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> arg <span class="hljs-keyword">is</span> <span class="hljs-string">"--dry-run"</span> dryRun = <span class="hljs-literal">true</span> <span class="hljs-keyword">else</span> usage()</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Abort if a dry run is requested without installing anything.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> dryRun <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> createInstaller usage()</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Print out the current configuration in a format that can be evaluated by a shell script (<code>eval $(pow --print-config)</code>).</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> printConfig <span class="hljs-function"> <span class="hljs-title">underscore</span> = <span class="hljs-params">(string)</span> -&gt;</span> string.replace <span class="hljs-regexp">/(.)([A-Z])/g</span>, <span class="hljs-function"><span class="hljs-params">(match, left, right)</span> -&gt;</span> left + <span class="hljs-string">"_"</span> + right.toLowerCase() <span class="hljs-function"> <span class="hljs-title">shellEscape</span> = <span class="hljs-params">(string)</span> -&gt;</span> <span class="hljs-string">"'"</span> + string.toString().replace(<span class="hljs-regexp">/'/g</span>, <span class="hljs-string">"'\\''"</span>) + <span class="hljs-string">"'"</span> <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">of</span> configuration.toJSON() <span class="hljs-built_in">console</span>.log <span class="hljs-string">"MASQ_"</span> + underscore(key).toUpperCase() + <span class="hljs-string">"="</span> + shellEscape(value)</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Create the installer, passing in our loaded configuration.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> createInstaller <span class="hljs-keyword">debugger</span> installer = createInstaller configuration</pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>If a dry run was requested, check to see whether any files need to be installed with root privileges. If yes, exit with a status of 1. If no, exit with a status of 0.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> dryRun installer.needsRootPrivileges (needsRoot) -&gt; exitCode = <span class="hljs-keyword">if</span> needsRoot <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span> installer.getStaleFiles (files) -&gt; util.puts file.path <span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> files process.exit exitCode</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Otherwise, install all the requested files, printing the full path of each installed file to stdout.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> installer.install (err) -&gt; <span class="hljs-keyword">throw</span> err <span class="hljs-keyword">if</span> err</pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Start up the Pow daemon if no arguments were passed. Terminate the process if the daemon requests a restart.</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">else</span> daemon = <span class="hljs-keyword">new</span> Daemon configuration daemon.<span class="hljs-literal">on</span> <span class="hljs-string">"restart"</span>, <span class="hljs-function">-&gt;</span> process.exit() daemon.start()</pre></div></div> </li> </ul> </div> </body> </html>