Easy Bash Scripts Configuration by Sparrow Framework
Tags: bash, linux, perl, sparrow
Published: 2017-11-05 06:42:18

---
title: Easy Bash Scripts Configuration by Sparrow Framework
published: true
description: How to configure Bash scripts by using Sparrow framework 
tags: Bash, linux, Perl, sparrow
---

Bash is not easy when it comes to configuration of scripts. A few cases come to my mind immediately:

- Passing nested, Hash-like structures as input parameters
- Configurations via declarative languages like JSON/YAML

Well it is hard when in Bash, unless you use Sparrow for a Bash scripts development. 

Following are some occasional examples to prove.


# Installing Sparrow

As usual go with:

```
$ cpanm Sparrow --notest --q   
```

# Developing Bash script

Following Sparrow rules it should be file called "story.bash". Let's do something useful, for example echoing an input parameter:


```
$ nano story.bash
#!/bin/bash
message=$(config message)
echo you say $message
```

And then run the script:


```
$ strun --param message="hello world"
```

The output will be:

```
2017-11-04 22:27:17 :  [path] /
you say hello world
ok      scenario succeeded
STATUS  SUCCEED

```

It does not look that impressive so far, we might have done the same with a "none-sparrow" Bash script:


```
$ nano hello.bash
#!/bin/bash
echo you say $1
``` 

And then:


```
$ bash hello.bash "hello world" 
```

However, lets move on, advantages of Sparrow to be revealed soon ...  

 
# Defining defaults

Say, we want to define default values for configuration parameter.

In Sparrow you just create file called "suite.yaml" to do this:


```
$ nano suite.yaml

message: Hello Sparrow
```

And then:

```
$ strun
```

The output will be:

```
2017-11-04 22:34:50 :  [path] /
you say Hello Sparrow
ok      scenario succeeded
STATUS  SUCCEED
```


# Nested parameters

Let's make things a bit more complicated and say we want pass "message" parameter within complex structure:

```
{
   "user" : {
      "id" : "Bash" 
      "message" : "Hello Bash"
    }
}
```

You'll need a trivial code changes to accept this new requirement:


One for the Bash script:

```
$ nano story.bash
#!/bin/bash
user_id=$(config user.id)
message=$(config user.message)
echo $user_id say $message
```

And one for the default configuration:


```
$ nano suite.yaml
user:
  id: Sparrow 
  message: Hello Sparrow
  
```

Now let's give it a run. 

* With default parameters:

```
$ strun
2017-11-04 22:42:13 :  [path] /
Sparrow say Hello Sparrow
ok      scenario succeeded
STATUS  SUCCEED

```

* And with some user's input:

```
$ strun --param user.id=Bash --param user.message="Hello Bash"
2017-11-04 22:43:01 :  [path] /
Bash say Hello Bash
ok      scenario succeeded
STATUS  SUCCEED
```

We've just added a bit and pieces of code and it's still pure Bash but we manage to handle nested, Hash-like structures, neat! :) 


BTW the level of nested structure is not limited, an imaginative case would be something like that:


```
$ strun --param user.language.dialects=Shell --param user.message="Hello Shell"

``` 

And so on ...


# Documentation

If you reach here, I assume you're interested in the topic, to delve into details - use pretty thorough documentation of Sparrow SDK called [Outthentic](https://github.com/melezhik/outthentic). Here you'll find a lot of examples and ways how to create and configure Bash ( not only Bash indeed ) scripts.


# Sparrow plugins

Say we want to upload our Bash script as Sparrow plugin called "user-greeting" so that other can use it. Unsurprisingly Sparrow plugins "inherit" the same way of configuration we've seen so far.  Following are examples:


* As command line parameters:


```
$ sparrow plg install user-greeting
$ sparrow plg run user-greeting --param user.id=Bash --param user.message="Hello Bash"

``` 

* As sparrow tasks:

```
$ sparrow plg install message
$ sparrow project create utils
$ sparrow task add utils greeting user-greeting
$ sparrow task ini utils/greeting

user:
  id: Ruby 
  message: Hello Ruby

$ sparrow task run utils/greeting
```

You can even override default configuration _partly_, this way:

```
$ sparrow plg run user-greeting --param user.id=Corbie
```

Or this one:

```
$ sparrow task ini utils/greeting

user:
  id: Ð¡orbie 

$ sparrow task run utils/greeting
```

Or even like that:

```
$ sparrow task ini utils/greeting --param --param user.id=Raven

```
 

# Conclusion

Sparrow provides scripts developers with our configuration needs. It's just a piece of cake to build flexible configuration for your Bash scripts with adding just a minimum of code.