Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
dry-wit
Bash framework
Jose San Leandro @rydnr
OSOCO
Contents
1 Introduction
2 dry-wit approach
3 dry-wit hooks
4 dry-wit API
5 Try it
Bash
In theory
• Powerful language.
• Conventions improve
reusability and
maintainability.
• Great fit for Unix and OSs
wit...
Bash
In theory
• Powerful language.
• Conventions improve
reusability and
maintainability.
• Great fit for Unix and OSs
wit...
dry-wit: Features
Features
1 Provides a consistent way to structure scripts.
2 Manages required dependencies.
3 Declarativ...
Usage / Help
usage()
function usage() {
cat <<EOF
Does this and that...
Where:
* myflag: changes the behavior somehow.
* p...
Input: checking
checkInput()
function checkInput() {
local _flags=$(extractFlags $@);
logDebug -n "Checking input";
for _f...
Input: parsing
parseInput()
function parseInput() {
local _flags=$(extractFlags $@);
for _flag in ${_flags}; do
case ${_fl...
Script requirements
Dependencies
function checkRequirements() {
checkReq docker DOCKER_NOT_INSTALLED;
checkReq realpath RE...
Error messages
defineErrors()
function defineErrors() {
export INVALID_OPTION="Unrecognized option";
export DOCKER_NOT_INST...
main()
main()
function main() {
// Focus on the logic itself.
// Forget about defensive programming
// regarding input var...
Logging
logging
logInfo -n "Calculating next prime number ...";
...
if [ $? -eq 0 ]; then
logInfoResult SUCCESS "done";
el...
Temporary files
API functions
1 Functions to create temporary files or folders.
2 dry-wit takes cares of cleaning them up af...
Environment variables: Declaration (1/3)
DSL for declaring environment variables
1 Declared in [script].inc.sh.
2 Safe to ...
Environment variables: Overridding (2/3)
DSL for overridding default values
1 Declared in .[script].inc.sh.
2 Not always s...
Environment variables: Overridding (3/3)
Environment value overridden from the command line
MYPASSWORD="abc123" ./script.s...
Free as in freedom
You’re welcome to try, fork, improve, even criticize!
https://github.com/rydnr/dry-wit
dry-wit Try it 1...
Próxima SlideShare
Cargando en…5
×

Dry-wit Overview

575 visualizaciones

Publicado el

Dry-wit is a bash framework useful when writing new scripts. It provides a common layout and a toolbox of functions to help writing shell scripts from scratch.

Publicado en: Software
  • Inicia sesión para ver los comentarios

  • Sé el primero en recomendar esto

Dry-wit Overview

  1. 1. dry-wit Bash framework Jose San Leandro @rydnr OSOCO
  2. 2. Contents 1 Introduction 2 dry-wit approach 3 dry-wit hooks 4 dry-wit API 5 Try it
  3. 3. Bash In theory • Powerful language. • Conventions improve reusability and maintainability. • Great fit for Unix and OSs with strong command-line focus. dry-wit Introduction 3 / 16
  4. 4. Bash In theory • Powerful language. • Conventions improve reusability and maintainability. • Great fit for Unix and OSs with strong command-line focus. In practice • Easy to start with. • Difficult to master. • Write once, use twice, dispose. • Expensive maintenance. • Hardly reusable. dry-wit Introduction 3 / 16
  5. 5. dry-wit: Features Features 1 Provides a consistent way to structure scripts. 2 Manages required dependencies. 3 Declarative approach for dealing with errors (Output message + error code). 4 Handles parameters, flags, and environment variables. 5 API for creating temporary files, logging, etc. 6 Composability: scripts can be injected into others. dry-wit dry-wit approach 4 / 16
  6. 6. Usage / Help usage() function usage() { cat <<EOF Does this and that... Where: * myflag: changes the behavior somehow. * param1: the first input. Common flags: * -h | --help: Display this message. * -v: Increase the verbosity. * -vv: Increase the verbosity further. * -q | --quiet: Be silent. EOF } dry-wit dry-wit hooks 5 / 16
  7. 7. Input: checking checkInput() function checkInput() { local _flags=$(extractFlags $@); logDebug -n "Checking input"; for _flag in ${_flags}; do case ${_flag} in -h | --help | -v | -vv | -f | --my-flag) ;; *) logDebugResult FAILURE "fail"; exitWithErrorCode INVALID_OPTION ${_flag}; ;; esac done } dry-wit dry-wit hooks 6 / 16
  8. 8. Input: parsing parseInput() function parseInput() { local _flags=$(extractFlags $@); for _flag in ${_flags}; do case ${_flag} in -f | --my-flag) shift; export MY_FLAG="${1}"; shift; ;; *) shift; ;; esac done } dry-wit dry-wit hooks 7 / 16
  9. 9. Script requirements Dependencies function checkRequirements() { checkReq docker DOCKER_NOT_INSTALLED; checkReq realpath REALPATH_NOT_INSTALLED; checkReq envsubst ENVSUBST_NOT_INSTALLED; } DSL 1 executable-file: The required dependency. 2 message: The name of a constant describing the error to display should the dependency is not present. 3 dry-wit checks each dependency and exits if the check fails. dry-wit dry-wit hooks 8 / 16
  10. 10. Error messages defineErrors() function defineErrors() { export INVALID_OPTION="Unrecognized option"; export DOCKER_NOT_INSTALLED="docker is not installed. See http://www.docker.org"; export REPOSITORY_IS_MANDATORY="The repository argument is mandatory"; ERROR_MESSAGES=( INVALID_OPTION DOCKER_NOT_INSTALLED REPOSITORY_IS_MANDATORY ); export ERROR_MESSAGES; } dry-wit takes care of the exit codes exitWithErrorCode REPOSITORY_IS_MANDATORY; dry-wit dry-wit hooks 9 / 16
  11. 11. main() main() function main() { // Focus on the logic itself. // Forget about defensive programming // regarding input variables // or dependencies. // Start by writing pseudo-code, // and later define the identified // functions. } dry-wit dry-wit hooks 10 / 16
  12. 12. Logging logging logInfo -n "Calculating next prime number ..."; ... if [ $? -eq 0 ]; then logInfoResult SUCCESS "done"; else logInfoResult FAILURE "Too optimistic"; fi output [2015/10/26 15:09:54 my-script:main] Calculating next prime number ... [done] dry-wit dry-wit API 11 / 16
  13. 13. Temporary files API functions 1 Functions to create temporary files or folders. 2 dry-wit takes cares of cleaning them up afterwards. createTempFile() createTempFile; local tempFile=${RESULT}; createTempFolder() createTempFolder; local tempFolder=${RESULT}; dry-wit dry-wit API 12 / 16
  14. 14. Environment variables: Declaration (1/3) DSL for declaring environment variables 1 Declared in [script].inc.sh. 2 Safe to add to version control systems. 3 Mandatory information: description and default value. defineEnvVar() defineEnvVar MYPASSWORD "The description of MYPASSWORD" "secret"; dry-wit dry-wit API 13 / 16
  15. 15. Environment variables: Overridding (2/3) DSL for overridding default values 1 Declared in .[script].inc.sh. 2 Not always safe to add to version control systems. overrideEnvVar() overrideEnvVar MYPASSWORD ".toor!"; dry-wit dry-wit API 14 / 16
  16. 16. Environment variables: Overridding (3/3) Environment value overridden from the command line MYPASSWORD="abc123" ./script.sh ... dry-wit dry-wit API 15 / 16
  17. 17. Free as in freedom You’re welcome to try, fork, improve, even criticize! https://github.com/rydnr/dry-wit dry-wit Try it 16 / 16

×