Code Monkey home page Code Monkey logo

dot-ns's Introduction

DOT NAMESPACER

The included (dot) ns script is a proof of concept to enable user-specified parameter sharing across bash scripts in place of a few different alternatives:
- dot/source commands (has to share all parameters)
- subshells (only easily passes back a single variable limited to saving stdout)
- argument passing (arguments have size limits and naming them requires a re-map)

Namespace enforcement is up to the user, but ideally script names and their associated variables would share the same naming prefix, and each invoked script could just have top-level statements if desired.

FEATURE SPEC
# test all specs:
spec/run

# To see all details of a particalar spec go to it's page, ie:
# spec/spec1/spec1.
# To run a similar test you'll need:
#     - a dedicated folder
#     - add the dedicated folder path and this repo folder to the PATH
#     - run: . ns init
# or just do this in the dot-ns repo folder:
mkdir -p temp; PATH="${PATH}:${PWD}"; cd temp; PATH="${PATH}:${PWD}"; . ns init

# then you can run the code
. ns run my_script

FEATURE 3 SPEC - SUMMARY - PIPE TO EXTERNAL PROGRAM

The output from ns run (or run-piped if taking in input), should be able to be
redirected to an external (non-ns script) program.

FEATURE 3 SPEC - EXAMPLE CODE

# run this in dot-ns repo folder:
mkdir -p temp; PATH="${PATH}:${PWD}"; cd temp; PATH="${PATH}:${PWD}"; . ns init

cat << eof > my_script
my_var=a
echo "\$my_var"
. ns export my_var
eof

chmod +x my_script

. ns run my_script > >(sed "s/a/b/") #  prints b
echo $my_var # prints a

# note: `. ns run my_script | sed "s/a/b/"` will not work since it will
#     use storage associated with a subshell process id and then won't find
#     that value when you try to `echo "$my_var"`.
#     So, instead you can use >(process substitution) and use stdout pipe: >

FEATURE 4 SPEC - SUMMARY - SEQUENTIAL PIPES FOR PIPED INPUT

The run-piped command runs a chain of piped scripts in the order given, ie:
. ns run-piped a b c < <(echo test)
=>
echo test | a | b | c

FEATURE 4 SPEC - EXAMPLE CODE

cat << eof > my_script_a
cat
my_var=test2
. ns export my_var
eof

cat << eof > my_script_b
cat
eof

chmod +x my_script_a
chmod +x my_script_b

. ns run-piped my_script_a my_script_b < <(echo test)  # prints test
# this ran something equivalent to:
#    echo test | my_script_a | my_script_b
# except the first script's (my_script_a) was able to export and run-piped
# was able to auto-set it into memory of the caller
echo "$my_var"  # prints test2

# note: currently auto-setting values will only work for the 1st script in the
#       piped script chain (pipeline), the subsequential scripts are being
#       treated as 'additional' scripts to the main (first) script.

FEATURE 5 SPEC - SUMMARY - SEQUENTIAL PIPES

The run command runs a chain of piped scripts in the order given, ie:
. ns run a b c
=>
a | b | c

FEATURE 5 SPEC - EXAMPLE CODE

cat << eof > my_script_a
echo test
my_var=test2
. ns export my_var
eof

cat << eof > my_script_b
cat
eof

chmod +x my_script_a
chmod +x my_script_b

. ns run my_script_a my_script_b # prints test
# this ran something equivalent to:
#    my_script_a | my_script_b
# except the first script's (my_script_a) was able to export and run
# was able to auto-set it into memory of the caller
echo "$my_var"  # prints test2

EXAMPLE
# pb (path builder) namespace example
# (in this example both scripts would have exported all that they imported,
# which gives a nice clean variable and command grouping)

pb_add_path=backups;
pb_delim="/"
pb_delim_on_empty=Y
pb_path="${PWD}"

. ns run pb_remove_last
. ns run pb_add

echo "$pb_path"

# If $PWD was /home/git: it removes last path component and then adds backups
# '/home/git' -> '/home' -> '/home/backups'

# If $PWD was /: it would have removed the last component but delimit on empty is set to Yes so it becomes the delimiter and then adds backups
# '/' -> '/' -> '/backups'

HOW TO TEST NS ON THE SHELL
	# cd into the ns directory
	PATH="${PATH}:${PWD}"
	. ns init
	test_hello=hello
	test_world=world
	. ns run test_script
	echo "$test_output"
        echo "non-exported script variables do not get set: delim=${delim}"
	echo "test_hello should be unset now: test_hello=${test_hello}"
	echo "test_world should be unset now: test_world=${test_world}"

# expected output
hello, world
non-exported script variables do not get set: delim=
test_hello should be unset now: test_hello=
test_world should be unset now: test_world=


WHAT'S NEXT

possibly script includes (however you can already invoke scripts that have been added to the PATH variable )

dot-ns's People

Contributors

koreyhinton avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.