Skip to content

Commit cf0dd58

Browse files
authored
Merge pull request #152 from DannyBen/add/private-commands
Add support for private commands
2 parents a300f72 + 13af4c5 commit cf0dd58

File tree

11 files changed

+277
-7
lines changed

11 files changed

+277
-7
lines changed

examples/colors/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,18 @@ echo
6666
6767
```
6868

69+
### `$ NO_COLOR=1 ./colorly`
70+
71+
```shell
72+
Message Recevied:
73+
74+
=> hello colors
75+
==> hello colors
76+
===> hello colors
77+
78+
79+
80+
```
81+
6982

7083

examples/command-private/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src/connect_ftp_command.sh
2+
src/connect_ssh_command.sh
3+
src/download_command.sh
4+
src/initialize.sh
5+
src/upload_command.sh
6+
cli

examples/command-private/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Private Command Example
2+
3+
Demonstrates how to hide a command from the commands list.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
# ... now edit src/bashly.yml to match the example ...
10+
$ bashly generate
11+
```
12+
13+
<!-- include: src/connect_command.sh -->
14+
15+
-----
16+
17+
## `bashly.yml`
18+
19+
```yaml
20+
name: cli
21+
help: Sample application with private commands
22+
version: 0.1.0
23+
24+
commands:
25+
- name: connect
26+
short: c
27+
help: Connect to the metaverse
28+
29+
args:
30+
- name: protocol
31+
required: true
32+
allowed: [ftp, ssh]
33+
help: Protocol to use for connection
34+
35+
# These two commands will be hidden from the commands list, but still executable
36+
# and visible when running 'cli connect-ftp --help' or 'cli connect-ssh --help'
37+
- name: connect-ftp
38+
help: Connect via FTP
39+
private: true
40+
41+
- name: connect-ssh
42+
help: Connect via SSH
43+
private: true
44+
```
45+
46+
## `src/connect_command.sh`
47+
48+
```bash
49+
# Execute a subsequent (private) command based on the PROTOCOL argument
50+
protocol=${args[protocol]}
51+
cmd="./cli connect-$protocol"
52+
echo "=== Calling $cmd"
53+
$cmd
54+
```
55+
56+
57+
## Generated script output
58+
59+
### `$ ./cli`
60+
61+
```shell
62+
cli - Sample application with private commands
63+
64+
Usage:
65+
cli [command]
66+
cli [command] --help | -h
67+
cli --version | -v
68+
69+
Commands:
70+
connect Connect to the metaverse
71+
72+
73+
74+
```
75+
76+
### `$ ./cli -h`
77+
78+
```shell
79+
cli - Sample application with private commands
80+
81+
Usage:
82+
cli [command]
83+
cli [command] --help | -h
84+
cli --version | -v
85+
86+
Commands:
87+
connect Connect to the metaverse
88+
89+
Options:
90+
--help, -h
91+
Show this help
92+
93+
--version, -v
94+
Show version number
95+
96+
97+
98+
```
99+
100+
### `$ ./cli connect ftp`
101+
102+
```shell
103+
=== Calling ./cli connect-ftp
104+
# this file is located in 'src/connect_ftp_command.sh'
105+
# code for 'cli connect-ftp' goes here
106+
# you can edit it freely and regenerate (it will not be overwritten)
107+
args: none
108+
109+
110+
```
111+
112+
### `$ ./cli connect-ssh`
113+
114+
```shell
115+
# this file is located in 'src/connect_ssh_command.sh'
116+
# code for 'cli connect-ssh' goes here
117+
# you can edit it freely and regenerate (it will not be overwritten)
118+
args: none
119+
120+
121+
```
122+
123+
### `$ ./cli connect-ftp --help`
124+
125+
```shell
126+
cli connect-ftp - Connect via FTP
127+
128+
Usage:
129+
cli connect-ftp
130+
cli connect-ftp --help | -h
131+
132+
Options:
133+
--help, -h
134+
Show this help
135+
136+
137+
138+
```
139+
140+
141+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: cli
2+
help: Sample application with private commands
3+
version: 0.1.0
4+
5+
commands:
6+
- name: connect
7+
short: c
8+
help: Connect to the metaverse
9+
10+
args:
11+
- name: protocol
12+
required: true
13+
allowed: [ftp, ssh]
14+
help: Protocol to use for connection
15+
16+
# These two commands will be hidden from the commands list, but still executable
17+
# and visible when running 'cli connect-ftp --help' or 'cli connect-ssh --help'
18+
- name: connect-ftp
19+
help: Connect via FTP
20+
private: true
21+
22+
- name: connect-ssh
23+
help: Connect via SSH
24+
private: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Execute a subsequent (private) command based on the PROTOCOL argument
2+
protocol=${args[protocol]}
3+
cmd="./cli connect-$protocol"
4+
echo "=== Calling $cmd"
5+
$cmd

examples/command-private/test.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
# Cleanup before testing
4+
rm -f "src/connect_ftp_command.sh"
5+
rm -f "src/connect_ssh_command.sh"
6+
rm -f "src/download_command.sh"
7+
rm -f "src/initialize.sh"
8+
rm -f "src/upload_command.sh"
9+
10+
set -x
11+
12+
bashly generate
13+
14+
### Try Me ###
15+
16+
./cli
17+
./cli -h
18+
./cli connect ftp
19+
./cli connect-ssh
20+
./cli connect-ftp --help
21+

examples/environment-variables/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ echo "# you can edit it freely and regenerate (it will not be overwritten)"
6060
inspect_args
6161
6262
echo "environment:"
63-
echo "- API_KEY=$API_KEY"
64-
echo "- ENVIRONMENT=$ENVIRONMENT"
65-
echo "- MY_SECRET=$MY_SECRET"
63+
echo "- API_KEY=${API_KEY:-}"
64+
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
65+
echo "- MY_SECRET=${MY_SECRET:-}"
6666
6767
```
6868

examples/yaml/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ server:
6464
## `src/root_command.sh`
6565

6666
```bash
67-
filename=${args[filename]}
68-
variable=${args[variable]}
69-
prefix=${args[--prefix]}
67+
filename=${args[filename]:-}
68+
variable=${args[variable]:-}
69+
prefix=${args[--prefix]:-}
7070
7171
if [[ $variable ]]; then
7272
eval "$(yaml_load "$filename" "$prefix")"

lib/bashly/script/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Base
2323
long
2424
name
2525
parent_name
26+
private
2627
required
2728
short
2829
validate

lib/bashly/views/command/usage_commands.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
printf "<%= strings[:commands] %>\n"
44
% end
55
% maxlen = command_names.map(&:size).max
6-
% commands.each do |command|
6+
% commands.reject(&:private).each do |command|
77
% summary = command.summary
88
% summary = strings[:default_command_summary] % { summary: summary } if command.default
99
% if command.group

0 commit comments

Comments
 (0)