aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--LICENSE661
-rw-r--r--Makefile52
-rw-r--r--README.md125
-rw-r--r--completions/ves.fish58
-rw-r--r--man/ves.1223
-rw-r--r--tests/run_tests.fish159
-rw-r--r--tests/run_tests.sh229
-rwxr-xr-x[-rw-r--r--]ves-activate.sh81
-rw-r--r--ves-copy.sh56
-rwxr-xr-x[-rw-r--r--]ves-create.sh29
-rwxr-xr-x[-rw-r--r--]ves-deactivate.sh19
-rwxr-xr-x[-rw-r--r--]ves-delete.sh15
-rw-r--r--ves-export.sh39
-rwxr-xr-x[-rw-r--r--]ves-init.sh232
-rwxr-xr-x[-rw-r--r--]ves-list.sh59
-rwxr-xr-x[-rw-r--r--]ves-prompt.sh9
-rw-r--r--ves-run.sh30
-rw-r--r--ves-show.sh25
-rw-r--r--ves-unset.sh57
-rwxr-xr-xves-var-add.sh93
-rwxr-xr-x[-rw-r--r--]ves-var-rm.sh70
-rw-r--r--ves.fish104
-rwxr-xr-x[-rw-r--r--]ves.sh105
24 files changed, 2406 insertions, 127 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..914541d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.tmp
+*.swp
+*~
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..be3f7b2
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,661 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU Affero General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU Affero General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU Affero General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU Affero General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU AGPL, see
+<https://www.gnu.org/licenses/>.
diff --git a/Makefile b/Makefile
index 9262db1..cfd057a 100644
--- a/Makefile
+++ b/Makefile
@@ -12,25 +12,49 @@ endif
BIN_DIR=$(HOME)/.local/bin
-# TODO: ves needs to be installed as a function in the user's
-# shell. This will require making updates to their configuration
-# file as part of the install process, which will require a bit
-# of extra work.
+all:
+ @echo "Nothing to build. Run 'make install' to install, 'make test' to run tests."
+
install:
- - test -d $(DATA_DIR)/ves/bin || mkdir -p $(DATA_DIR)/ves/bin
- - test -d $(DATA_DIR)/ves/lib || mkdir -p $(DATA_DIR)/ves/lib
- - test -d $(DATA_DIR)/ves/envs || mkdir -p $(DATA_DIR)/ves/envs
- - test -d $(CONF_DIR)/ves || mkdir -p $(CONF_DIR)/ves
- - test -d $(BIN_DIR)/ves_scripts || mkdir -p $(BIN_DIR)/ves_scripts
+ -mkdir -p $(DATA_DIR)/ves/bin
+ -mkdir -p $(DATA_DIR)/ves/lib
+ -mkdir -p $(DATA_DIR)/ves/envs
+ -mkdir -p $(CONF_DIR)/ves
+ -mkdir -p $(BIN_DIR)/ves_scripts
+ -mkdir -p $(DATA_DIR)/man/man1
cp ./*.sh $(BIN_DIR)/ves_scripts
chmod u+x $(BIN_DIR)/ves_scripts/*
+ cp ./man/ves.1 $(DATA_DIR)/man/man1/ves.1
+ @if command -v fish > /dev/null 2>&1; then \
+ mkdir -p $(CONF_DIR)/fish/functions $(CONF_DIR)/fish/completions; \
+ cp ./ves.fish $(CONF_DIR)/fish/functions/ves.fish; \
+ cp ./completions/ves.fish $(CONF_DIR)/fish/completions/ves.fish; \
+ echo "fish detected: installed function and completions"; \
+ fi
+ @echo ""
+ @echo "Installation complete. To enable sh-ves, add the following line"
+ @echo "to your shell's rc or profile file:"
+ @echo ""
+ @echo " . $(BIN_DIR)/ves_scripts/ves-init.sh"
+ @echo ""
+test:
+ sh ./tests/run_tests.sh
+ @if command -v fish > /dev/null 2>&1; then \
+ fish ./tests/run_tests.fish; \
+ else \
+ echo "fish not found, skipping fish shim tests"; \
+ fi
uninstall:
- - test -f $(BIN_DIR)/ves && rm -f $(BIN_DIR)/ves
- - test -d $(BIN_DIR)/ves_scripts && rm -rf $(BIN_DIR)/ves_scripts
-
+ -rm -f $(BIN_DIR)/ves
+ -rm -rf $(BIN_DIR)/ves_scripts
+ -rm -f $(CONF_DIR)/fish/functions/ves.fish
+ -rm -f $(CONF_DIR)/fish/completions/ves.fish
+ -rm -f $(DATA_DIR)/man/man1/ves.1
purge: uninstall
- - test -d $(DATA_DIR)/ves && rm -rf $(DATA_DIR)/ves
- - test -d $(CONF_DIR)/ves && rm -rf $(CONF_DIR)/ves
+ -rm -rf $(DATA_DIR)/ves
+ -rm -rf $(CONF_DIR)/ves
+
+.PHONY: all install test uninstall purge
diff --git a/README.md b/README.md
index 0cb5243..23b7ddd 100644
--- a/README.md
+++ b/README.md
@@ -10,9 +10,36 @@ with a package manager. It simple allows swapping between environments,
so it is on the user to install the different versions of various packages
on their own, if they would like to use sh-ves for this.
+## Installation
+To install, run
+```bash
+$ make install
+```
+which will copy the scripts into `~/.local/bin/ves_scripts`, create the
+sh-ves data directories, and install the `ves(1)` man page to
+`~/.local/share/man/man1`. Then add the following line to your shell's rc or
+profile file,
+```bash
+. ~/.local/bin/ves_scripts/ves-init.sh
+```
+sh-ves operates by defining shell functions, so this file must be sourced
+into each shell session that wants to use it. `make uninstall` removes the
+scripts; `make purge` additionally removes all data, including your saved
+environments.
+
+### fish
+sh-ves also works in fish via a shim. If fish is detected, `make install`
+copies `ves.fish` to `~/.config/fish/functions/` (which autoloads a `ves`
+function — no rc file changes needed) and tab completions to
+`~/.config/fish/completions/`. The shim runs each command through
+the real POSIX implementation in an `sh` subshell, then replays any
+environment changes into the fish session, so all commands behave
+identically. Activation state is carried in exported `SHVES_SAVED_*`
+variables, so activate/deactivate work across invocations.
+
## Commands
sh-ves consists of a series of related scripts, which can be called
-from a global wrapper script for convenience. All of these examples use
+from a global wrapper function for convenience. All of these examples use
the global wrapper.
### Create a new environment
@@ -52,6 +79,22 @@ $ ves deactivate
when within an active environment. This will restore the environment variables
to the state they were prior to activating the environment.
+### Switch between environments
+As a convenience, the command,
+```bash
+$ ves switch <name>
+```
+will deactivate the current environment (if any) and activate the named
+one in a single step.
+
+### Run a command inside an environment
+To run a one-off command inside an environment without activating it,
+```bash
+$ ves run <name> <command...>
+```
+The environment is applied in a subshell, so the current shell is left
+untouched and no deactivation is necessary.
+
### Add a variable to the environment
To add a new environment variable to the environment, use the command
```bash
@@ -61,6 +104,19 @@ If called from within an activate ves environment, the `--env` flag can
be omitted, which will result in the export applying to the active
environment.
+To remove a variable from an environment entirely,
+```bash
+$ ves unset [--env=<name>] <variable>
+```
+If the target environment is active, the live variable is restored to its
+pre-activation value.
+
+To see all variables stored in an environment,
+```bash
+$ ves show [<name>]
+```
+which defaults to the active environment when no name is given.
+
### Manage PATH, LDPATH, etc.
A variety of important environment variables actually represent :-delimited
arrays, such as PATH and LDPATH. These are called "path-like" in sh-ves.
@@ -69,9 +125,13 @@ related to adding and removing elements from these variables.
To add an entry to a path-like variable,
```bash
-$ ves var-add [--env=<name>] <variable> <value>
+$ ves var-add [--env=<name>] [--append] <variable> <value>
```
-will prepend `<value>:` to the specified variable.
+will prepend `<value>:` to the specified variable (or append `:<value>`
+with the `--append` flag, for fallback entries). If the variable is not
+yet stored in the environment, it is seeded from the live shell value
+first, so prepending to PATH never truncates it. To truncate PATH and
+friends deliberately, create the environment with `--override`.
To remove an entry from a path-like variable,
```bash
@@ -83,11 +143,11 @@ ves will automatically expand `<value>` to an absolute path using certain
rules, for convenience, if it does not begin with a `/` or a `./`. For example,
values added to the PATH will have,
```
-$XDG_DATA_ROOT/ves/bin/
+$XDG_DATA_HOME/ves/bin/
```
-prepended to them, and values added to LDPATH will have,
+prepended to them, and values added to LDPATH and LD_LIBRARY_PATH will have,
```
-$XDG_DATA_ROOT/ves/lib/
+$XDG_DATA_HOME/ves/lib/
```
prepended. It is advisable to install binaries and libraries that you would
like to participate in ves environments into independent directories within
@@ -99,8 +159,26 @@ $ ves list [--index] <variable>
```
which will list the contents of the variable, each on its own line,
in order of precedence. The `--index` flag will cause the command to
-list a numeric (starting at 0) index before each entry.
+list a numeric (starting at 0) index before each entry. Note that `ves list`
+reads from the live shell, so it reflects the current state of the variable,
+whether or not an environment is active.
+### List, copy, rename, and delete environments
+To see all created environments,
+```bash
+$ ves envs
+```
+Environments can be duplicated and renamed with,
+```bash
+$ ves copy <src> <dst>
+$ ves rename <old> <new>
+```
+and deleted with,
+```bash
+$ ves delete <name>
+```
+The currently active environment cannot be deleted or renamed; deactivate
+it first.
## Potential Use-cases
sh-ves can be used to manage different projects requiring specified
@@ -115,18 +193,37 @@ can be then set up for this project,
```
$ ves create example_project
$ ves activate example_project
-$ ves add PATH gcc-4
-$ ves add LDPATH libexample-6.12
+$ ves var-add PATH gcc-4
+$ ves var-add LDPATH libexample-6.12
```
## Shell prompt integration
-sh-ves has a built in helper script called ves-prompt that will automatically
-generate a string to be included in your POSIX compliant shell's PS1 string
-(or where-ever else you want it to go).
+sh-ves has a built in helper that will automatically generate a string to
+be included in your POSIX compliant shell's PS1 string (or where-ever else
+you want it to go).
```bash
-$ ves-prompt [symbol]
-( [symbol] <name> )
+$ ves prompt [symbol]
+([symbol] <name>)
```
This script doesn't do any color manipulation, so you can add color codes prior
to it within your prompt to configure it however you like. The prompt
string will only appear when an sh-ves environment is currently active.
+
+In fish, use the quoted command substitution form within your fish_prompt
+function, so that the empty output when no environment is active does not
+swallow adjacent arguments,
+```fish
+function fish_prompt
+ echo -n "$(ves prompt) "(prompt_pwd)' $ '
+end
+```
+
+## Testing
+The test suite requires no external framework and can be run with,
+```bash
+$ make test
+```
+
+## License
+sh-ves is licensed under the GNU Affero General Public License,
+version 3. See the LICENSE file for details.
diff --git a/completions/ves.fish b/completions/ves.fish
new file mode 100644
index 0000000..7efb5ff
--- /dev/null
+++ b/completions/ves.fish
@@ -0,0 +1,58 @@
+# Tab completions for the sh-ves fish shim.
+#
+# Install this file to ~/.config/fish/completions/ves.fish (make install
+# does this automatically when fish is present).
+
+function __ves_envs
+ set -l dir
+ if set -q XDG_DATA_HOME
+ set dir $XDG_DATA_HOME/ves/envs
+ else
+ set dir $HOME/.local/share/ves/envs
+ end
+ for f in $dir/*
+ if test -f $f
+ basename $f
+ end
+ end
+end
+
+function __ves_no_subcommand
+ not __fish_seen_subcommand_from create delete copy rename activate \
+ deactivate switch run export unset show var-add var-rm list envs \
+ prompt help
+end
+
+complete -c ves -f
+
+complete -c ves -n __ves_no_subcommand -a create -d 'Create a new environment'
+complete -c ves -n __ves_no_subcommand -a delete -d 'Delete an environment'
+complete -c ves -n __ves_no_subcommand -a copy -d 'Copy an environment'
+complete -c ves -n __ves_no_subcommand -a rename -d 'Rename an environment'
+complete -c ves -n __ves_no_subcommand -a activate -d 'Activate an environment'
+complete -c ves -n __ves_no_subcommand -a deactivate -d 'Deactivate the active environment'
+complete -c ves -n __ves_no_subcommand -a switch -d 'Switch to another environment'
+complete -c ves -n __ves_no_subcommand -a run -d 'Run a command inside an environment'
+complete -c ves -n __ves_no_subcommand -a export -d 'Set a variable in an environment'
+complete -c ves -n __ves_no_subcommand -a unset -d 'Remove a variable from an environment'
+complete -c ves -n __ves_no_subcommand -a show -d 'Show the variables of an environment'
+complete -c ves -n __ves_no_subcommand -a var-add -d 'Prepend an entry to a path-like variable'
+complete -c ves -n __ves_no_subcommand -a var-rm -d 'Remove an entry from a path-like variable'
+complete -c ves -n __ves_no_subcommand -a list -d 'List the entries of a path-like variable'
+complete -c ves -n __ves_no_subcommand -a envs -d 'List all environments'
+complete -c ves -n __ves_no_subcommand -a prompt -d 'Print a prompt fragment for PS1'
+complete -c ves -n __ves_no_subcommand -a help -d 'Show usage'
+
+# subcommands taking an environment name
+complete -c ves -n '__fish_seen_subcommand_from delete activate switch show copy rename run' \
+ -a '(__ves_envs)' -d 'environment'
+
+# options
+complete -c ves -n '__fish_seen_subcommand_from create' -l override \
+ -d 'Truncate PATH, LDPATH, etc. in the new environment'
+complete -c ves -n '__fish_seen_subcommand_from export unset var-add var-rm' \
+ -l env -d 'Target environment' -xa '(__ves_envs)'
+complete -c ves -n '__fish_seen_subcommand_from var-add' -l append \
+ -d 'Append instead of prepend'
+complete -c ves -n '__fish_seen_subcommand_from list' -l index \
+ -d 'Number each entry'
diff --git a/man/ves.1 b/man/ves.1
new file mode 100644
index 0000000..79f27ce
--- /dev/null
+++ b/man/ves.1
@@ -0,0 +1,223 @@
+.TH VES 1 "June 2026" "sh-ves" "User Commands"
+.SH NAME
+ves \- Bourne shell virtual environment system
+.SH SYNOPSIS
+.B ves
+.I command
+.RI [ arguments ]
+.SH DESCRIPTION
+.B sh-ves
+manages named collections of environment variables, called
+.IR environments ,
+and swaps between them within a shell session. Activating an environment
+applies its stored variables over the current shell; deactivating it
+restores every variable to its prior state exactly, including variables
+that did not previously exist.
+.PP
+.B sh-ves
+is not a package manager and does not install software. It only manages
+environment variables, so installing the different versions of compilers
+or libraries that an environment points at is up to the user.
+.PP
+The
+.B ves
+command is a shell function, not a binary; it must be loaded by sourcing
+.I ves-init.sh
+in the shell's rc or profile file (see
+.BR FILES ).
+Environments do not compose: only one environment may be active at a
+time, and activating a second one is refused (but see
+.BR switch ).
+.SH COMMANDS
+.TP
+.BI "create " [\-\-override] " " name
+Create a new environment. By default the new environment overrides
+nothing. With
+.BR \-\-override ,
+the variables listed in
+.B SHVES_OVERRIDE_VARS
+(by default: PATH, LDPATH, LD_LIBRARY_PATH, and CPATH) are stored as
+empty strings, so activating the environment truncates them.
+.TP
+.BI "delete " name
+Delete an environment. The active environment cannot be deleted.
+.TP
+.BI "copy " "src dst"
+Copy an environment to a new name.
+.TP
+.BI "rename " "old new"
+Rename an environment. The active environment cannot be renamed.
+.TP
+.BI "activate " name
+Activate an environment, exporting all of its stored variables. The
+prior value of each variable is saved for restoration. Fails if another
+environment is already active.
+.TP
+.B deactivate
+Deactivate the active environment, restoring all overridden variables
+to their pre-activation values.
+.TP
+.BI "switch " name
+Deactivate the current environment (if any), then activate
+.IR name .
+.TP
+.BI "run " "name command ..."
+Run a single command inside an environment without activating it in the
+current shell. The environment is applied in a subshell, so no
+deactivation is necessary. The exit status is that of the command.
+.TP
+.BI "export " [\-\-env=name] " " "variable value"
+Set a variable within an environment. With no
+.B \-\-env
+option, the active environment is targeted. If the target environment is
+active, the variable is also exported into the live shell.
+.TP
+.BI "unset " [\-\-env=name] " " variable
+Remove a variable from an environment entirely. If the target
+environment is active, the live variable is restored to its
+pre-activation value.
+.TP
+.BI "show " [name]
+Print the variables stored in an environment, one
+.I VAR=value
+per line. Defaults to the active environment.
+.TP
+.BI "var-add " [\-\-env=name] " " [\-\-append] " " "variable value"
+Prepend
+.I value
+to a path-like (colon-delimited) variable, or append it with
+.BR \-\-append .
+If the variable is not yet stored in the environment, it is seeded from
+the live shell value first, so prepending to PATH never truncates it.
+Values are expanded according to the rules in
+.B PATH EXPANSION
+below.
+.TP
+.BI "var-rm " [\-\-env=name] " " "variable value"
+Remove an entry from a path-like variable. The value is expanded with
+the same rules as
+.BR var-add ,
+so entries can be removed using the same form they were added with.
+.TP
+.BI "list " [\-\-index] " " variable
+List the entries of a path-like variable from the live shell, one per
+line, in order of precedence. With
+.BR \-\-index ,
+each entry is prefixed with its numeric (0-based) index.
+.TP
+.B envs
+List all created environments.
+.TP
+.BI "prompt " [symbol]
+Print a prompt fragment of the form
+.BI ( "symbol name" )
+when an environment is active, and nothing otherwise.
+.I symbol
+defaults to
+.BR VENV .
+Intended for inclusion in PS1; it performs no color manipulation, so
+color codes may be added around it.
+.TP
+.B help
+Print a usage summary.
+.SH PATH EXPANSION
+For
+.B var-add
+and
+.BR var-rm ,
+values that do not begin with
+.B /
+or
+.B ./
+are expanded for convenience: values added to PATH are prefixed with
+.IR $XDG_DATA_HOME /ves/bin/,
+and values added to LDPATH or LD_LIBRARY_PATH are prefixed with
+.IR $XDG_DATA_HOME /ves/lib/.
+It is advisable to install binaries and libraries that participate in
+ves environments into independent directories within these two
+locations, e.g.
+.IR ~/.local/share/ves/bin/gcc-4/ .
+Other variables are never expanded.
+.SH ENVIRONMENT
+.TP
+.B XDG_DATA_HOME
+Base directory for sh-ves data. Defaults to
+.IR ~/.local/share .
+.TP
+.B SHVES_SCRIPTS_DIR
+Location of the sh-ves scripts. Defaults to
+.IR ~/.local/bin/ves_scripts .
+.TP
+.B SHVES_OVERRIDE_VARS
+Space-separated list of variables truncated by
+.BR "create \-\-override" .
+Defaults to
+.BR "PATH LDPATH LD_LIBRARY_PATH CPATH" .
+.TP
+.B SHVES_ENV_NM
+Name of the currently active environment. Exported while an environment
+is active; read by
+.BR "ves prompt" .
+Do not set manually.
+.SH FILES
+.TP
+.I ~/.local/bin/ves_scripts/ves-init.sh
+Initialization file. Source this from your shell's rc or profile file:
+.IP
+.EX
+\&. ~/.local/bin/ves_scripts/ves-init.sh
+.EE
+.TP
+.I $XDG_DATA_HOME/ves/envs/
+One file per environment, holding its name header and stored variables.
+.TP
+.I $XDG_DATA_HOME/ves/bin/, $XDG_DATA_HOME/ves/lib/
+Suggested locations for binaries and libraries used by ves environments
+(see
+.BR "PATH EXPANSION" ).
+.SH FISH
+In
+.BR fish (1),
+.B ves
+is provided by a shim function installed to
+.IR ~/.config/fish/functions/ves.fish ,
+with tab completions in
+.IR ~/.config/fish/completions/ .
+The shim runs each command through the POSIX implementation in an
+.BR sh (1)
+subshell and replays the resulting environment changes into the fish
+session, so all commands behave identically. For prompt integration, use
+the quoted command substitution form so that empty output does not
+swallow adjacent arguments:
+.IP
+.EX
+function fish_prompt
+ echo -n "$(ves prompt) "(prompt_pwd)' $ '
+end
+.EE
+.SH EXAMPLES
+Set up an environment for a project needing GCC 4 and libexample 6.12,
+previously installed under the sh-ves data directories:
+.IP
+.EX
+$ ves create example_project
+$ ves activate example_project
+$ ves var-add PATH gcc-4
+$ ves var-add LDPATH libexample-6.12
+.EE
+.PP
+Run a single build inside that environment without activating it:
+.IP
+.EX
+$ ves run example_project make
+.EE
+.SH EXIT STATUS
+0 on success, 1 on operational errors (missing environment, activation
+conflicts, and similar), 2 on usage errors.
+.SH SEE ALSO
+.BR sh (1),
+.BR env (1),
+.BR fish (1)
+.SH LICENSE
+sh-ves is licensed under the GNU Affero General Public License,
+version 3.
diff --git a/tests/run_tests.fish b/tests/run_tests.fish
new file mode 100644
index 0000000..b1b2c06
--- /dev/null
+++ b/tests/run_tests.fish
@@ -0,0 +1,159 @@
+#!/usr/bin/env fish
+#
+# Test suite for the sh-ves fish shim. Mirrors the core lifecycle checks of
+# run_tests.sh, driven through the ves.fish wrapper function.
+#
+# $ fish tests/run_tests.fish
+
+set fails 0
+set checks 0
+
+function assert_eq
+ set -g checks (math $checks + 1)
+ if test "$argv[2]" = "$argv[3]"
+ printf "ok - %s\n" $argv[1]
+ else
+ printf "FAIL - %s\n expected: [%s]\n actual: [%s]\n" $argv[1] $argv[2] $argv[3]
+ set -g fails (math $fails + 1)
+ end
+end
+
+function assert_status
+ set -g checks (math $checks + 1)
+ if test $argv[2] -eq $argv[3]
+ printf "ok - %s\n" $argv[1]
+ else
+ printf "FAIL - %s\n expected status: [%s]\n actual status: [%s]\n" $argv[1] $argv[2] $argv[3]
+ set -g fails (math $fails + 1)
+ end
+end
+
+# Set up an isolated sh-ves installation in a temp directory.
+set tmpdir (mktemp -d); or exit 1
+
+set script_dir (cd (dirname (status filename))/..; and pwd)
+
+set -gx XDG_DATA_HOME $tmpdir/share
+set -gx SHVES_SCRIPTS_DIR $script_dir
+
+source $script_dir/ves.fish
+
+# --- creation ----------------------------------------------------------------
+
+ves create test_env > /dev/null
+assert_status "create succeeds" 0 $status
+
+ves create test_env > /dev/null 2>&1
+assert_status "duplicate create fails" 1 $status
+
+# --- export to an inactive environment ---------------------------------------
+
+ves export --env=test_env MY_VAR hello > /dev/null
+assert_status "export --env succeeds" 0 $status
+assert_eq "inactive export does not touch shell" "" "$MY_VAR"
+
+# --- activation --------------------------------------------------------------
+
+set prior_path "$PATH"
+
+ves activate test_env
+assert_status "activate succeeds" 0 $status
+assert_eq "active env name set" "test_env" "$SHVES_ENV_NM"
+assert_eq "stored variable exported" "hello" "$MY_VAR"
+assert_eq "child processes see variable" "hello" (sh -c 'printf %s "$MY_VAR"')
+
+ves activate test_env > /dev/null 2>&1
+assert_status "second activation refused" 1 $status
+
+# --- live modification -------------------------------------------------------
+
+ves export MY_VAR goodbye > /dev/null
+assert_eq "export updates active shell" "goodbye" "$MY_VAR"
+
+ves var-add MY_PATH /opt/foo > /dev/null
+assert_eq "var-add creates :-list" "/opt/foo" "$MY_PATH"
+ves var-add MY_PATH /opt/bar > /dev/null
+assert_eq "var-add prepends" "/opt/bar:/opt/foo" "$MY_PATH"
+
+ves var-rm MY_PATH /opt/bar > /dev/null
+assert_eq "var-rm removes entry" "/opt/foo" "$MY_PATH"
+
+set prior_path_head $PATH[1]
+ves var-add PATH /opt/mybin > /dev/null
+assert_eq "var-add PATH visible to children" "/opt/mybin" (sh -c 'printf %s "${PATH%%:*}"')
+assert_eq "PATH stays a fish list" "/opt/mybin" "$PATH[1]"
+assert_eq "var-add PATH seeds from live value" "$prior_path_head" "$PATH[2]"
+ves var-rm PATH /opt/mybin > /dev/null
+
+assert_eq "prompt shows active env" "(VENV test_env)" (ves prompt)
+
+# --- deactivation ------------------------------------------------------------
+
+ves deactivate
+assert_status "deactivate succeeds" 0 $status
+assert_eq "env name cleared" "" "$SHVES_ENV_NM"
+assert_eq "modified variable restored" "" "$MY_VAR"
+assert_eq "var-add variable removed" "" "$MY_PATH"
+assert_eq "PATH restored" "$prior_path" "$PATH"
+
+ves deactivate > /dev/null 2>&1
+assert_status "double deactivate fails" 1 $status
+
+# --- show / unset / copy / rename / run / switch -------------------------
+
+assert_eq "show prints stored variables" "MY_VAR=goodbye" (ves show test_env | grep MY_VAR)
+
+ves export --env=test_env TMP_VAR x > /dev/null
+ves unset --env=test_env TMP_VAR > /dev/null
+assert_status "unset succeeds" 0 $status
+assert_eq "unset removes variable" "" (ves show test_env | grep TMP_VAR)
+
+set -gx MY_VAR original
+ves activate test_env > /dev/null
+ves unset MY_VAR > /dev/null
+assert_eq "live unset restores prior value" "original" "$MY_VAR"
+ves deactivate > /dev/null
+set -e MY_VAR
+
+# restore the variable for the run check below
+ves export --env=test_env MY_VAR goodbye > /dev/null
+
+ves export --env=test_env AP_PATH /opt/a > /dev/null
+ves var-add --env=test_env --append AP_PATH /opt/b > /dev/null
+assert_eq "var-add --append appends" "AP_PATH=/opt/a:/opt/b" (ves show test_env | grep AP_PATH)
+
+ves copy test_env copy_env > /dev/null
+assert_status "copy succeeds" 0 $status
+ves rename copy_env renamed_env > /dev/null
+assert_status "rename succeeds" 0 $status
+ves delete renamed_env > /dev/null
+
+assert_eq "run executes inside env" "goodbye" (ves run test_env sh -c 'printf %s "$MY_VAR"')
+assert_eq "run does not leak into shell" "" "$MY_VAR"
+
+ves create other_env > /dev/null
+ves activate other_env > /dev/null
+ves switch test_env > /dev/null
+assert_eq "switch changes active env" "test_env" "$SHVES_ENV_NM"
+ves deactivate > /dev/null
+ves delete other_env > /dev/null
+
+# --- completions ----------------------------------------------------------
+
+source $script_dir/completions/ves.fish
+set -l comp (complete -C'ves activ')
+assert_eq "subcommand completion works" "activate" (string split \t -- $comp[1])[1]
+set -l comp (complete -C'ves activate ')
+assert_eq "env name completion works" "test_env" (string split \t -- $comp[1])[1]
+
+# --- envs / delete -----------------------------------------------------------
+
+assert_eq "envs lists environments" "test_env" (ves envs)
+ves delete test_env > /dev/null
+assert_status "delete succeeds" 0 $status
+
+# --- summary -------------------------------------------------------------
+
+rm -rf $tmpdir
+printf "\n%d checks, %d failures\n" $checks $fails
+test $fails -eq 0
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
new file mode 100644
index 0000000..e0134fc
--- /dev/null
+++ b/tests/run_tests.sh
@@ -0,0 +1,229 @@
+#!/bin/sh
+#
+# Test suite for sh-ves. Runs the full create/activate/modify/deactivate
+# lifecycle in an isolated temporary data directory and asserts on variable
+# state at each step. Run with any POSIX shell:
+#
+# $ sh tests/run_tests.sh
+# $ dash tests/run_tests.sh
+#
+
+fails=0
+checks=0
+
+assert_eq() {
+ checks=$((checks + 1))
+ if [ "$2" = "$3" ]; then
+ printf "ok - %s\n" "$1"
+ else
+ printf "FAIL - %s\n expected: [%s]\n actual: [%s]\n" "$1" "$2" "$3"
+ fails=$((fails + 1))
+ fi
+}
+
+assert_status() {
+ checks=$((checks + 1))
+ if [ "$2" -eq "$3" ]; then
+ printf "ok - %s\n" "$1"
+ else
+ printf "FAIL - %s\n expected status: [%s]\n actual status: [%s]\n" "$1" "$2" "$3"
+ fails=$((fails + 1))
+ fi
+}
+
+# Set up an isolated sh-ves installation in a temp directory.
+tmpdir=$(mktemp -d) || exit 1
+trap 'rm -rf "$tmpdir"' EXIT
+
+script_dir=$(cd "$(dirname "$0")/.." && pwd)
+
+XDG_DATA_HOME="$tmpdir/share"
+SHVES_SCRIPTS_DIR="$script_dir"
+export XDG_DATA_HOME SHVES_SCRIPTS_DIR
+
+. "$script_dir/ves-init.sh"
+
+# --- creation ---------------------------------------------------------------
+
+ves create test_env > /dev/null
+assert_status "create succeeds" 0 $?
+assert_eq "env file created" "name: test_env" "$(cat "$SHVES_ENV_DIR/test_env")"
+
+ves create test_env > /dev/null 2>&1
+assert_status "duplicate create fails" 1 $?
+
+ves create "bad name" > /dev/null 2>&1
+assert_status "invalid name rejected" 1 $?
+
+ves create --override ovr_env > /dev/null
+assert_status "create --override succeeds" 0 $?
+grep -q "^export_var:PATH=$" "$SHVES_ENV_DIR/ovr_env"
+assert_status "--override truncates PATH" 0 $?
+
+# --- export to an inactive environment --------------------------------------
+
+ves export --env=test_env MY_VAR hello > /dev/null
+assert_status "export --env succeeds" 0 $?
+assert_eq "value stored in file" "hello" "$(_shves_get_var "$SHVES_ENV_DIR/test_env" MY_VAR)"
+assert_eq "inactive export does not touch shell" "" "$MY_VAR"
+
+ves export MY_VAR oops > /dev/null 2>&1
+assert_status "export with no env active or given fails" 1 $?
+
+# --- activation --------------------------------------------------------------
+
+prior_path="$PATH"
+
+ves activate test_env
+assert_status "activate succeeds" 0 $?
+assert_eq "active env name set" "test_env" "$SHVES_ENV_NM"
+assert_eq "stored variable exported" "hello" "$MY_VAR"
+assert_eq "child processes see variable" "hello" "$(sh -c 'printf %s "$MY_VAR"')"
+
+ves activate ovr_env > /dev/null 2>&1
+assert_status "second activation refused" 1 $?
+
+# --- live modification -------------------------------------------------------
+
+ves export MY_VAR goodbye > /dev/null
+assert_eq "export updates active shell" "goodbye" "$MY_VAR"
+assert_eq "export updates file" "goodbye" "$(_shves_get_var "$SHVES_ENV_DIR/test_env" MY_VAR)"
+
+ves var-add MY_PATH /opt/foo > /dev/null
+assert_eq "var-add creates :-list" "/opt/foo" "$MY_PATH"
+ves var-add MY_PATH /opt/bar > /dev/null
+assert_eq "var-add prepends" "/opt/bar:/opt/foo" "$MY_PATH"
+
+ves var-add LDPATH mylib > /dev/null
+assert_eq "var-add expands relative LDPATH entries" "$XDG_DATA_HOME/ves/lib/mylib" "$LDPATH"
+
+ves var-add PATH /opt/mybin > /dev/null
+assert_eq "var-add PATH seeds from live value" "/opt/mybin:$prior_path" "$PATH"
+ves var-rm PATH /opt/mybin > /dev/null
+assert_eq "var-rm PATH restores prior entries" "$prior_path" "$PATH"
+
+ves var-rm LDPATH mylib > /dev/null
+assert_eq "var-rm removes expanded entry" "" "$LDPATH"
+
+ves var-rm MY_PATH /opt/bar > /dev/null
+assert_eq "var-rm removes entry" "/opt/foo" "$MY_PATH"
+ves var-rm MY_PATH /nonexistent > /dev/null 2>&1
+assert_status "var-rm of absent entry fails" 1 $?
+
+ves list MY_PATH > "$tmpdir/list.out"
+assert_eq "list prints entries" "/opt/foo" "$(cat "$tmpdir/list.out")"
+
+ves var-add MY_PATH /opt/baz > /dev/null
+assert_eq "list --index" "0 /opt/baz
+1 /opt/foo" "$(ves list --index MY_PATH)"
+
+# --- prompt ------------------------------------------------------------------
+
+assert_eq "prompt shows active env" "(VENV test_env)" "$(ves prompt)"
+assert_eq "prompt with symbol" "(>> test_env)" "$(ves prompt '>>')"
+
+# --- delete protection -------------------------------------------------------
+
+ves delete test_env > /dev/null 2>&1
+assert_status "deleting active env refused" 1 $?
+
+# --- deactivation ------------------------------------------------------------
+
+ves deactivate
+assert_status "deactivate succeeds" 0 $?
+assert_eq "env name cleared" "" "$SHVES_ENV_NM"
+assert_eq "modified variable restored" "" "$MY_VAR"
+assert_eq "var-add variable removed" "" "$MY_PATH"
+assert_eq "PATH restored" "$prior_path" "$PATH"
+assert_eq "prompt empty when inactive" "" "$(ves prompt)"
+
+ves deactivate > /dev/null 2>&1
+assert_status "double deactivate fails" 1 $?
+
+# changes made while active must persist in the file
+assert_eq "file retains exported value" "goodbye" "$(_shves_get_var "$SHVES_ENV_DIR/test_env" MY_VAR)"
+
+# --- override environments ---------------------------------------------------
+
+ves activate ovr_env
+assert_eq "override env truncates PATH" "" "$PATH"
+ves deactivate
+assert_eq "PATH restored after override env" "$prior_path" "$PATH"
+
+# --- show / unset ------------------------------------------------------------
+
+assert_eq "show prints stored variables" "MY_VAR=goodbye" "$(ves show test_env | grep MY_VAR)"
+ves show > /dev/null 2>&1
+assert_status "show with no env active or given fails" 1 $?
+
+ves unset --env=test_env MY_PATH > /dev/null
+assert_status "unset succeeds" 0 $?
+_shves_has_var "$SHVES_ENV_DIR/test_env" MY_PATH
+assert_status "unset removes variable from file" 1 $?
+ves unset --env=test_env NOSUCH > /dev/null 2>&1
+assert_status "unset of absent variable fails" 1 $?
+
+# unset while active restores the pre-activation value immediately
+MY_VAR=original
+export MY_VAR
+ves activate test_env
+ves unset MY_VAR > /dev/null
+assert_eq "live unset restores prior value" "original" "$MY_VAR"
+ves deactivate
+assert_eq "deactivate after live unset is clean" "original" "$MY_VAR"
+unset MY_VAR
+
+# restore the variable for the run/switch checks below
+ves export --env=test_env MY_VAR goodbye > /dev/null
+
+# --- var-add --append ----------------------------------------------------
+
+ves export --env=test_env AP_PATH /opt/a > /dev/null
+ves var-add --env=test_env --append AP_PATH /opt/b > /dev/null
+assert_eq "var-add --append appends" "/opt/a:/opt/b" "$(_shves_get_var "$SHVES_ENV_DIR/test_env" AP_PATH)"
+
+# --- copy / rename -------------------------------------------------------
+
+ves copy test_env copy_env > /dev/null
+assert_status "copy succeeds" 0 $?
+assert_eq "copy preserves variables" "$(ves show test_env)" "$(ves show copy_env)"
+assert_eq "copy rewrites name header" "name: copy_env" "$(grep '^name:' "$SHVES_ENV_DIR/copy_env")"
+ves copy test_env copy_env > /dev/null 2>&1
+assert_status "copy onto existing env fails" 1 $?
+
+ves rename copy_env renamed_env > /dev/null
+assert_status "rename succeeds" 0 $?
+_shves_check_env_exists copy_env 1
+assert_status "rename removes old env" 1 $?
+ves delete renamed_env > /dev/null
+
+# --- run -----------------------------------------------------------------
+
+assert_eq "run executes inside env" "goodbye" "$(ves run test_env sh -c 'printf %s "$MY_VAR"')"
+assert_eq "run does not leak into shell" "" "$MY_VAR"
+ves run nosuch_env true > /dev/null 2>&1
+assert_status "run with bad env fails" 1 $?
+
+# --- switch --------------------------------------------------------------
+
+ves activate ovr_env > /dev/null
+ves switch test_env > /dev/null
+assert_eq "switch changes active env" "test_env" "$SHVES_ENV_NM"
+assert_eq "switch applies new env vars" "goodbye" "$MY_VAR"
+ves deactivate
+assert_eq "deactivate after switch restores" "" "$MY_VAR"
+
+# --- envs / delete -----------------------------------------------------------
+
+assert_eq "envs lists environments" "ovr_env
+test_env" "$(ves envs)"
+
+ves delete test_env > /dev/null
+assert_status "delete succeeds" 0 $?
+ves delete ovr_env > /dev/null
+assert_eq "envs empty after deletes" "" "$(ves envs)"
+
+# --- summary -------------------------------------------------------------
+
+printf "\n%d checks, %d failures\n" "$checks" "$fails"
+[ "$fails" -eq 0 ]
diff --git a/ves-activate.sh b/ves-activate.sh
index 1a24852..5199f5b 100644..100755
--- a/ves-activate.sh
+++ b/ves-activate.sh
@@ -1 +1,82 @@
#!/bin/sh
+#
+# Activate an sh-ves environment in the current shell. The prior values of
+# all variables the environment overrides are saved in SHVES_SAVED_<var>
+# shell variables (with SHVES_SAVEDSET_<var> recording whether the variable
+# existed at all), so that ves_deactivate can restore them exactly.
+#
+ves_activate() {
+ if [ "$#" -lt 1 ]; then
+ printf "ERROR: No environment name specified.\n" >&2
+ return 1
+ fi
+
+ if [ -n "$SHVES_ENV_NM" ]; then
+ printf "ERROR: Environment [%s] is already active. Deactivate it first.\n" "$SHVES_ENV_NM" >&2
+ return 1
+ fi
+
+ env_name="$1"
+ if ! _shves_check_env_name "$env_name"; then
+ return 1
+ fi
+
+ if ! _shves_check_env_exists "$env_name"; then
+ return 1
+ fi
+
+ SHVES_SAVED_VARS=""
+
+ while IFS= read -r line; do
+ case $line in
+ export_var:*)
+ ;;
+ *)
+ continue
+ ;;
+ esac
+
+ entry="${line#export_var:}"
+ var="${entry%%=*}"
+ value="${entry#*=}"
+
+ if ! _shves_check_var_name "$var"; then
+ continue
+ fi
+
+ # save the prior value (and whether the variable was set at all)
+ # so that deactivation can restore the environment exactly
+ _shves_live_export "$var" "$value"
+ done < "$SHVES_ENV_DIR/$env_name"
+
+ SHVES_ENV_NM="$env_name"
+ export SHVES_ENV_NM
+
+ return 0
+}
+
+#
+# Switch to another environment: deactivate the current one (if any) and
+# activate the named one. Pure convenience; environments still do not
+# compose.
+#
+ves_switch() {
+ if [ "$#" -lt 1 ]; then
+ printf "ERROR: No environment name specified.\n" >&2
+ return 1
+ fi
+
+ if ! _shves_check_env_name "$1"; then
+ return 1
+ fi
+
+ if ! _shves_check_env_exists "$1"; then
+ return 1
+ fi
+
+ if [ -n "$SHVES_ENV_NM" ]; then
+ ves_deactivate || return 1
+ fi
+
+ ves_activate "$1"
+}
diff --git a/ves-copy.sh b/ves-copy.sh
new file mode 100644
index 0000000..11dde25
--- /dev/null
+++ b/ves-copy.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Copy and rename sh-ves environments. Environments are single files, so
+# both operations are simple file manipulations; only the name header
+# needs rewriting.
+#
+ves_copy() {
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves copy <src> <dst>\n" >&2
+ return 2
+ fi
+
+ src="$1"
+ dst="$2"
+
+ if ! _shves_check_env_name "$src" || ! _shves_check_env_name "$dst"; then
+ return 1
+ fi
+
+ if ! _shves_check_env_exists "$src"; then
+ return 1
+ fi
+
+ if _shves_check_env_exists "$dst" 1; then
+ printf "ERROR: Environment [%s] already exists.\n" "$dst" >&2
+ return 1
+ fi
+
+ printf "name: %s\n" "$dst" > "$SHVES_ENV_DIR/$dst"
+ grep "^export_var:" "$SHVES_ENV_DIR/$src" >> "$SHVES_ENV_DIR/$dst"
+
+ printf "Environment [%s] copied to [%s]\n" "$src" "$dst"
+
+ return 0
+}
+
+ves_rename() {
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves rename <old> <new>\n" >&2
+ return 2
+ fi
+
+ if [ "$1" = "$SHVES_ENV_NM" ]; then
+ printf "ERROR: Environment [%s] is currently active. Deactivate it first.\n" "$1" >&2
+ return 1
+ fi
+
+ if ! ves_copy "$1" "$2" > /dev/null; then
+ return 1
+ fi
+
+ rm -f "$SHVES_ENV_DIR/$1"
+ printf "Environment [%s] renamed to [%s]\n" "$1" "$2"
+
+ return 0
+}
diff --git a/ves-create.sh b/ves-create.sh
index b4f6467..11e6f05 100644..100755
--- a/ves-create.sh
+++ b/ves-create.sh
@@ -1,32 +1,43 @@
#!/bin/sh
+#
+# Create a new sh-ves environment. With --override, the variables listed
+# in SHVES_OVERRIDE_VARS (PATH, LDPATH, etc. by default) are initialized
+# to empty strings in the new environment, so activating it truncates them.
+#
_shves_create_env() {
file="$SHVES_ENV_DIR/$1"
- printf "name: $1\n" > $file
+ printf "name: %s\n" "$1" > "$file"
- if [ "$2" -eq "1" ]; then
+ if [ "$2" -eq 1 ]; then
for var in $SHVES_OVERRIDE_VARS; do
- printf "export_var:%s=\n" "$var" >> $file
+ printf "export_var:%s=\n" "$var" >> "$file"
done
fi
}
ves_create() {
+ override=0
+ case $1 in
+ --override)
+ override=1
+ shift
+ ;;
+ esac
+
if [ "$#" -lt 1 ]; then
- printf "ERROR: No environment name specified.\n" > /dev/stderr
+ printf "ERROR: No environment name specified.\n" >&2
return 1
fi
- override=0
env_name="$1"
- test "$#" -eq "2" && override=1
if ! _shves_check_env_name "$env_name"; then
- return 1;
+ return 1
fi
if _shves_check_env_exists "$env_name" 1; then
- printf "ERROR: Environment [%s] already exists.\n" "$env_name"
- return 1;
+ printf "ERROR: Environment [%s] already exists.\n" "$env_name" >&2
+ return 1
fi
_shves_create_env "$env_name" "$override"
diff --git a/ves-deactivate.sh b/ves-deactivate.sh
index 1a24852..3937f93 100644..100755
--- a/ves-deactivate.sh
+++ b/ves-deactivate.sh
@@ -1 +1,20 @@
#!/bin/sh
+#
+# Deactivate the currently active sh-ves environment, restoring all
+# overridden variables to the values saved by ves_activate.
+#
+ves_deactivate() {
+ if [ -z "$SHVES_ENV_NM" ]; then
+ printf "ERROR: No environment is currently active.\n" >&2
+ return 1
+ fi
+
+ for var in $SHVES_SAVED_VARS; do
+ _shves_restore_var "$var"
+ done
+
+ unset SHVES_SAVED_VARS
+ unset SHVES_ENV_NM
+
+ return 0
+}
diff --git a/ves-delete.sh b/ves-delete.sh
index 6d1557b..648677d 100644..100755
--- a/ves-delete.sh
+++ b/ves-delete.sh
@@ -1,7 +1,11 @@
#!/bin/sh
+#
+# Delete an existing sh-ves environment. The active environment may not
+# be deleted; deactivate it first.
+#
ves_delete() {
if [ "$#" -lt 1 ]; then
- printf "ERROR: No environment name specified.\n" > /dev/stderr
+ printf "ERROR: No environment name specified.\n" >&2
return 1
fi
@@ -11,11 +15,16 @@ ves_delete() {
fi
if ! _shves_check_env_exists "$env_name"; then
- return 1;
+ return 1
+ fi
+
+ if [ "$env_name" = "$SHVES_ENV_NM" ]; then
+ printf "ERROR: Environment [%s] is currently active. Deactivate it first.\n" "$env_name" >&2
+ return 1
fi
rm -f "$SHVES_ENV_DIR/$env_name"
- printf "Environment [%s] deleted successfully\n" $env_name
+ printf "Environment [%s] deleted successfully\n" "$env_name"
return 0
}
diff --git a/ves-export.sh b/ves-export.sh
index e69de29..0f36006 100644
--- a/ves-export.sh
+++ b/ves-export.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+#
+# Set an environment variable within an sh-ves environment. If the target
+# environment is currently active, the variable is also exported into the
+# live shell.
+#
+ves_export() {
+ env=""
+ case $1 in
+ --env=*)
+ env="${1#--env=}"
+ shift
+ ;;
+ esac
+
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves export [--env=<name>] <variable> <value>\n" >&2
+ return 2
+ fi
+
+ var="$1"
+ value="$2"
+
+ if ! env=$(_shves_resolve_env "$env"); then
+ return 1
+ fi
+
+ if ! _shves_check_var_name "$var"; then
+ return 1
+ fi
+
+ _shves_set_var "$SHVES_ENV_DIR/$env" "$var" "$value"
+
+ if [ "$env" = "$SHVES_ENV_NM" ]; then
+ _shves_live_export "$var" "$value"
+ fi
+
+ return 0
+}
diff --git a/ves-init.sh b/ves-init.sh
index e323aaa..8885cf9 100644..100755
--- a/ves-init.sh
+++ b/ves-init.sh
@@ -7,8 +7,8 @@
#
# Note, the correct functioning of other sh-ves functions requires that this
# file be sourced prior to calling them. As such, they will check for
-# SH_VES_INIT to be created prior to doing anything. If is not suggested to try
-# to set this variable manually to get around sourcing this file.
+# SH_VES_INIT to be created prior to doing anything. It is not suggested to
+# try to set this variable manually to get around sourcing this file.
#
# This file also provides default values for several sh-ves configuration
# variables. You may override these settings by setting the variables
@@ -16,11 +16,13 @@
# or by overwriting their values after sourcing the file.
if [ -z "${XDG_DATA_HOME+x}" ]; then
- SHVES_ENV_DIR="$HOME/.local/share/ves/envs"
+ SHVES_DATA_DIR="$HOME/.local/share/ves"
else
- SHVES_ENV_DIR="$XDG_DATA_HOME/ves/envs"
+ SHVES_DATA_DIR="$XDG_DATA_HOME/ves"
fi
+SHVES_ENV_DIR="$SHVES_DATA_DIR/envs"
+
if [ -z "${SHVES_OVERRIDE_VARS+x}" ]; then
SHVES_OVERRIDE_VARS="PATH LDPATH LD_LIBRARY_PATH CPATH"
fi
@@ -31,20 +33,33 @@ else
SHVES_BIN="$SHVES_SCRIPTS_DIR"
fi
+if [ ! -d "$SHVES_ENV_DIR" ]; then
+ mkdir -p "$SHVES_ENV_DIR"
+fi
+
+#
+# Validation is done with case globs rather than grep so that it keeps
+# working even when an environment has truncated PATH.
+#
_shves_check_env_name() {
- if ! echo $1 | grep "^[[:alpha:][:digit:]_-]*$" > /dev/null; then
- printf "ERROR: Environment [%s] is invalid. Name must contain only letters, -, and _\n" $1 > /dev/stderr
- return 1
- fi
+ case $1 in
+ ""|*[!a-zA-Z0-9_-]*)
+ printf "ERROR: Environment [%s] is invalid. Name must contain only letters, digits, -, and _\n" "$1" >&2
+ return 1
+ ;;
+ esac
return 0
}
-
+#
+# Check that environment $1 exists. If a second argument is given, the
+# check is quiet; otherwise an error is printed on failure.
+#
_shves_check_env_exists() {
if [ ! -f "$SHVES_ENV_DIR/$1" ]; then
if [ "$#" -lt 2 ]; then
- printf "ERROR: Environment [%s] does not exist.\n" $1 > /dev/stderr
+ printf "ERROR: Environment [%s] does not exist.\n" "$1" >&2
fi
return 1
@@ -53,6 +68,196 @@ _shves_check_env_exists() {
return 0
}
+_shves_check_var_name() {
+ case $1 in
+ ""|[!a-zA-Z_]*|*[!a-zA-Z0-9_]*)
+ printf "ERROR: Variable name [%s] is invalid.\n" "$1" >&2
+ return 1
+ ;;
+ esac
+
+ return 0
+}
+
+#
+# Resolve and validate the target environment for a command. $1 is the
+# requested environment name (possibly empty); when empty, the active
+# environment is used. The name is checked for validity and existence,
+# and printed on success.
+#
+_shves_resolve_env() {
+ if [ -n "$1" ]; then
+ _shves_env="$1"
+ elif [ -n "$SHVES_ENV_NM" ]; then
+ _shves_env="$SHVES_ENV_NM"
+ else
+ printf "ERROR: No environment active or specified.\n" >&2
+ return 1
+ fi
+
+ if ! _shves_check_env_name "$_shves_env"; then
+ return 1
+ fi
+
+ if ! _shves_check_env_exists "$_shves_env"; then
+ return 1
+ fi
+
+ printf '%s' "$_shves_env"
+ return 0
+}
+
+#
+# Print the stored value of variable $2 within the environment file $1.
+# Prints nothing if the variable is not stored in the environment.
+#
+_shves_get_var() {
+ grep "^export_var:$2=" "$1" | cut -d '=' -f 2-
+}
+
+#
+# Returns 0 if variable $2 is stored in environment file $1, 1 otherwise.
+#
+_shves_has_var() {
+ grep "^export_var:$2=" "$1" > /dev/null
+}
+
+#
+# Set variable $2 to value $3 within environment file $1, replacing any
+# existing definition. The file is rewritten rather than edited in place
+# with sed, so values may safely contain characters special to sed.
+#
+_shves_set_var() {
+ grep -v "^export_var:$2=" "$1" > "$1.tmp"
+ printf "export_var:%s=%s\n" "$2" "$3" >> "$1.tmp"
+ mv "$1.tmp" "$1"
+}
+
+#
+# Remove variable $2 from environment file $1.
+#
+_shves_del_var() {
+ grep -v "^export_var:$2=" "$1" > "$1.tmp"
+ mv "$1.tmp" "$1"
+}
+
+#
+# Expand value $2 for path-like variable $1 according to the sh-ves
+# convenience rules: values not beginning with / or ./ are taken to be
+# relative to $SHVES_DATA_DIR/bin (for PATH) or $SHVES_DATA_DIR/lib
+# (for LDPATH and LD_LIBRARY_PATH). Other variables are left untouched.
+#
+_shves_expand_path() {
+ case $2 in
+ /*|./*)
+ printf '%s' "$2"
+ return 0
+ ;;
+ esac
+
+ case $1 in
+ PATH)
+ printf '%s' "$SHVES_DATA_DIR/bin/$2"
+ ;;
+ LDPATH|LD_LIBRARY_PATH)
+ printf '%s' "$SHVES_DATA_DIR/lib/$2"
+ ;;
+ *)
+ printf '%s' "$2"
+ ;;
+ esac
+}
+
+#
+# Returns 0 if variable $1 is recorded in the activation save-state. The
+# names in SHVES_SAVED_VARS are space-separated, so padding both the list
+# and the pattern with spaces matches whole names only.
+#
+_shves_is_saved() {
+ case " $SHVES_SAVED_VARS " in
+ *" $1 "*)
+ return 0
+ ;;
+ esac
+
+ return 1
+}
+
+#
+# Restore variable $1 to its pre-activation value from the activation
+# save-state, and clear the variable's bookkeeping. The caller is
+# responsible for removing the variable from SHVES_SAVED_VARS.
+#
+# The variable name is only known at run time, so the references must be
+# built with eval. For $1 = PATH, the lines below expand to:
+# [ "$SHVES_SAVEDSET_PATH" = 1 ]
+# export PATH="$SHVES_SAVED_PATH"
+#
+_shves_restore_var() {
+ if eval "[ \"\$SHVES_SAVEDSET_$1\" = 1 ]"; then
+ eval "export $1=\"\$SHVES_SAVED_$1\""
+ else
+ unset "$1"
+ fi
+
+ unset "SHVES_SAVED_$1" "SHVES_SAVEDSET_$1"
+}
+
+#
+# Begin and end word-splitting of a :-delimited value. Between the two
+# calls, an unquoted expansion splits into one word per entry, with
+# globbing disabled so entries containing wildcards pass through intact.
+#
+_shves_split_begin() {
+ case $- in
+ *f*) _shves_had_f=1 ;;
+ *) _shves_had_f=0 ;;
+ esac
+ set -f
+ _shves_old_ifs="$IFS"
+ IFS=:
+}
+
+_shves_split_end() {
+ IFS="$_shves_old_ifs"
+ if [ "$_shves_had_f" -eq 0 ]; then
+ set +f
+ fi
+ unset _shves_had_f _shves_old_ifs
+}
+
+#
+# Export variable $1 with value $2 into the live shell, recording the prior
+# value in the activation save-state (if it is not already recorded) so that
+# ves_deactivate restores it properly. Only call while an environment is
+# active.
+#
+# The save-state variables are exported so that the state survives across
+# subshell invocations. This allows non-POSIX shells (such as fish) to drive
+# sh-ves through a wrapper that runs each command in a fresh sh subshell.
+#
+_shves_live_export() {
+ if ! _shves_is_saved "$1"; then
+ # The eval lines build references to the variable named in $1.
+ # For $1 = PATH, they expand to:
+ # [ -n "${PATH+x}" ] (is PATH set at all?)
+ # SHVES_SAVED_PATH=$PATH
+ if eval "[ -n \"\${$1+x}\" ]"; then
+ eval "SHVES_SAVEDSET_$1=1"
+ eval "SHVES_SAVED_$1=\$$1"
+ export "SHVES_SAVED_$1"
+ else
+ eval "SHVES_SAVEDSET_$1=0"
+ fi
+
+ export "SHVES_SAVEDSET_$1"
+ SHVES_SAVED_VARS="$SHVES_SAVED_VARS $1"
+ export SHVES_SAVED_VARS
+ fi
+
+ export "$1=$2"
+}
+
. "$SHVES_BIN"/ves.sh
. "$SHVES_BIN"/ves-export.sh
. "$SHVES_BIN"/ves-activate.sh
@@ -61,5 +266,10 @@ _shves_check_env_exists() {
. "$SHVES_BIN"/ves-var-rm.sh
. "$SHVES_BIN"/ves-create.sh
. "$SHVES_BIN"/ves-delete.sh
+. "$SHVES_BIN"/ves-list.sh
+. "$SHVES_BIN"/ves-unset.sh
+. "$SHVES_BIN"/ves-show.sh
+. "$SHVES_BIN"/ves-copy.sh
+. "$SHVES_BIN"/ves-run.sh
-
+SH_VES_INIT=1
diff --git a/ves-list.sh b/ves-list.sh
index 1b90703..61e6bca 100644..100755
--- a/ves-list.sh
+++ b/ves-list.sh
@@ -1,6 +1,57 @@
#!/bin/sh
+#
+# ves_list: list the entries of a path-like (:-delimited) variable from the
+# live shell, one per line, in order of precedence. With --index, each entry
+# is prefixed by its numeric (0-based) index.
+#
+# ves_envs: list all created sh-ves environments.
+#
+ves_list() {
+ index=0
+ case $1 in
+ --index)
+ index=1
+ shift
+ ;;
+ esac
-basename -a "$SHVES_ENV_DIR"/*
-#for i in "$ENV_DIR/*"; do
-# printf "%s\n" $(basename "$i")
-#done
+ if [ "$#" -lt 1 ]; then
+ printf "ERROR: No variable specified. usage: ves list [--index] <variable>\n" >&2
+ return 2
+ fi
+
+ if ! _shves_check_var_name "$1"; then
+ return 1
+ fi
+
+ # Read the value of the variable named in $1 from the live shell.
+ # For $1 = PATH, this expands to: var_value=${PATH}
+ eval "var_value=\${$1}"
+
+ if [ -z "$var_value" ]; then
+ return 0
+ fi
+
+ i=0
+ _shves_split_begin
+ for entry in $var_value; do
+ if [ "$index" -eq 1 ]; then
+ printf "%d %s\n" "$i" "$entry"
+ else
+ printf "%s\n" "$entry"
+ fi
+ i=$((i + 1))
+ done
+ _shves_split_end
+
+ return 0
+}
+
+ves_envs() {
+ for f in "$SHVES_ENV_DIR"/*; do
+ [ -f "$f" ] || continue
+ basename "$f"
+ done
+
+ return 0
+}
diff --git a/ves-prompt.sh b/ves-prompt.sh
index 1d79373..c870db2 100644..100755
--- a/ves-prompt.sh
+++ b/ves-prompt.sh
@@ -1,4 +1,9 @@
#!/bin/sh
+#
+# Print a prompt fragment naming the active sh-ves environment, for
+# inclusion in a POSIX shell's PS1 string. Prints nothing when no
+# environment is active.
+#
if [ $# -lt 1 ]; then
sym="VENV"
@@ -6,6 +11,6 @@ else
sym="$1"
fi
-if [ ! -z "$SHVES_ENV_NM" ]; then
- printf "(%s %s)" "$sym" "$VES_ENV_NM"
+if [ -n "$SHVES_ENV_NM" ]; then
+ printf "(%s %s)" "$sym" "$SHVES_ENV_NM"
fi
diff --git a/ves-run.sh b/ves-run.sh
new file mode 100644
index 0000000..57e52a0
--- /dev/null
+++ b/ves-run.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Run a one-off command inside an sh-ves environment without activating it
+# in the current shell. The activation happens in a subshell, so the parent
+# shell's environment is untouched and no deactivation is needed.
+#
+ves_run() {
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves run <env> <command...>\n" >&2
+ return 2
+ fi
+
+ env_name="$1"
+ shift
+
+ if ! _shves_check_env_name "$env_name"; then
+ return 1
+ fi
+
+ if ! _shves_check_env_exists "$env_name"; then
+ return 1
+ fi
+
+ (
+ SHVES_ENV_NM=""
+ SHVES_SAVED_VARS=""
+ ves_activate "$env_name" > /dev/null || exit 1
+ "$@"
+ )
+}
diff --git a/ves-show.sh b/ves-show.sh
new file mode 100644
index 0000000..b50487a
--- /dev/null
+++ b/ves-show.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# Print the variables stored in an sh-ves environment, one VAR=value per
+# line. With no argument, shows the currently active environment.
+#
+ves_show() {
+ env=""
+ if [ "$#" -ge 1 ]; then
+ env="$1"
+ fi
+
+ if ! env=$(_shves_resolve_env "$env"); then
+ return 1
+ fi
+
+ while IFS= read -r line; do
+ case $line in
+ export_var:*)
+ printf "%s\n" "${line#export_var:}"
+ ;;
+ esac
+ done < "$SHVES_ENV_DIR/$env"
+
+ return 0
+}
diff --git a/ves-unset.sh b/ves-unset.sh
new file mode 100644
index 0000000..d25a178
--- /dev/null
+++ b/ves-unset.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+#
+# Remove a variable from an sh-ves environment entirely; the inverse of
+# ves export. If the target environment is currently active, the live
+# variable is restored to its pre-activation value, just as ves deactivate
+# would do.
+#
+ves_unset() {
+ env=""
+ case $1 in
+ --env=*)
+ env="${1#--env=}"
+ shift
+ ;;
+ esac
+
+ if [ "$#" -lt 1 ]; then
+ printf "ERROR: No variable specified. usage: ves unset [--env=<name>] <variable>\n" >&2
+ return 2
+ fi
+
+ var="$1"
+
+ if ! env=$(_shves_resolve_env "$env"); then
+ return 1
+ fi
+
+ if ! _shves_check_var_name "$var"; then
+ return 1
+ fi
+
+ fname="$SHVES_ENV_DIR/$env"
+ if ! _shves_has_var "$fname" "$var"; then
+ printf "ERROR: Variable [%s] does not exist in environment [%s].\n" "$var" "$env" >&2
+ return 1
+ fi
+
+ _shves_del_var "$fname" "$var"
+
+ # If the target environment is active and the variable was part of the
+ # activation save-state, restore the pre-activation value in the live
+ # shell and drop the variable from the bookkeeping.
+ if [ "$env" = "$SHVES_ENV_NM" ] && _shves_is_saved "$var"; then
+ _shves_restore_var "$var"
+
+ new_saved=""
+ for v in $SHVES_SAVED_VARS; do
+ if [ "$v" != "$var" ]; then
+ new_saved="$new_saved $v"
+ fi
+ done
+ SHVES_SAVED_VARS="$new_saved"
+ export SHVES_SAVED_VARS
+ fi
+
+ return 0
+}
diff --git a/ves-var-add.sh b/ves-var-add.sh
index d36647c..01bc457 100755
--- a/ves-var-add.sh
+++ b/ves-var-add.sh
@@ -1,78 +1,79 @@
#!/bin/sh
+#
+# Prepend an entry to a path-like (:-delimited) variable within an sh-ves
+# environment, or append it with --append. Values not beginning with / or
+# ./ are expanded relative to the sh-ves data directories (see
+# _shves_expand_path). If the target environment is currently active, the
+# variable is also updated in the live shell.
+#
ves_var_add() {
- if [ "$#" -lt 2 ]; then
- printf "ERROR: Insufficient arguments provided.\n"
- return 2
- fi
-
env=""
+ append=0
while :; do
case $1 in
--env=*)
- env=$( echo $1 | cut -d "=" -f 2 )
+ env="${1#--env=}"
shift
- break
;;
- --)
+ --append)
+ append=1
shift
- break
- ;;
-
- -?*)
- printf "ERROR: Invalid option [%s]\n" $1 > /dev/stderr
- return 2
;;
-
*)
break
+ ;;
esac
- shift
done
- active_env=0
- if [ -z "$env" ]; then
- if ! [ -z "$SHVES_ENV_NM"]; then
- env="$SHVES_ENV_NM"
- active_env=1
- else
- printf "ERROR: No valid environment active or specified\n" > /dev/stderr
- return 1
- fi
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves var-add [--env=<name>] <variable> <value>\n" >&2
+ return 2
fi
- if ! _shves_check_env_name "$env"; then
+ var="$1"
+ value="$2"
+
+ if ! env=$(_shves_resolve_env "$env"); then
return 1
fi
- if ! _shves_check_env_exists "$env"; then
- return 1;
+ if ! _shves_check_var_name "$var"; then
+ return 1
+ fi
+
+ value=$(_shves_expand_path "$var" "$value")
+
+ # Get the current value from the environment file and prepend the new
+ # entry to it. If the variable is not stored in the environment at all,
+ # seed it from the live shell value first, so that prepending to PATH
+ # and friends never truncates them. An explicitly stored empty value
+ # (as created by ves create --override) is respected, however.
+ fname="$SHVES_ENV_DIR/$env"
+ if _shves_has_var "$fname" "$var"; then
+ var_value=$(_shves_get_var "$fname" "$var")
+ else
+ # Read the live shell value of the variable named in $var,
+ # defaulting to empty if it is unset. For $var = PATH, this
+ # expands to: var_value=${PATH:-}
+ eval "var_value=\${$var:-}"
fi
- # First, we will get the variable value from the environment file and
- # create our new version of it.
- fname="$SHVES_ENV_DIR"/"$env"
- var_value=$(grep "export_var:$var=" $SHVES_ENV_DIR/test2 | cut -d '=' -f 2)
if [ -z "$var_value" ]; then
- var_value="$2"
+ var_value="$value"
+ elif [ "$append" -eq 1 ]; then
+ var_value="$var_value:$value"
else
- var_value="$var_value:$2"
+ var_value="$value:$var_value"
fi
- # And then update the file to use the new version.
- sed -i "s/export_var:$var=.*"/export_var:$var=$var_value"/" "$fname"
+ _shves_set_var "$fname" "$var" "$var_value"
# If we are updating the currently active environment, export the
- # new value as well,
- if [ $active_env -eq 1 ]; then
- export "$var"="$var_value"
+ # new value as well.
+ if [ "$env" = "$SHVES_ENV_NM" ]; then
+ _shves_live_export "$var" "$var_value"
fi
- # Ah, bash would have made this so much nicer... alas
- # if [ -z ${!1+x} ]; then ...
-# if [ "$active_env" -eq 1 ] && eval [ -z \${$1+x} ]; then
-# printf "ERROR: Specified variable [%s] does not exist in environment.\n" $1 > /dev/stderr
-# printf "\tYou can create it with\n\t ves export %s\n " $1 > /dev/stderr
-# return 2
-# fi
+ return 0
}
diff --git a/ves-var-rm.sh b/ves-var-rm.sh
index c0b2ecd..cc0a1e1 100644..100755
--- a/ves-var-rm.sh
+++ b/ves-var-rm.sh
@@ -1,4 +1,72 @@
#!/bin/sh
+#
+# Remove an entry from a path-like (:-delimited) variable within an sh-ves
+# environment. The value is expanded with the same rules as ves var-add, so
+# entries can be removed using the same form they were added with. If the
+# target environment is currently active, the variable is also updated in
+# the live shell.
+#
ves_var_rm() {
- echo "not implemented"
+ env=""
+ case $1 in
+ --env=*)
+ env="${1#--env=}"
+ shift
+ ;;
+ esac
+
+ if [ "$#" -lt 2 ]; then
+ printf "ERROR: Insufficient arguments. usage: ves var-rm [--env=<name>] <variable> <value>\n" >&2
+ return 2
+ fi
+
+ var="$1"
+ value="$2"
+
+ if ! env=$(_shves_resolve_env "$env"); then
+ return 1
+ fi
+
+ if ! _shves_check_var_name "$var"; then
+ return 1
+ fi
+
+ value=$(_shves_expand_path "$var" "$value")
+
+ fname="$SHVES_ENV_DIR/$env"
+ if ! _shves_has_var "$fname" "$var"; then
+ printf "ERROR: Variable [%s] does not exist in environment [%s].\n" "$var" "$env" >&2
+ return 1
+ fi
+
+ var_value=$(_shves_get_var "$fname" "$var")
+
+ # Rebuild the :-delimited list, dropping every element matching the
+ # value to remove.
+ new_value=""
+ found=0
+ _shves_split_begin
+ for entry in $var_value; do
+ if [ "$entry" = "$value" ]; then
+ found=1
+ elif [ -z "$new_value" ]; then
+ new_value="$entry"
+ else
+ new_value="$new_value:$entry"
+ fi
+ done
+ _shves_split_end
+
+ if [ "$found" -eq 0 ]; then
+ printf "ERROR: Entry [%s] not present in variable [%s].\n" "$value" "$var" >&2
+ return 1
+ fi
+
+ _shves_set_var "$fname" "$var" "$new_value"
+
+ if [ "$env" = "$SHVES_ENV_NM" ]; then
+ _shves_live_export "$var" "$new_value"
+ fi
+
+ return 0
}
diff --git a/ves.fish b/ves.fish
new file mode 100644
index 0000000..3315f61
--- /dev/null
+++ b/ves.fish
@@ -0,0 +1,104 @@
+# ves.fish
+#
+# fish shell shim for sh-ves. Runs each ves command in a POSIX sh subshell
+# against the real implementation, then diffs the subshell's resulting
+# environment against fish's and replays the changes with set -gx / set -e.
+# The activation save-state is carried in exported SHVES_SAVED_* variables,
+# so it survives between subshell invocations.
+#
+# Install this file to ~/.config/fish/functions/ves.fish (make install does
+# this automatically when fish is present).
+
+function ves --description 'sh-ves: Bourne Shell Virtual Environment System'
+ if not set -q SHVES_SCRIPTS_DIR
+ set -gx SHVES_SCRIPTS_DIR $HOME/.local/bin/ves_scripts
+ end
+
+ if not test -f "$SHVES_SCRIPTS_DIR/ves-init.sh"
+ printf "ERROR: sh-ves scripts not found in [%s]. Is sh-ves installed?\n" "$SHVES_SCRIPTS_DIR" >&2
+ return 1
+ end
+
+ set -l dump (mktemp)
+
+ # Resolve env(1) to an absolute path up front: the command may rewrite
+ # PATH inside the subshell, which would otherwise make the dump fail.
+ set -l envbin (command -v env)
+ if test -z "$envbin"
+ set envbin /usr/bin/env
+ end
+
+ # Run the real command, then dump the subshell's final environment
+ # NUL-delimited (values may contain newlines) for replay into fish.
+ VES_ENV_DUMP=$dump VES_ENV_BIN=$envbin sh -c '
+ . "$SHVES_SCRIPTS_DIR"/ves-init.sh
+ ves "$@"
+ _shves_status=$?
+ "$VES_ENV_BIN" -0 > "$VES_ENV_DUMP"
+ exit $_shves_status
+ ' ves $argv
+ set -l ret $status
+
+ # An empty dump means the subshell could not write its environment at
+ # all; replaying it would wrongly erase every exported variable.
+ if not test -s $dump
+ printf "ERROR: failed to capture subshell environment, shell state unchanged.\n" >&2
+ rm -f $dump
+ return 1
+ end
+
+ # Variables managed by the wrapper machinery itself, plus per-process
+ # bookkeeping that should never be replayed into the parent shell.
+ set -l skip VES_ENV_DUMP VES_ENV_BIN PWD OLDPWD SHLVL _
+
+ # fish list-style path variables: replay these split on ':' so fish
+ # keeps treating them as lists.
+ set -l pathvars PATH CDPATH MANPATH
+
+ set -l seen
+ for entry in (string split0 < $dump)
+ set -l kv (string split -m1 = -- $entry)
+ set -l name $kv[1]
+ set -l value $kv[2]
+
+ # entries that are not well-formed variable names (e.g. exported
+ # multiline values' continuation, or odd env entries) are skipped
+ if not string match -qr '^[a-zA-Z_][a-zA-Z0-9_]*$' -- $name
+ continue
+ end
+ if contains -- $name $skip
+ continue
+ end
+
+ set -a seen $name
+
+ if contains -- $name $pathvars
+ set -l new (string split : -- $value)
+ if not set -q $name
+ set -gx $name $new
+ else if test "$new" != "$$name"
+ set -gx $name $new
+ end
+ else
+ if not set -q $name
+ set -gx $name $value
+ else if test "$value" != "$$name"
+ set -gx $name $value
+ end
+ end
+ end
+
+ # Anything fish exports that vanished from the subshell environment
+ # was unset by the command (e.g. by ves deactivate).
+ for name in (set -nx)
+ if contains -- $name $skip
+ continue
+ end
+ if not contains -- $name $seen
+ set -e $name
+ end
+ end
+
+ rm -f $dump
+ return $ret
+end
diff --git a/ves.sh b/ves.sh
index 28325db..99049a4 100644..100755
--- a/ves.sh
+++ b/ves.sh
@@ -1,60 +1,119 @@
#!/bin/sh
+#
+# Global wrapper function dispatching to the various sh-ves subcommands.
+#
ves() {
- if [ "$#" -lt 1 ]; then
- printf "ERROR: Please specify a command.\n"
- return
+ if [ -z "$SH_VES_INIT" ]; then
+ printf "ERROR: sh-ves is not initialized. Source ves-init.sh first.\n" >&2
+ return 1
fi
+ if [ "$#" -lt 1 ]; then
+ printf "ERROR: Please specify a command.\n" >&2
+ ves_help >&2
+ return 1
+ fi
# extract the appropriate command
case $1 in
create)
shift
- ves_create $@
- break
+ ves_create "$@"
;;
delete)
shift
- ves_delete $@
- break
+ ves_delete "$@"
;;
var-add)
shift
- ves_var_add $@
- break
+ ves_var_add "$@"
;;
var-rm)
shift
- ves_var_rm $@
- break
+ ves_var_rm "$@"
;;
export)
shift
- ves_export $@
- break
+ ves_export "$@"
+ ;;
+ unset)
+ shift
+ ves_unset "$@"
+ ;;
+ show)
+ shift
+ ves_show "$@"
+ ;;
+ copy)
+ shift
+ ves_copy "$@"
+ ;;
+ rename)
+ shift
+ ves_rename "$@"
+ ;;
+ run)
+ shift
+ ves_run "$@"
+ ;;
+ switch)
+ shift
+ ves_switch "$@"
;;
list)
shift
- "$SHVES_BIN"/ves-list.sh $@
- break
+ ves_list "$@"
+ ;;
+ envs)
+ shift
+ ves_envs "$@"
;;
activate)
shift
- ves_activate $@
- break
+ ves_activate "$@"
;;
deactivate)
shift
- ves_deactivate $@
- break
+ ves_deactivate "$@"
;;
prompt)
shift
- . $SHVES_BIN/ves-prompt.sh $@
- break
+ "$SHVES_BIN"/ves-prompt.sh "$@"
+ ;;
+ help|--help|-h)
+ ves_help
;;
*)
- printf "ERROR: Invalid command [%s]\n" $1 > /dev/stderr
- return
+ printf "ERROR: Invalid command [%s]\n" "$1" >&2
+ ves_help >&2
+ return 1
+ ;;
esac
}
+
+ves_help() {
+ cat <<'EOF'
+usage: ves <command> [arguments]
+
+commands:
+ create [--override] <name> create a new environment
+ delete <name> delete an environment
+ copy <src> <dst> copy an environment
+ rename <old> <new> rename an environment
+ activate <name> activate an environment
+ deactivate deactivate the active environment
+ switch <name> deactivate, then activate <name>
+ run <name> <command...> run a command inside an environment
+ export [--env=<name>] <var> <val> set a variable in an environment
+ unset [--env=<name>] <var> remove a variable from an environment
+ show [<name>] show the variables of an environment
+ var-add [--env=<name>] [--append] <var> <val>
+ prepend (or append) an entry to a
+ path-like variable
+ var-rm [--env=<name>] <var> <val> remove an entry from a path-like variable
+ list [--index] <var> list the entries of a path-like variable
+ envs list all environments
+ prompt [symbol] print a prompt fragment for PS1
+ help show this message
+EOF
+}