├── .codeclimate.yml ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── gvar ├── libexec └── gvar.sh ├── man ├── gvar.1 ├── gvar.1.html └── gvar.1.ronn └── test └── gvar.bats /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | engines: 2 | shellcheck: 3 | enabled: true 4 | ratings: 5 | paths: 6 | - bin/** 7 | - libexec/** 8 | - "**.sh" 9 | exclude_paths: 10 | - test/**/* 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | before_install: 3 | - sudo add-apt-repository ppa:duggan/bats --yes 4 | - sudo apt-get update -qq 5 | - sudo apt-get install -qq bats 6 | script: 7 | - bats test/gvar.bats 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Arturo Herrero, http://arturoherrero.com/ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gvar 2 | 3 | [![Code Climate](https://codeclimate.com/github/arturoherrero/gvar/badges/gpa.svg)](https://codeclimate.com/github/arturoherrero/gvar) 4 | [![Build Status](https://travis-ci.org/arturoherrero/gvar.svg?branch=master)](https://travis-ci.org/arturoherrero/gvar) 5 | 6 | gvar(1) -- display, set, or remove global variables*. 7 | 8 | * When I say global variable, I refer to a key-value pair that you can 9 | read/write at any time in any terminal session. You can think this is similar 10 | to the environment variables because it's a set of dynamic-named values but they 11 | are not session-wide or system-wide related. 12 | 13 | 14 | ## Description 15 | 16 | **gvar** is a pure Bash key-value store where each user has a different collection 17 | of data. The records are stored in the user's home directory as `~/.gvar` file. 18 | 19 | 20 | ## Installation 21 | 22 | Clone this repository: 23 | 24 | ```shell 25 | $ git clone git@github.com:arturoherrero/gvar.git 26 | ``` 27 | 28 | Add `gvar/bin/` to your `PATH`: 29 | 30 | ```shell 31 | $ echo 'export PATH="${PATH}:path/to/gvar/bin"' >> ~/.bash_profile 32 | ``` 33 | 34 | Source your profile: 35 | 36 | ```shell 37 | $ source ~/.bash_profile 38 | ``` 39 | 40 | #### OS X 41 | 42 | ```shell 43 | $ brew tap arturoherrero/formulae 44 | $ brew install gvar 45 | ``` 46 | 47 | ## Usage 48 | 49 | To print out the names and values of all the global variables, use: 50 | 51 | ```shell 52 | $ gvar 53 | ``` 54 | 55 | To set global variables, use arguments of the form `=`, 56 | setting variable `` to value ``: 57 | 58 | ```shell 59 | $ gvar VARIABLE=VALUE 60 | ``` 61 | 62 | Setting a global variable to an empty value is different from unsetting it: 63 | 64 | ```shell 65 | $ gvar VARIABLE= 66 | ``` 67 | 68 | To print the value of the global variable ``, use: 69 | 70 | ```shell 71 | $ gvar VARIABLE 72 | ``` 73 | 74 | To remove (unset) a global variable ``, use: 75 | 76 | ```shell 77 | $ gvar -u VARIABLE 78 | $ gvar --unset=VARIABLE 79 | ``` 80 | 81 | To delete the environment, removing all the global variables, use: 82 | 83 | ```shell 84 | $ gvar -d 85 | $ gvar --delete-environment 86 | ``` 87 | 88 | 89 | ## Who made this? 90 | 91 | This was made by Arturo Herrero under the MIT License. Find me on Twitter 92 | [@ArturoHerrero][1]. 93 | 94 | 95 | [1]: https://twitter.com/ArturoHerrero 96 | -------------------------------------------------------------------------------- /bin/gvar: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # How can I get the behavior of GNU's readlink -f on a Mac? 4 | # http://stackoverflow.com/q/1055671/462015 5 | script_path() { 6 | target_file=$0 7 | cd "$(dirname "$target_file")" || return 8 | target_file=$(basename "$target_file") 9 | 10 | # Iterate down a (possible) chain of symlinks 11 | while [ -L "$target_file" ]; do 12 | target_file=$(readlink "$target_file") 13 | cd "$(dirname "$target_file")" || return 14 | target_file=$(basename "$target_file") 15 | done 16 | 17 | pwd -P 18 | } 19 | 20 | "$(script_path)"/../libexec/gvar.sh "$@" 21 | -------------------------------------------------------------------------------- /libexec/gvar.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | VERSION=0.2.0 4 | FILE=$HOME/.gvar 5 | 6 | test -e "$FILE" || touch "$FILE" 7 | 8 | version() { 9 | echo "gvar version $VERSION" 10 | } 11 | 12 | help() { 13 | echo "usage: gvar [[=]] [-u | --unset=]" 14 | echo " [-d | --delete-environment] [-h | --help] [-v | --version]" 15 | } 16 | 17 | get_variable() { 18 | < "$FILE" grep -w "$1" | cut -d'=' -f2 19 | } 20 | 21 | set_variable() { 22 | echo "$1"="$2" >> "$FILE" 23 | } 24 | 25 | remove_variable() { 26 | sed -i.bak "/^$1=/d" "$FILE" 27 | } 28 | 29 | for option in "$@"; do 30 | case $option in 31 | -h | --help) 32 | help 33 | exit 0 34 | ;; 35 | -v | --version) 36 | version 37 | exit 0 38 | ;; 39 | -u) 40 | variable="$2" 41 | remove_variable "$variable" 42 | exit 0 43 | ;; 44 | --unset=*) 45 | variable="${option#*=}" 46 | remove_variable "$variable" 47 | exit 0 48 | ;; 49 | -d | --delete-environment) 50 | echo -n > "$FILE" 51 | ;; 52 | *=*) 53 | variable="${option%=*}" 54 | value="${option#*=}" 55 | remove_variable "$variable" 56 | set_variable "$variable" "$value" 57 | ;; 58 | *) 59 | variable="$1" 60 | get_variable "$variable" 61 | exit 0 62 | ;; 63 | esac 64 | done 65 | 66 | if [[ $# == 0 ]]; then 67 | cat "$FILE" 68 | fi 69 | -------------------------------------------------------------------------------- /man/gvar.1: -------------------------------------------------------------------------------- 1 | .\" generated with Ronn/v0.7.3 2 | .\" http://github.com/rtomayko/ronn/tree/0.7.3 3 | . 4 | .TH "GVAR" "1" "May 2016" "" "" 5 | . 6 | .SH "NAME" 7 | \fBgvar\fR \- display, set, or remove global variables 8 | . 9 | .SH "SYNOPSIS" 10 | \fBgvar\fR [\fIvariable\fR[=\fIvalue\fR] \.\.\.] 11 | . 12 | .br 13 | \fBgvar\fR \fB\-u\fR \fIvariable\fR | \fB\-\-unset\fR=\fIvariable\fR 14 | . 15 | .br 16 | \fBgvar\fR \fB\-d\fR | \fB\-\-delete\-environment\fR 17 | . 18 | .br 19 | \fBgvar\fR \fB\-h\fR | \fB\-\-help\fR 20 | . 21 | .br 22 | \fBgvar\fR \fB\-v\fR | \fB\-\-version\fR 23 | . 24 | .SH "DESCRIPTION" 25 | \fBgvar\fR is a pure Bash key\-value store where each user has a different collection of data\. The records are stored in the user\'s home directory as \fB~/\.gvar\fR file\. 26 | . 27 | .SH "OPTIONS" 28 | The following options are available: 29 | . 30 | .TP 31 | \fB\-u\fR \fIVARIABLE\fR, \fB\-\-unset\fR=\fIVARIABLE\fR 32 | Remove variable VARIABLE, if it was set\. 33 | . 34 | .TP 35 | \fB\-d\fR, \fB\-\-delete\-environment\fR 36 | Remove all the global variables\. 37 | . 38 | .TP 39 | \fB\-h\fR, \fB\-\-help\fR 40 | Display the command options and exit\. 41 | . 42 | .TP 43 | \fB\-v\fR, \fB\-\-version\fR 44 | Output version information and exit\. 45 | . 46 | .SH "EXAMPLES" 47 | To print out the names and values of all the global variables, use: 48 | . 49 | .IP "" 4 50 | . 51 | .nf 52 | 53 | $ gvar 54 | . 55 | .fi 56 | . 57 | .IP "" 0 58 | . 59 | .P 60 | To set global variables, use arguments of the form \fIVARIABLE\fR=\fIVALUE\fR, setting variable \fIVARIABLE\fR to value \fIVALUE\fR: 61 | . 62 | .IP "" 4 63 | . 64 | .nf 65 | 66 | $ gvar VARIABLE=VALUE 67 | . 68 | .fi 69 | . 70 | .IP "" 0 71 | . 72 | .P 73 | Setting a global variable to an empty value is different from unsetting it: 74 | . 75 | .IP "" 4 76 | . 77 | .nf 78 | 79 | $ gvar VARIABLE= 80 | . 81 | .fi 82 | . 83 | .IP "" 0 84 | . 85 | .P 86 | To print the value of the global variable \fIVARIABLE\fR, use: 87 | . 88 | .IP "" 4 89 | . 90 | .nf 91 | 92 | $ gvar VARIABLE 93 | . 94 | .fi 95 | . 96 | .IP "" 0 97 | . 98 | .P 99 | To remove (unset) a global variable \fIVARIABLE\fR, use: 100 | . 101 | .IP "" 4 102 | . 103 | .nf 104 | 105 | $ gvar \-u VARIABLE 106 | $ gvar \-\-unset=VARIABLE 107 | . 108 | .fi 109 | . 110 | .IP "" 0 111 | . 112 | .P 113 | To delete the environment, removing all the global variables, use: 114 | . 115 | .IP "" 4 116 | . 117 | .nf 118 | 119 | $ gvar \-d 120 | $ gvar \-\-delete\-environment 121 | . 122 | .fi 123 | . 124 | .IP "" 0 125 | . 126 | .SH "EXIT STATUS" 127 | The gvar utility exits 0 on success, and > 0 if an error occurs\. 128 | . 129 | .SH "COPYRIGHT" 130 | gvar is copyright (c) Arturo Herrero, \fIhttp://arturoherrero\.com/\fR 131 | -------------------------------------------------------------------------------- /man/gvar.1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | gvar(1) - display, set, or remove global variables 7 | 44 | 45 | 52 | 53 |
54 | 55 | 64 | 65 |
    66 |
  1. gvar(1)
  2. 67 |
  3. 68 |
  4. gvar(1)
  5. 69 |
70 | 71 |

NAME

72 |

73 | gvar - display, set, or remove global variables 74 |

75 | 76 |

SYNOPSIS

77 | 78 |

gvar [variable[=value] ...]
79 | gvar -u variable | --unset=variable
80 | gvar -d | --delete-environment
81 | gvar -h | --help
82 | gvar -v | --version

83 | 84 |

DESCRIPTION

85 | 86 |

gvar is a pure Bash key-value store where each user has a different collection 87 | of data. The records are stored in the user's home directory as ~/.gvar file.

88 | 89 |

OPTIONS

90 | 91 |

The following options are available:

92 | 93 |
94 |
-u VARIABLE, --unset=VARIABLE

Remove variable VARIABLE, if it was set.

95 |
-d, --delete-environment

Remove all the global variables.

96 |
-h, --help

Display the command options and exit.

97 |
-v, --version

Output version information and exit.

98 |
99 | 100 | 101 |

EXAMPLES

102 | 103 |

To print out the names and values of all the global variables, use:

104 | 105 |
$ gvar
106 | 
107 | 108 |

To set global variables, use arguments of the form VARIABLE=VALUE, 109 | setting variable VARIABLE to value VALUE:

110 | 111 |
$ gvar VARIABLE=VALUE
112 | 
113 | 114 |

Setting a global variable to an empty value is different from unsetting it:

115 | 116 |
$ gvar VARIABLE=
117 | 
118 | 119 |

To print the value of the global variable VARIABLE, use:

120 | 121 |
$ gvar VARIABLE
122 | 
123 | 124 |

To remove (unset) a global variable VARIABLE, use:

125 | 126 |
$ gvar -u VARIABLE
127 | $ gvar --unset=VARIABLE
128 | 
129 | 130 |

To delete the environment, removing all the global variables, use:

131 | 132 |
$ gvar -d
133 | $ gvar --delete-environment
134 | 
135 | 136 |

EXIT STATUS

137 | 138 |

The gvar utility exits 0 on success, and > 0 if an error occurs.

139 | 140 | 141 | 142 |

gvar is copyright (c) Arturo Herrero, http://arturoherrero.com/

143 | 144 | 145 |
    146 |
  1. 147 |
  2. May 2016
  3. 148 |
  4. gvar(1)
  5. 149 |
150 | 151 |
152 | 153 | 154 | -------------------------------------------------------------------------------- /man/gvar.1.ronn: -------------------------------------------------------------------------------- 1 | gvar(1) -- display, set, or remove global variables 2 | =================================================== 3 | 4 | ## SYNOPSIS 5 | 6 | `gvar` [[=] ...] 7 | `gvar` `-u` | `--unset`= 8 | `gvar` `-d` | `--delete-environment` 9 | `gvar` `-h` | `--help` 10 | `gvar` `-v` | `--version` 11 | 12 | 13 | ## DESCRIPTION 14 | 15 | **gvar** is a pure Bash key-value store where each user has a different collection 16 | of data. The records are stored in the user's home directory as `~/.gvar` file. 17 | 18 | 19 | ## OPTIONS 20 | 21 | The following options are available: 22 | 23 | * `-u` , `--unset`=: 24 | Remove variable VARIABLE, if it was set. 25 | 26 | * `-d`, `--delete-environment`: 27 | Remove all the global variables. 28 | 29 | * `-h`, `--help`: 30 | Display the command options and exit. 31 | 32 | * `-v`, `--version`: 33 | Output version information and exit. 34 | 35 | 36 | ## EXAMPLES 37 | 38 | To print out the names and values of all the global variables, use: 39 | 40 | $ gvar 41 | 42 | To set global variables, use arguments of the form =, 43 | setting variable to value : 44 | 45 | $ gvar VARIABLE=VALUE 46 | 47 | Setting a global variable to an empty value is different from unsetting it: 48 | 49 | $ gvar VARIABLE= 50 | 51 | To print the value of the global variable , use: 52 | 53 | $ gvar VARIABLE 54 | 55 | To remove (unset) a global variable , use: 56 | 57 | $ gvar -u VARIABLE 58 | $ gvar --unset=VARIABLE 59 | 60 | To delete the environment, removing all the global variables, use: 61 | 62 | $ gvar -d 63 | $ gvar --delete-environment 64 | 65 | 66 | ## EXIT STATUS 67 | 68 | The gvar utility exits 0 on success, and > 0 if an error occurs. 69 | 70 | 71 | ## COPYRIGHT 72 | 73 | gvar is copyright (c) Arturo Herrero, 74 | -------------------------------------------------------------------------------- /test/gvar.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | gvar="${BATS_TEST_DIRNAME}/../bin/gvar" 4 | FILE=$HOME/.gvar 5 | 6 | setup() { 7 | test -e "$FILE" || touch "$FILE" 8 | mv "$FILE" "$FILE".copy 9 | echo OTHER=other > "$FILE" 10 | echo TEST=test >> "$FILE" 11 | echo TESTING=abc >> "$FILE" 12 | } 13 | 14 | @test "invoking gvar prints the names and values of the global variables" { 15 | run "$gvar" 16 | [ "$status" -eq 0 ] 17 | [ "$output" = "$(printf 'OTHER=other\nTEST=test\nTESTING=abc\n')" ] 18 | } 19 | 20 | @test "invoking gvar VARIABLE prints the value of the global variable" { 21 | run "$gvar" TEST 22 | [ "$status" -eq 0 ] 23 | [ "$output" = "test" ] 24 | } 25 | 26 | @test "invoking gvar VARIABLE OTHER prints the value of the first global variable" { 27 | run "$gvar" TEST OTHER 28 | [ "$status" -eq 0 ] 29 | [ "$output" = "test" ] 30 | } 31 | 32 | @test "invoking gvar -u VARIABLE removes the global variable" { 33 | run "$gvar" -u TEST 34 | [ "$status" -eq 0 ] 35 | run "$gvar" 36 | [ "$output" = "$(printf 'OTHER=other\nTESTING=abc\n')" ] 37 | } 38 | 39 | @test "invoking gvar -u VARIABLE OTHER removes the first global variable" { 40 | run "$gvar" -u TEST OTHER 41 | [ "$status" -eq 0 ] 42 | run "$gvar" 43 | [ "$output" = "$(printf 'OTHER=other\nTESTING=abc\n')" ] 44 | } 45 | 46 | @test "invoking gvar --unset=VARIABLE removes the global variable" { 47 | run "$gvar" --unset=TEST 48 | [ "$status" -eq 0 ] 49 | run "$gvar" 50 | [ "$output" = "$(printf 'OTHER=other\nTESTING=abc\n')" ] 51 | } 52 | 53 | @test "invoking gvar VARIABLE=VALUE adds the global variable" { 54 | run "$gvar" NAME=arturo 55 | [ "$status" -eq 0 ] 56 | run "$gvar" NAME 57 | [ "$output" = "arturo" ] 58 | } 59 | 60 | @test "invoking gvar VARIABLE=VALUE VARIABLE=VALUE adds every global variable of the list" { 61 | run "$gvar" NAME=arturo SURNAME=herrero 62 | [ "$status" -eq 0 ] 63 | run "$gvar" NAME 64 | [ "$output" = "arturo" ] 65 | run "$gvar" SURNAME 66 | [ "$output" = "herrero" ] 67 | } 68 | 69 | @test "invoking gvar VARIABLE= adds an empty global variable" { 70 | run "$gvar" EMPTY= 71 | [ "$status" -eq 0 ] 72 | run "$gvar" EMPTY 73 | [ "$output" = "$(printf '\n')" ] 74 | } 75 | 76 | @test "invoking gvar VARIABLE=VALUE for an existing variable, updates the global variable" { 77 | run "$gvar" TEST=updated 78 | [ "$status" -eq 0 ] 79 | run "$gvar" TEST 80 | [ "$output" = "updated" ] 81 | } 82 | 83 | @test "invoking gvar -d deletes all the global variables" { 84 | run "$gvar" -d 85 | [ "$status" -eq 0 ] 86 | run "$gvar" 87 | [ "$output" = "" ] 88 | } 89 | 90 | @test "invoking gvar --delete-environment deletes all the global variables" { 91 | run "$gvar" --delete-environment 92 | [ "$status" -eq 0 ] 93 | run "$gvar" 94 | [ "$output" = "" ] 95 | } 96 | 97 | teardown() { 98 | mv "$FILE".copy "$FILE" 99 | } 100 | --------------------------------------------------------------------------------