Skip to content

Commit 1b1b1c3

Browse files
committed
Allow find/replace directive
1 parent 39827ee commit 1b1b1c3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

AutoSSHApp.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,33 @@ private static List<KeyValuePair<HostEntry, List<string>>> LoadCommands(string c
104104
string cleanedLine;
105105
bool isHostLine = false;
106106
int lineIndex = 0;
107+
Dictionary<string, string> replacers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
108+
107109
foreach (string line in File.ReadAllLines(commandFile))
108110
{
111+
// clean and trim
109112
cleanedLine = line.Trim();
110113
int pos = cleanedLine.IndexOf('#');
111114
if (pos >= 0)
112115
{
113116
cleanedLine = cleanedLine.Substring(0, pos).Trim();
114117
}
118+
119+
// replace any find and replace directives
120+
foreach (var kv in replacers)
121+
{
122+
cleanedLine = cleanedLine.Replace(kv.Key, kv.Value, StringComparison.OrdinalIgnoreCase);
123+
}
124+
125+
// look for defines ($...$=value)
126+
Match replacer = Regex.Match(cleanedLine, @"(?<name>\$[^\$]+\$)\s*=\s*(?<value>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
127+
if (replacer.Success)
128+
{
129+
replacers[replacer.Groups["name"].Value] = replacer.Groups["value"].Value;
130+
continue;
131+
}
132+
133+
// check if this is a host
115134
isHostLine = cleanedLine.StartsWith("$host", StringComparison.OrdinalIgnoreCase);
116135
if (currentEntry != null && (cleanedLine.Length == 0 || isHostLine))
117136
{
@@ -125,6 +144,7 @@ private static List<KeyValuePair<HostEntry, List<string>>> LoadCommands(string c
125144
}
126145
if (isHostLine)
127146
{
147+
// found a host, set the current entry
128148
string[] pieces = cleanedLine.Split(' ');
129149
if (pieces.Length < 3)
130150
{
@@ -161,6 +181,7 @@ private static List<KeyValuePair<HostEntry, List<string>>> LoadCommands(string c
161181
lines.AddRange(inheritedLines);
162182
if (currentEntry != null && lines.Count != 0)
163183
{
184+
// add commands for this host
164185
commands.Add(new KeyValuePair<HostEntry, List<string>>(currentEntry, lines));
165186
}
166187
return commands;

Readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ $host * *
2525
# update packages
2626
apt-get -q -y update
2727
28+
# define replacement
29+
$NEWFILES$ = newfiles
30+
2831
# ignore files by case insensitive regex, in this case any file called big_file[0-9]+\.bin
2932
$ignore big_file[0-9]+\.bin
3033
3134
# backup files and folders, always recursive, separate multiple with |
3235
$backup /var/www|/etc/apache2/apache2.conf|/etc/apache2/sites-enabled
3336
3437
# upload files and folders, always recursive, separate local and remote path with a ;
35-
$upload c:/files/newfiles;/home/user/newfiles
38+
$upload c:/files/newfiles;/home/user/$NEWFILES$ # use the macro from above
3639
3740
# hosts have a name and an address/dns name
3841
# when backing up, the hostname will be used for the folder name inside the root backup folder.

0 commit comments

Comments
 (0)