|
@@ -1,30 +1,281 @@
|
1
|
|
-Shellfu Bash library
|
2
|
|
-====================
|
|
1
|
+Shellfu
|
|
2
|
+=======
|
3
|
3
|
|
4
|
|
-Shellfu - Bash dot on steroids
|
|
4
|
+Shellfu - shell dot on steroids
|
5
|
5
|
|
|
6
|
+**Disclaimer**: Use at your own risk. See Project Status below for
|
|
7
|
+details.
|
6
|
8
|
|
7
|
|
-NOTES
|
8
|
|
------
|
9
|
9
|
|
10
|
|
- * if when using `debug -v` you stumble on a variable name
|
11
|
|
- that is *really* used internally by pretty.sh, routines
|
12
|
|
- pick up the internal ones which may easily confuse you.
|
13
|
|
- Examples are `src` or `caller`.
|
|
10
|
+Project Status and Versioning
|
|
11
|
+-----------------------------
|
14
|
12
|
|
15
|
|
- You can circumvent this by composing the debug string
|
16
|
|
- yourself, e.g.:
|
|
13
|
+ * Development stage is something between "experimental" and "alpha",
|
|
14
|
+ that is lot of tests are yet to be written and only real user is
|
|
15
|
+ its own author.
|
17
|
16
|
|
18
|
|
- debug "src='$src'"
|
|
17
|
+ * API (commands, options...) may be changed/removed as needed.
|
19
|
18
|
|
20
|
|
- in place of
|
|
19
|
+ * Although that will not happen within same version.
|
21
|
20
|
|
22
|
|
- debug -v src
|
|
21
|
+ * Branch 'last' contains only the last released version. Changes in
|
|
22
|
+ this branch can be API-breaking.
|
23
|
23
|
|
24
|
|
- This will be solved in the future (1.0.0?) probably by
|
25
|
|
- refactoring the internal names to some long, ugly but
|
26
|
|
- "safe" names. Now, however, readability of the internal
|
27
|
|
- code is of higher priority.
|
|
24
|
+ * Branch 'devel' is generally treated as development version and
|
|
25
|
+ opposed to 'last', is more likely to contain bugs.
|
28
|
26
|
|
29
|
|
- Note that there is no risk damage to data integrity, it's
|
30
|
|
- just that the routine is unable to show you desired value.
|
|
27
|
+ * Branch 'master' is used only for pre-release merging; don't build your
|
|
28
|
+ work on it.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+Introduction
|
|
32
|
+------------
|
|
33
|
+
|
|
34
|
+Most shell languages don't have libraries.
|
|
35
|
+
|
|
36
|
+You might think: "Are you kidding me? Libraries are for real serious
|
|
37
|
+programmers, and real serious programmers don't real-serious-program
|
|
38
|
+in shell! So how is that a problem lol."
|
|
39
|
+
|
|
40
|
+I mostly agree. Shell languages, by definition are command languages,
|
|
41
|
+basically meant to allow you kick around files, create directories and
|
|
42
|
+most importantly, launch *other* real serious programs.
|
|
43
|
+
|
|
44
|
+What shell languages can also be used for is gluing together some of
|
|
45
|
+these real serious programs so that you don't have to type everything
|
|
46
|
+over and over. These glue scripts are normally nothing serious, just
|
|
47
|
+copy this there and launch that with that piped in. Done.
|
|
48
|
+
|
|
49
|
+*Granted,* in some cases, you simply don't stop there. Sometimes you
|
|
50
|
+create one script here, one script there, one over there. When you
|
|
51
|
+maintain a system, sometimes you do write scripties here and there.
|
|
52
|
+That's OK, all of them are just few lines; trivial things with obvious
|
|
53
|
+purpose, right?
|
|
54
|
+
|
|
55
|
+But sometimes, you don't work alone. Sometimes there's bunch of other
|
|
56
|
+folks and you want to work together so that when you take vacation and
|
|
57
|
+something breaks, your colleagues can use your scripts instead of
|
|
58
|
+needing to do everything manually or call you.
|
|
59
|
+
|
|
60
|
+So even if it's not real serious web application or real serious kernel,
|
|
61
|
+*some* level of re-usability pays off.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+### OK so what ###
|
|
65
|
+
|
|
66
|
+It's not that it's not possible to build a library from a bunch of scripts
|
|
67
|
+and import all with the magical dot command, but let's be honest: you end
|
|
68
|
+up either with swarm of modules of varying style, maturity and quality
|
|
69
|
+*and* overlapping scopes, or one humongous beast-of-a-module with pack
|
|
70
|
+of mysterious functions and variables that everyone is scared to touch.
|
|
71
|
+
|
|
72
|
+Unless you take it seriously and start thinking *way* ahead from the
|
|
73
|
+start.
|
|
74
|
+
|
|
75
|
+Which is exactly what I did.
|
|
76
|
+
|
|
77
|
+Finding myself in swarm of varying scripties, constantly trying to keep
|
|
78
|
+them at least a bit tidy, I decided to stop and do it another way: Just
|
|
79
|
+try and see how much of the magic, the modularity, maintainability and
|
|
80
|
+discoverability that's so common in other platforms could be taken into
|
|
81
|
+the dark, blunt world of shell languages.
|
|
82
|
+
|
|
83
|
+Result of these efforts is called Shellfu.
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+### So what does it do? ###
|
|
87
|
+
|
|
88
|
+Shellfu, as a project consists of three parts:
|
|
89
|
+
|
|
90
|
+ * `shellfu()`, the shell function that lets you import a module from
|
|
91
|
+ known location just by name, therefore decoupling the *deployment*
|
|
92
|
+ from *the dependency*.
|
|
93
|
+
|
|
94
|
+ * Shellfu core library, providing few modules readily usable for
|
|
95
|
+ some of the most common tasks (printing messages for user, loading
|
|
96
|
+ basic INI files...).
|
|
97
|
+
|
|
98
|
+ Note that this "library" is currently extremely tiny with about
|
|
99
|
+ 4-6 modules. It's currently (July 2016) not clear what modules
|
|
100
|
+ should be added or removed and what should be the size, although
|
|
101
|
+ there are no plans to add a lot. Most of advantage of shellfu
|
|
102
|
+ is to be gained from writing *domain-specific* modules.
|
|
103
|
+
|
|
104
|
+ * Shellfu Style Guide, setting common standard for shell modules,
|
|
105
|
+ allowing you to collaborate on the code in an efficient manner
|
|
106
|
+ (in-line documentation, peer review, code readability...).
|
|
107
|
+
|
|
108
|
+ * As a bonus, *shellfu-doc*, building upon the Shellfu Style Guide,
|
|
109
|
+ allows your user to discover and read documentation of installed
|
|
110
|
+ modules (akin to *perldoc* or *pydoc*).
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+### Schizophrenia kicks in ###
|
|
114
|
+
|
|
115
|
+> *So are we talking POSIX shell, Bash or Zsh or what?*
|
|
116
|
+
|
|
117
|
+The basic principle is, albeit untested, meant to be extensible to most
|
|
118
|
+common *bourne-like* shell languages. In more specific sense, this
|
|
119
|
+means two things:
|
|
120
|
+
|
|
121
|
+ * *shellfu.sh*, the core part of the code should be compatible with
|
|
122
|
+ *dash*, the Debian Almquist Shell,
|
|
123
|
+
|
|
124
|
+ * although I don't expect various-language modules to co-exist,
|
|
125
|
+ *shellfu.sh* aims to be able to filter modules based on interpreter
|
|
126
|
+ (more on that later).
|
|
127
|
+
|
|
128
|
+ * and some modules of the core library will try to be POSIX/dash
|
|
129
|
+ compatible, which will make them loadable from all.
|
|
130
|
+
|
|
131
|
+Note that I'm kind of using terms "POSIX" and "dash" interchangeably.
|
|
132
|
+What I really mean is that since POSIX is rather vague in many places,
|
|
133
|
+and there are kajilions of POSIX shells, it's impractical to aim for
|
|
134
|
+*real* POSIX compatibility. Therefore, the "POSIX-oriented" code will
|
|
135
|
+be tested under *dash*, plus set of most common interpreters: `bash`,
|
|
136
|
+(add here).
|
|
137
|
+
|
|
138
|
+I reckon that's about as POSIXey as one can get without going mad.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+Bring it on!
|
|
142
|
+------------
|
|
143
|
+
|
|
144
|
+### Installation ###
|
|
145
|
+
|
|
146
|
+OK, less talk, more make. Dependencies are almost none: you'll need
|
|
147
|
+just bash and common Linux utilities. So basically, none.
|
|
148
|
+
|
|
149
|
+ git clone <insert-url-here>
|
|
150
|
+ cd shellfu
|
|
151
|
+ make
|
|
152
|
+ sudo make install
|
|
153
|
+
|
|
154
|
+You can also run our humble test suite (~10 seconds):
|
|
155
|
+
|
|
156
|
+ make test
|
|
157
|
+
|
|
158
|
+Alternatively you can ask author for highly non-standard but working
|
|
159
|
+RPM/DEB packages; availability is currently on-demand-happily.
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+### Your first script ###
|
|
163
|
+
|
|
164
|
+After installing, one of the first things you can do is:
|
|
165
|
+
|
|
166
|
+ shellfu-get path
|
|
167
|
+ shellfu-get --version
|
|
168
|
+
|
|
169
|
+That should return path to *shellfu.sh* and version, respectively.
|
|
170
|
+And that's also *all* this command does.
|
|
171
|
+
|
|
172
|
+Next, if you have also installed some modules, you can look around
|
|
173
|
+using `shellfu-doc`:
|
|
174
|
+
|
|
175
|
+ shellfu-doc --lsmods
|
|
176
|
+ shellfu-doc inigrep
|
|
177
|
+
|
|
178
|
+This is how you access documentation of modules available on the system.
|
|
179
|
+You can also access documentation of a modules directly by specifying
|
|
180
|
+path to the file (the path must contain slash):
|
|
181
|
+
|
|
182
|
+ shellfu-doc path/to/my/module.sh
|
|
183
|
+
|
|
184
|
+Note that shellfu-doc only works if the module follows Shellfu Style
|
|
185
|
+Guide, in particular the Docstrings section.
|
|
186
|
+
|
|
187
|
+Next, you can create your first Shellfu-Bash script:
|
|
188
|
+
|
|
189
|
+ #!/usr/bin/bash
|
|
190
|
+
|
|
191
|
+ . "$(shellfu-get path)" || exit 3
|
|
192
|
+
|
|
193
|
+ shellfu import pretty
|
|
194
|
+
|
|
195
|
+ think "this will be displayed if PRETTY_VERBOSE is 'true'"
|
|
196
|
+ debug "this will be displayed if PRETTY_DEBUG is 'true'"
|
|
197
|
+
|
|
198
|
+ fn() {
|
|
199
|
+ local some_var=some value
|
|
200
|
+ local some_array=(some items)
|
|
201
|
+ debug -v some_var some_array something_else
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ warn "I'm getting tired"
|
|
205
|
+
|
|
206
|
+ fn
|
|
207
|
+
|
|
208
|
+ die "ok I'm done"
|
|
209
|
+
|
|
210
|
+This is how above script works:
|
|
211
|
+
|
|
212
|
+ 1. shellfu-get provides path to the main shellfu.sh, which defines
|
|
213
|
+ `shellfu()` function (plus a bunch of boring variables).
|
|
214
|
+
|
|
215
|
+ 2. Using `shellfu import pretty`, we're importing module called "pretty".
|
|
216
|
+ shellfu will figure out where exactly it is.
|
|
217
|
+
|
|
218
|
+ 3. If we called `shellfu-doc pretty` in another terminal, we would find
|
|
219
|
+ out that among others, this module contains functions `think`, `debug`,
|
|
220
|
+ `warn` and `die`.
|
|
221
|
+
|
|
222
|
+ 4. Rest of it is just showing off how cool `pretty` is.
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+Calling the script is the same as any other bash scripts, what you can
|
|
226
|
+do though, is play with some pretty variables:
|
|
227
|
+
|
|
228
|
+ PRETTY_DEBUG=true ./my_script
|
|
229
|
+ PRETTY_VERBOSE=true ./my_script
|
|
230
|
+ PRETTY=color PRETTY_DEBUG=true ./my_script
|
|
231
|
+ PRETTY=notify PRETTY_VERBOSE=true ./my_script
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+### Your first module ###
|
|
235
|
+
|
|
236
|
+In its most basic form, a Shellfu module is nothing special. All you need
|
|
237
|
+to do is
|
|
238
|
+
|
|
239
|
+ 1. write a file with .sh extension with some bash functions/variables,
|
|
240
|
+
|
|
241
|
+ 2. save it somewhere,
|
|
242
|
+
|
|
243
|
+ 3. in your script, call `shellfu import my_module`--you can now access
|
|
244
|
+ the functions/variables you have defined.
|
|
245
|
+
|
|
246
|
+ 4. When running your script, set `SHELLFU_PATH` to the location where
|
|
247
|
+ my_module.sh is.
|
|
248
|
+
|
|
249
|
+To get the basic stuff, nothing more is really needed. If you want, however,
|
|
250
|
+your module be explorable by `shellfu-doc`, you need to follow Shellfu Style
|
|
251
|
+Guide.
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+And friends
|
|
255
|
+-----------
|
|
256
|
+
|
|
257
|
+Apart from Shellfu, there also other "sibling" projects helping you even
|
|
258
|
+furhter:
|
|
259
|
+
|
|
260
|
+ * TFkit -- to help create and run unit test suites for shell functions
|
|
261
|
+ and commands.
|
|
262
|
+
|
|
263
|
+ * MKit -- to help get you started with boring stuff like deployment,
|
|
264
|
+ packaging (deb and rpm) and release management (versioning,
|
|
265
|
+ changelogs).
|
|
266
|
+
|
|
267
|
+ * saturnin -- to help with creating more rich toolkits in form of
|
|
268
|
+ meta-commands.
|
|
269
|
+
|
|
270
|
+Not all of them are public, yet (but all of them *will be*). Well, the first
|
|
271
|
+two are actually integrated *in*, and used to maintain Shellfu codebase.
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+Contact
|
|
275
|
+-------
|
|
276
|
+
|
|
277
|
+Author of Shellfu idea, design and code is Alois Mahdal <netvor at vornet dot cz>.
|
|
278
|
+
|
|
279
|
+For general questions, visit `#shellfu` at freenode.
|
|
280
|
+
|
|
281
|
+More communication channels will be added at some point.
|