Skip to content

Commit 4972ed3

Browse files
committed
feat: add documentation for adding new commands
1 parent 1084bf9 commit 4972ed3

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [Test Environment](getting-started/test-environment.md)
1111
* [Developer Commands](getting-started/developer-commands.md)
1212
* [Adding a new Game Server](getting-started/adding-a-new-game-server.md)
13+
* [Adding a new Command](getting-started/adding-a-new-command.md)
1314

1415
## Workflow
1516

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Adding a new Command
2+
3+
Adding new [commands](https://dev-docs.linuxgsm.com/technical/commands) to LinuxGSM is easier than you think and can be done in a few simple steps by following the instructions below.
4+
5+
## Create new command script
6+
7+
The first step is to create a new script in the `lgsm/modules` directory. The script should be named `command_[commandname].sh`, where `[commandname]` is the name of the command you want to add.
8+
9+
For example `lgsm/modules/command_hello_world.sh`:
10+
11+
```bash
12+
#!/bin/bash
13+
# LinuxGSM command_hello_world.sh module
14+
# Author: Daniel Gibbs
15+
# Contributors: http://linuxgsm.com/contrib
16+
# Website: https://linuxgsm.com
17+
# Description: Example Hello World command.
18+
19+
commandname="HELLO-WORLD" # Identifies the command in code
20+
commandaction="Hello World" # Tells the user which command is running
21+
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" # Used to get the filename of the current script
22+
fn_firstcommand_set # Track the first command executed
23+
24+
check.sh # Used to perform a variety of checks
25+
26+
# Place your code here
27+
fn_print_info_nl "Hello World!" # Print to console
28+
fn_script_log_info "Hello World!" # Log output of the script to the LinuxGSM log
29+
30+
core_exit.sh # Used to exit the script
31+
```
32+
33+
## Add neccessary checks
34+
35+
You should ensure that any necessary [checks](https://dev-docs.linuxgsm.com/technical/checks) are run before or during the execution of the command. This is to ensure that the command (or parts of the command) can only be executed if the server is in a state where the command can do its job.
36+
37+
To do this, you need to add your command name to the `allowed_commands_array` in `lgsm/modules/check.sh` for each check you want LinuxGSM to perform.
38+
39+
Some Examples:
40+
41+
- The check `check_logs.sh` is needed by almost every command to check if log files exist:
42+
43+
```bash
44+
allowed_commands_array=([...] HELLO-WORLD [...])
45+
for allowed_command in "${allowed_commands_array[@]}"; do
46+
if [ "${allowed_command}" == "${commandname}" ]; then
47+
check_logs.sh
48+
fi
49+
done
50+
```
51+
52+
- You may want to check that the server is running before your command does anything:
53+
54+
```bash
55+
allowed_commands_array=([...] HELLO-WORLD [...])
56+
for allowed_command in "${allowed_commands_array[@]}"; do
57+
if [ "${allowed_command}" == "${commandname}" ]; then
58+
check_status.sh
59+
fi
60+
done
61+
```
62+
63+
- You can also run checks directly in your command script:
64+
65+
```bash
66+
[...]
67+
68+
check_status.sh
69+
if [ "${status}" == "1" ]; then
70+
fn_print_info_nl "Server Online"
71+
else
72+
fn_print_info_nl "Server Offline"
73+
fi
74+
75+
76+
# Place your code here
77+
fn_print_info_nl "Hello World!" # Print to console
78+
fn_script_log_info "Hello World!" # Log output of the script to the LinuxGSM log
79+
80+
core_exit.sh # Used to exit the script
81+
```
82+
83+
### Adding custom checks
84+
85+
If you need to add a custom check, you can do so by creating a new check script in `lgsm/modules` and using it either directly in your command script or by adding it to `lgsm/modules/check.sh` as shown above.
86+
87+
## Add your command to the list of commands
88+
89+
Now you need to have a look at the file `lgsm/modules/core_getopt.sh` and add your command to the list of commands. To do this, follow these last 2 steps:
90+
91+
### Add a new command array
92+
93+
This array should contain the following information
94+
95+
- `User commands` - The commands (short form & long form) that the user can type to run the command
96+
- `Trigger comands` - The filename of the command
97+
- `Description` - A short description of the command
98+
99+
For example:
100+
101+
```bash
102+
cmd_hello_world=("hw;hello-world" "command_hello_world.sh" "Print Hello World!")
103+
```
104+
105+
### Add the command array to the list of commands
106+
107+
Append the command array to the list of commands called `currentopt`. Do this wherever you think it makes sense. If your command has any conditions (e.g. only available for certain games), you should check for these conditions before adding the command to the list.
108+
109+
Some examples:
110+
111+
- If you want to make the command available for the game `examplegame`(`eg`):
112+
113+
```bash
114+
# Hello World exclusive.
115+
if [ "${shortname}" == "eg" ]; then
116+
currentopt+=("${cmd_hello_world[@]}")
117+
fi
118+
```
119+
120+
- If you want to make the command available for multiple games `examplegame`(`eg`) & `helloworldgame`(`hwg`):
121+
122+
```bash
123+
# Hello World for examplegame and helloworldgame.
124+
if [ "${shortname}" == "eg" ] || [ "${shortname}" == "hwg" ]; then
125+
currentopt+=("${cmd_hello_world[@]}")
126+
fi
127+
```
128+
129+
- If you want to make the command available for all games that use a particular game engine (e.g. `source'):
130+
131+
```bash
132+
# Hello World for source engine games
133+
if [ "${engine}" == "source" ]; then
134+
currentopt+=("${cmd_hello_world[@]}")
135+
fi
136+
```
137+
138+
- If you want to make the command available for all games
139+
140+
```bash
141+
# Hello World
142+
currentopt+=("${cmd_hello_world[@]}")
143+
```

getting-started/adding-a-new-game-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ Not all game servers use SteamCMD. If this is the case a custom update module wi
7070

7171
## Custom Commands
7272

73-
Some game servers may require bespoke commands to complete tasks. Examples of this include Teamspeak 3 and Unreal Tournament 2004. Take a look at the `core_getopts.sh` module for examples of how to add commands.
73+
Some game servers may require bespoke commands to complete tasks. Examples of this include Teamspeak 3 and Unreal Tournament 2004. Take a look at [Adding a new command](https://dev-docs.linuxgsm.com/getting-started/adding-a-new-command) for examples of how to add new commands.
7474

0 commit comments

Comments
 (0)