Skip to content

Commit 8dae641

Browse files
committed
feat: add 'export-file' function
1 parent bf1bbbd commit 8dae641

41 files changed

Lines changed: 283 additions & 147 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
!.gitignore
88
!.nvmrc
99
!.travis.yml
10+
!CHANGELOG.md
1011
!CONTRIBUTING.md
1112
!README.md
1213
!package.json

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ before_install:
1919
- tar --xz -xvf shellcheck-"${scversion}".linux.x86_64.tar.xz
2020
- sudo cp shellcheck-"${scversion}"/shellcheck /usr/bin/
2121
- shellcheck --version
22+
- export ALLOW_EXTERNAL_SOURCE='-x '
2223

2324
env:
2425
- CXX=g++-4.8

CHANGELOG.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
---
5+
6+
## [1.0.0](https://github.com/abbotto/shell-api/releases/tag/v1.0.0) - 2018-09-17
7+
### Added
8+
- `changelog` file.
9+
- [export-file](doc/api.md#export-file) function.
10+
11+
### Changed
12+
- Updated examples.
13+
- Improved testing.
14+
- Code refactoring.
15+
16+
### Removed
17+
- `import` function.
18+
19+
### Fixed
20+
- Ensure aliases are expanded.
21+
22+
## [0.2.0](https://github.com/abbotto/shell-api/releases/tag/v0.2.0) - 2018-08-05
23+
### Added
24+
- Placeholder support for the [pipe-file](doc/api.md#pipe-file) function.
25+
26+
## [0.1.1](https://github.com/abbotto/shell-api/releases/tag/v0.1.1) - 2018-08-03
27+
### Changed
28+
- Fixed a documentation typo.
29+
30+
## [0.1.0](https://github.com/abbotto/shell-api/releases/tag/v0.1.0) - 2018-08-01
31+
### Added
32+
- Build tools.
33+
- Configuration files.
34+
- API functions.
35+
- Tests.
36+
37+
---
38+
39+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
40+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ All functions have been tested in `bash`, `dash`, `ksh`, and `zsh`.
1616

1717
### The API
1818
- [confirm](doc/api.md#confirm)
19-
- [import](doc/api.md#import)
19+
- [export-file](doc/api.md#export-file)
2020
- [join-file](doc/api.md#join-file)
2121
- [pipe-file](doc/api.md#pipe-file)
2222
- [print-text](doc/api.md#print-text)
@@ -35,9 +35,9 @@ All functions have been tested in `bash`, `dash`, `ksh`, and `zsh`.
3535

3636
### Install
3737

38-
- Install this project as an `npm` module.
38+
- Install this project via `npm`.
3939

40-
npm i shell-api
40+
npm i shell-api --save
4141

4242
- Symlink the `shell` directory within a project.
4343

@@ -55,7 +55,7 @@ All functions have been tested in `bash`, `dash`, `ksh`, and `zsh`.
5555
# Load the whole framework at once
5656
. "${SHELL_API_PATH}"/shell-api
5757

58-
import ./.env
58+
export-file ./.env
5959
require <SCRIPT>
6060

6161
...
@@ -67,12 +67,12 @@ All functions have been tested in `bash`, `dash`, `ksh`, and `zsh`.
6767
SCRIPT_PATH=$(cd "$(dirname "${0}")"; pwd)
6868
SHELL_API_PATH="${SCRIPT_PATH}/../.shell"
6969

70-
# Load specific parts of the framework
70+
# Selectively load parts of the framework
7171
. "${SHELL_API_PATH}"/strict-mode
72-
. "${SHELL_API_PATH}"/import
72+
. "${SHELL_API_PATH}"/export-file
7373
. "${SHELL_API_PATH}"/require
7474

75-
import ./.env
75+
export-file ./.env
7676
require <SCRIPT>
7777

7878
...

doc/api.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
---
1313

14-
## [import](#import)
14+
## [export-file](#export-file)
1515

16-
- Fetch a `.env` file and export it's environment variables.
16+
- Safely export an environment variable file.
17+
- Properly handles newlines, nested quotes, and embedded variables.
18+
- Will not overwrite previously set variables in the current process unless the `--force` or `-f` flag is passed.
1719

18-
Usage: import <ENV>
20+
Usage: export-file <PATH>
1921

20-
[source code](../shell/import)
22+
export-file -f <PATH>
23+
24+
[source code](../shell/export-file)
2125

2226
---
2327

@@ -111,6 +115,8 @@ Reference: [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit)
111115

112116
- Force POSIX-compliant behaviour.
113117
- Disable brace expansion.
118+
- Prevent errors in a pipeline from being masked.
119+
- Expand aliases.
114120
- Exit if a command has a non-zero exit status.
115121
- Throws an error if an undefined variable is referenced.
116122

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "shell-api",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"description": "A POSIX-compliant framework for shell scripts",
55
"main": "shell/shell-api",
66
"scripts": {

shell/common/expand-aliases

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
3+
# Internal helper function
4+
5+
# Expand aliases in BASH
6+
CURRENT_SHELL=$(ps -p $$ -oargs=)
7+
8+
if test "${CURRENT_SHELL#*bash}" != "$CURRENT_SHELL"; then
9+
eval "shopt -s expand_aliases"
10+
fi

shell/confirm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ confirm(){
66
if [ -z "${1}" ]; then
77
echo "confirm: arguments are missing"
88
return 127
9-
109
else
1110
QUESTION="${1}"
1211
YES_CALLBACK="${2}"
@@ -24,10 +23,8 @@ confirm(){
2423
# Respond to input
2524
if echo "$response" | grep -iq "^y"; then
2625
"${YES_CALLBACK}"
27-
2826
elif [ -n "${2}" ]; then
2927
"${NO_CALLBACK}"
30-
3128
fi
3229

3330
fi

shell/export-file

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env sh
2+
3+
# Usage: doc/api.md#export-file
4+
5+
# Create an alias
6+
. ./shell/common/expand-aliases
7+
alias export-file="export_file"
8+
9+
export_file(){
10+
if [ -z "${1}" ]; then
11+
printf '%s\n' "export-file: arguments are missing"
12+
return 127
13+
fi
14+
15+
PROCESS_ENV_FILE=''
16+
17+
# Use the '-f' or '--force' flag to
18+
# overwrite previously exported variables
19+
if [ "$1" = "-f" ] || [ "$1" = "--force" ] ; then
20+
shift $(( $# > 0 ? 1 : 0 ))
21+
else
22+
PROCESS_ENV_FILE=$(mktemp)
23+
fi
24+
25+
INPUT_ENV_FILE="${1}"
26+
27+
if [ ! -f "${INPUT_ENV_FILE}" ]; then
28+
printf '%s\n' "Required resource not found: '${INPUT_ENV_FILE}'"
29+
else
30+
# Cache existing exported vars in a file so they can be preserved
31+
# Calling 'set' in POSIX mode returns a list of exported variables
32+
if [ -n "${PROCESS_ENV_FILE}" ]; then set > "${PROCESS_ENV_FILE}"; fi
33+
34+
# Export the target variables
35+
set -a
36+
. "$INPUT_ENV_FILE"
37+
38+
# Restore previously exported variables that belong to the current process
39+
if [ -n "${PROCESS_ENV_FILE}" ]; then . "${PROCESS_ENV_FILE}"; fi
40+
set +a
41+
42+
# Cleanup
43+
rm -rf "${PROCESS_ENV_FILE}"
44+
fi
45+
}

shell/import

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)