Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Cross-platform bash: Need an easy framework.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
1clue
Advocate
Advocate


Joined: 05 Feb 2006
Posts: 2569

PostPosted: Thu Sep 25, 2014 10:32 pm    Post subject: Cross-platform bash: Need an easy framework. Reply with quote

Hi,

For some years now I've been storing my personal preferences like aliases and utility scripts in an git, because I manage a bunch of servers and it's very difficult to manage without an scm.

Now I'm using Linux of various flavors (have been for years, not really a problem) as well as Mac OS X and Solaris, and Windows with Cygwin.

The problem is that while commands usually have the same name, the options almost never do across platforms.

So I'm trying to come up with a cross-platform configuration initializer.

Here's what I know:

  1. I want to add a single line to my .bashrc or .bash_profile or whatever the default for the OS is, and have everything managed from there.
  2. I want an alias to perform the same way (or as close as possible) on all platforms. For example, alias psg='ps axf | grep' on Linux, but the options are different on Solaris or Mac.
  3. name.sh would be the dafault file, name.Darwin or name.Whatever would be for Mac or Whatever.
  4. I manage far more Linux boxes than anything else, and sometimes the box has a gnu/linux version of a command and I want to use it even if it's nonstandard on that platform.
  5. I think I need an 'atom' for each alias or script, so I'll need an alias directory, and possibly a bin directory for each platform and a 'default' directory.


Anyway, what I'm thinking of is this:

I need some sort of core detector that checks what platform I'm on and sets things up the correct way. I want each 'atom' of configuration to be as simple as possible, and have the core engine be where any necessary complexity is.

I'd like to know if anyone else is doing something like this, and maybe use that instead of trying to roll my own.

Failing that, I'd like to set up a framework and maybe publish it for others.
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Fri Sep 26, 2014 2:03 am    Post subject: Reply with quote

That sounds awfully complicated; I'd use a case on $(uname -s)
Back to top
View user's profile Send private message
1clue
Advocate
Advocate


Joined: 05 Feb 2006
Posts: 2569

PostPosted: Fri Sep 26, 2014 2:27 am    Post subject: Reply with quote

You mean on each atom? I suppose that could work. Then I'd only need one directory for files to pull in.

Thanks.
Back to top
View user's profile Send private message
Ottre
Tux's lil' helper
Tux's lil' helper


Joined: 23 Dec 2012
Posts: 129

PostPosted: Fri Sep 26, 2014 2:47 am    Post subject: Re: Cross-platform bash: Need an easy framework. Reply with quote

1clue wrote:

I'd like to know if anyone else is doing something like this, and maybe use that instead of trying to roll my own.


I'm working on a bash script that takes advantage of new EAPI 6 features which make it easy to apply user patches, so the user can choose where his config are read from, eg ~/program.conf or ~/.config/program/ or ~/dmcrypt/config/program.conf

If I get it working, then I will add support for Gentoo prefix installs, eg portage on Mac OS X and FreeBSD. I could use a framework for differences in bash and the coreutilities.
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Fri Sep 26, 2014 5:16 pm    Post subject: Reply with quote

1clue wrote:
You mean on each atom?

There's only one "atom" in response to uname -s; but yes, on each possibility for your systems.
Quote:
I suppose that could work. Then I'd only need one directory for files to pull in.

Yup, eg:
Code:
case $(uname -s) in
   Linux) f=~/bashrc/linux
;; *) f=~/bashrc/default
esac
[[ -e $f ]] && source "$f"

So you'd just add the two for solaris and darwin, before the default (usually with a * and a couple | of | cases for proprietary stuff.)

Actually I'd be curious to see what you get for uname -s on the above two, if you don't mind sharing.
Back to top
View user's profile Send private message
1clue
Advocate
Advocate


Joined: 05 Feb 2006
Posts: 2569

PostPosted: Sat Sep 27, 2014 5:42 am    Post subject: Reply with quote

uname -s:

Mac OS X gives Darwin and solaris, at least the boxes I'm working with now, say something like SunOS. Cygwin is CygWin. The Sun boxes belong to a customer and they're pretty fussy about people just randomly logging on without a specific need. I'll check next time I have a reason to be on.

I've started on this, and it's actually turning out to be both simpler and more complicated than I had anticipated. There's no error handling right now so it's a bit of a house of cards, but you suck it into the user's environment with one line:

Code:

[[ -s "/etc/mystuff/profile" ]] && source "/etc/mystuff/profile"


I've got it down to a handful of files at each level, but they're not all populated right now:

  1. profile is the primary file at either level. It pulls in the global configurations, prompts, root/normal/sudo related stuff, sets global paths and then passes control to the local profile.
  2. homes sets up manually installed software mostly in /opt. This is a local-only file, where the software package is a single directory with a predictable structure.
  3. env sets up environment variables, aliases is self explanatory.
  4. There's also a bin directory in the global stuff, haven't done anything with it on a local setup.

    Here's my thoughts:

    1. git-based code in /usr/local/mystuff is more sophisticated and contains the lion's share.
    2. /etc/mystuff has site-specific and platform-specific code, no need to make it cross platform.
    3. .bashrc has user-specific alterations after the source line above.


    This is a bit more complicated than it has to be, but I'm tired of having to hack something together with every system, or copy-and-edit without getting the same goodness everywhere. This way I have about 6 steps and I have all my favorites, and everything can be updated with git.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum