<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrei Pavel</title>
    <description>The latest articles on DEV Community by Andrei Pavel (@andreipavel0).</description>
    <link>https://dev.to/andreipavel0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F238698%2F5f2d85c5-06da-4868-b448-ef63238e3c9b.jpg</url>
      <title>DEV Community: Andrei Pavel</title>
      <link>https://dev.to/andreipavel0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andreipavel0"/>
    <language>en</language>
    <item>
      <title>bash-boilerplate: enriching bash</title>
      <dc:creator>Andrei Pavel</dc:creator>
      <pubDate>Wed, 18 Dec 2019 12:13:11 +0000</pubDate>
      <link>https://dev.to/andreipavel0/bash-boilerplate-enriching-bash-2693</link>
      <guid>https://dev.to/andreipavel0/bash-boilerplate-enriching-bash-2693</guid>
      <description>&lt;p&gt;For when you want your humble bash script to have a little more umph!, have at these hiqh level features that can be easily embedded in any project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/andrei-pavel/bash-boilerplate"&gt;https://github.com/andrei-pavel/bash-boilerplate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/dcAMPgFA7EWYSv16Tva7Jj5kD"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k0t_Pf7W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://asciinema.org/a/dcAMPgFA7EWYSv16Tva7Jj5kD.svg" alt="asciicast"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Libraries
&lt;/h2&gt;

&lt;p&gt;All libraries are found inside &lt;code&gt;lib&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Import any library with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SCRIPT_PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;dirname&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;readlink&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;0&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;SCRIPT_PATH&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/lib/&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;library&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;They all have import guards to make sure no library is imported twice.&lt;/p&gt;

&lt;h3&gt;
  
  
  header
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;☑ &lt;code&gt;set -euo pipefail&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;print-usage&lt;/code&gt; function to print all &lt;code&gt;${ARGUMENTS}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;☑ default parameters &lt;code&gt;-c|--config $config_yaml&lt;/code&gt; reads the given YAML file and
converts all YAML variables to shell variables&lt;/li&gt;
&lt;li&gt;☑ default parameters &lt;code&gt;-d|--debug&lt;/code&gt; calls &lt;code&gt;set -x&lt;/code&gt; for debug mode&lt;/li&gt;
&lt;li&gt;☑ default parameters &lt;code&gt;-h|--help&lt;/code&gt; calls &lt;code&gt;print-usage()&lt;/code&gt; automatically&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;additional&lt;/code&gt; function to print all given arguments other than the default
ones above&lt;/li&gt;
&lt;li&gt;☑ ANSII colors &lt;code&gt;${BLACK}&lt;/code&gt;, &lt;code&gt;${RED}&lt;/code&gt;, &lt;code&gt;${GREEN}&lt;/code&gt;, &lt;code&gt;${YELLOW}&lt;/code&gt;, &lt;code&gt;${BLUE}&lt;/code&gt;,
&lt;code&gt;${PURPLE}&lt;/code&gt;, &lt;code&gt;${CYAN}&lt;/code&gt;, &lt;code&gt;${WHITE}&lt;/code&gt; are exported&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;${SCRIPT_PATH}&lt;/code&gt;, &lt;code&gt;${LIB_PATH}&lt;/code&gt; paths are exported&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  spinner
&lt;/h3&gt;

&lt;p&gt;The best bash spinner out there&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;☑ &lt;code&gt;start-spinner ${text}&lt;/code&gt; function to start a spinner with specified &lt;code&gt;${text}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;stop-spinner ${exit_code}&lt;/code&gt; function to stop the spinner with success if
&lt;code&gt;${exit_code}&lt;/code&gt; is 0, with failure otherwise&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;disable-spinners&lt;/code&gt; function to ignore commands that start and stop spinners&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;enable-spinners&lt;/code&gt; function [default]&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;disable-verbose&lt;/code&gt; function to print output on stopped spinner only if exit
code was non-null [default]&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;enable-verbose&lt;/code&gt; function to print output on stopped spinner all the time&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;configure-spinner-output [full-output|stdout-only|stderr-only|no-output]&lt;/code&gt;
function&lt;/li&gt;
&lt;li&gt;☐ nested spinners&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  traps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;☑ &lt;code&gt;traps [--force|--return-code-only] ${custom_trap}&lt;/code&gt; function to set what
runs after catching HUP, INT, QUIT, KILL, TERM, EXIT signals

&lt;ul&gt;
&lt;li&gt;usually captures return code, disables traps so that they don't run
recursively, stops any spinners and then runs the &lt;code&gt;${custom_trap}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--force&lt;/code&gt; makes it so that only &lt;code&gt;${custom_trap}&lt;/code&gt; is run&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--return-code-only&lt;/code&gt; skips over stopping the spinners which is needed in
case you're not using spinners&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  utils
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;☑ &lt;code&gt;confirm [${custom_question}]&lt;/code&gt; function to ask the user a yes or no
question&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;mandatory ${variable}&lt;/code&gt; function to check if &lt;code&gt;${variable}&lt;/code&gt; is set, &lt;code&gt;exit 2&lt;/code&gt;
otherwise&lt;/li&gt;
&lt;li&gt;☑ &lt;code&gt;mandatory-command ${command}&lt;/code&gt; function to check if &lt;code&gt;${command}&lt;/code&gt; is
available, &lt;code&gt;exit 3&lt;/code&gt; otherwise&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  yaml-to-env
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;☑ Simply importing it with
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;SCRIPT_PATH&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/lib/yaml-to-env [&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;config_yaml&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;]"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;will read &lt;code&gt;${config_yaml}&lt;/code&gt; or &lt;code&gt;./config.yaml&lt;/code&gt; if it is not provided and will&lt;br&gt;
convert all YAML variables to shell variables just like&lt;br&gt;
&lt;code&gt;-c|--config $config_yaml&lt;/code&gt;, but programatically instead of user-requested.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;See the &lt;code&gt;hello-world&lt;/code&gt; example which showcases the different uses of these&lt;br&gt;
arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Usage: hello-world &lt;span class="o"&gt;{{&lt;/span&gt;options&lt;span class="o"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;{{&lt;/span&gt;arguments&lt;span class="o"&gt;}}&lt;/span&gt;
Options:
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-c&lt;/span&gt;|--config &lt;span class="nv"&gt;$config_yaml&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;   Reads from a YAML configuration and converts all
                               key-value pairs with literal values to
                               environment variables.
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-d&lt;/span&gt;|--debug]                 Enables debug mode, showing every executed
                               statement.
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-h&lt;/span&gt;|--help]                  Prints usage &lt;span class="o"&gt;(&lt;/span&gt;this text&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Arguments:
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-a&lt;/span&gt;|--approval]              whether to ask user &lt;span class="k"&gt;for &lt;/span&gt;approval
  &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="nv"&gt;$count&lt;/span&gt;                    print &lt;span class="nv"&gt;$count&lt;/span&gt; &lt;span class="nb"&gt;times&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;-y&lt;/span&gt;|--yaml &lt;span class="nv"&gt;$yaml&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;            specify path to the configuration file
  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$string&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;                    something other than &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt; to print
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>bash</category>
      <category>boilerplate</category>
      <category>spinner</category>
      <category>unix</category>
    </item>
  </channel>
</rss>
