Skip to content

A simple framework for sharing Bash profiles, reusable shell libraries, and commands across hosts and teams. Contains builtin libraries for common functions like logging, error handling, and assertions. Built with SRE / DevOps teams in mind.

License

Notifications You must be signed in to change notification settings

codeforester/base

Repository files navigation

What is Base?

Great things are done by a series of small things brought together.
- Vincent van Gogh

Base is a sharing platform for shell settings, libraries, and light-weight tools. It gives a structured way for Bash users to organize the following across multiple hosts:

  • .bash_profile
  • .bashrc
  • generic Bash libraries and commands
  • company specific Bash libraries, commands, and configuration
  • team specific Bash libraries, commands, and configuration
  • user specific settings (aliases, functions, Bash settings)
  • Bash libraries, commands, and configuration that are shared across teams

It can benefit anyone who engages with Mac/Linux command line to get their work done.

Requirements

Base needs Bash version 4.2 or above.

How can I get set up?

Set up is easy. Essentially, this is what you have to do:

  • Check out Base. The standard location for Base is $HOME/base. In case your git directory is elsewhere, symlink$HOME/gitto it or specify the path by settingBASE_HOMEin$HOME/.basercfile.
  • Consolidate your individual settings from your current.bash_profileand.bashrcinto$USER.shfile. Place this file underbase/userdirectory and check it in to git.
  • Make a backup of your.bash_profile.Replace this file with a symlink tobase/lib/bash_profile.
  • Make a backup of your.bashrc.Replace this file with a symlink tobase/lib/bashrc.

Log out and log back in or just doexec bashand you are all set!

Here is an example:

cd $HOME
mkdir git && cd git
git clone [email protected]:codeforester/base.git
cd $HOME
mv.bash_profile.bash_profile.safe && ln -sf $HOME/base/lib/bash_profile.bash_profile
mv.bashrc.bashrc.safe && ln -sf $HOME/base/lib/bashrc.bashrc
cp $USER.sh $HOME/base/user
cd $HOME/base
git add user/$USER.sh
git commit -m "Adding the initial version of $USER.sh"
git push

If you don't want to disturb your.bash_profileand.bashrc,you can still use Base in a less full-fledged manner. See the FAQ section for details.

How does Base work?

In a typical setting,.bashrcsources in$BASE_HOME/base_init.shwhich does the following:

  • source inlib/stdlib.sh
  • source incompany/lib/company.shif it exists
  • source incompany/lib/bashrcif it exists, if the shell is interactive
  • source inuser/$USER.shif it exists and if the shell is interactive
  • source in team specific bashrc fromteam/<team>/lib/bashrcfor each team defined inBASE_TEAMandBASE_SHARED_TEAMSvariables, if the shell is interactive. Note thatBASE_TEAMandBASE_SHARED_TEAMSshould be ideally set inuser/$USER.sh.
  • source in team specific library fromteam/<team>/lib/<team>.shfor each team defined inBASE_TEAMandBASE_SHARED_TEAMSvariables, if they exist
  • update$PATHto include the relevantbindirectories
    • $BASE_HOME/binis always added
    • $BASE_HOME/team/$BASE_TEAM/binis added if$BASE_TEAMis set inuser/$USER.sh
    • $BASE_HOME/team/$BASE_TEAM/binis added for each team defined in$BASE_SHARED_TEAMS(space-separated string), set inuser/$USER.sh
    • $BASE_HOME/company/binis always added

Directory structure

Screenshot of directory structure

Environment variables

  • BASE_HOME
  • BASE_DEBUG
  • BASE_TEAM
  • BASE_SHARED_TEAMS
  • BASE_OS
  • BASE_HOST
  • BASE_SOURCES

Functions exported by base_init.sh

  • import - sources in libraries from any place underBASE_HOMEdirectory
  • base_update - does agit pullon Base git directory; add it touser/<user>.shto "auto update" Base

FAQ

My git location is not$HOME/base.What should I do?

You can either

  • specify your Base location in$HOME/.baserc,like

    BASE_HOME=/path/to/base

  • symlink$HOME/baseto the right place

You need to do this on every host where you want Base.

I want to keep my personal settings private, and not in git. What should I do?

  • write a one-liner inuser/$USER.shlike this:

    source /path/to/your.settings

You would need to manage this file outside of Base.

I do want to use the default settings. What should I do?

Add this to youruser/$USER.shfile:

import lib/base_defaults.sh

I want to make sure I keep my Base repository updated always. How can I do it?

Add this to youruser/$USER.shfile:

base_update

I don't want to reorganize my.bash_profileor.bashrc.Can I still use Base?

Yes, you can, though you will lose the flexibility of keeping your.bash_profileand.bashrcsynced across hosts in case you are working with multiple hosts.

To turn on Base upon login, add this to your.bash_profile:

export BASE_HOME=/path/to/base
source "$BASE_HOME/base_init.sh"

after making sure you have the base repo checked out under$BASE_HOMEdirectory.

If you don't want to change your.bash_profileat all, you can still turn Base on and off as needed. First, make sure BASE_HOME is set appropriately, ideally in your.bash_profile.

Run this command to get a shell with Base turned on:

$BASE_HOME/base.sh shell

or $BASE_HOME/base.sh

Debugging

  • You can turn on debug mode by touching$HOME/.base_debugfile. You can also do the same by setting environment variableBASE_DEBUGto 1.
  • You can addset -xto$HOME/.basercfile to trace the execution in detail.

About

A simple framework for sharing Bash profiles, reusable shell libraries, and commands across hosts and teams. Contains builtin libraries for common functions like logging, error handling, and assertions. Built with SRE / DevOps teams in mind.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages