DEV Community

Alexey Melezhik
Alexey Melezhik

Posted on

Easy Bash Scripts Configuration by Sparrow Framework

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   
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And then run the script:

$ strun --param message="hello world"
Enter fullscreen mode Exit fullscreen mode

The output will be:

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And then:

$ bash hello.bash "hello world" 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And then:

$ strun
Enter fullscreen mode Exit fullscreen mode

The output will be:

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

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"
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And one for the default configuration:

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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. 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"

Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

You can even override default configuration partly, this way:

$ sparrow plg run user-greeting --param user.id=Corbie
Enter fullscreen mode Exit fullscreen mode

Or this one:

$ sparrow task ini utils/greeting

user:
  id: Сorbie 

$ sparrow task run utils/greeting
Enter fullscreen mode Exit fullscreen mode

Or even like that:

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

Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
melezhik profile image
Alexey Melezhik

There is an interesting discussion on Reddit/Bash based on this post - reddit.com/r/bash/comments/7aws33/...