Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit cdec956

Browse files
committed
Fix announcements feature by changing template format from $FOO to {FOO} so it doesn't conflict with bash
Previously it wasn't broken, just confusing (to me and likely to others) since: ``` export ANNOUNCEMENT="Hello I'm $USERNAME" ``` Would have $USERNAME replaced by an empty string (since the environment variable isn't set). The fix is relatively easy (just escape the $ like `\$USERNAME`) but non-obvious. So it seemed better to just use {FOO} as the format since it doesn't conflict with that.
1 parent 90127d3 commit cdec956

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

docs/env.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ the bot is started. This is useful if you would like the bot to announce the fac
9393
a given team. The `ANNOUNCEMENT` environment variable supports a number of templating variables that will be instantiated
9494
based off of the current config. These are:
9595

96-
* `$USERNAME` will be replaced with the username of the bot
97-
* `$CURRENT_TEAM` will be replaced with the team that the message is being sent in
98-
* `$TEAMS` will be replaced with the comma separated list of teams that the bot is running in
96+
* `{USERNAME}` will be replaced with the username of the bot
97+
* `{CURRENT_TEAM}` will be replaced with the team that the message is being sent in
98+
* `{TEAMS}` will be replaced with the comma separated list of teams that the bot is running in
9999

100100
Examples:
101101

102102
```bash
103103
export ANNOUNCEMENT="SSH CA bot starting up..."
104-
export ANNOUNCEMENT="Hello! I'm $USERNAME and I'm an SSH bot! See github.com/keybase/bot-sshca for information on using Keybase for SSH."
105-
export ANNOUNCEMENT="Hello! I'm $USERNAME and I'm an SSH bot! I'm currently listening in $TEAMS."
106-
export ANNOUNCEMENT="Hello! I'm $USERNAME and I'm an SSH bot! Being in $CURRENT_TEAM will grant you SSH access to certain servers. Reach out to @dworken for more information."
104+
export ANNOUNCEMENT="Hello! I'm {USERNAME} and I'm an SSH bot! See github.com/keybase/bot-sshca for information on using Keybase for SSH."
105+
export ANNOUNCEMENT="Hello! I'm {USERNAME} and I'm an SSH bot! I'm currently listening in {TEAMS}."
106+
export ANNOUNCEMENT="Hello! I'm {USERNAME} and I'm an SSH bot! Being in {CURRENT_TEAM} will grant you SSH access to certain servers. Reach out to @dworken for more information."
107107
```
108108

109109
## Developer Options

src/keybaseca/bot/bot.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ func isConfiguredTeam(conf config.Config, teamName string, channelName string) b
152152

153153
func buildAnnouncement(template, username, currentTeam string, teams []string) string {
154154
replacements := map[string]string{
155-
"$USERNAME": username,
156-
"$CURRENT_TEAM": currentTeam,
157-
"$TEAMS": strings.Join(teams, ", "),
155+
"{USERNAME}": username,
156+
"{CURRENT_TEAM}": currentTeam,
157+
"{TEAMS}": strings.Join(teams, ", "),
158158
}
159159

160160
templatedMessage := template
161-
for template, val := range replacements {
162-
templatedMessage = strings.Replace(templatedMessage, template, val, -1)
161+
for templateStr, templateVal := range replacements {
162+
templatedMessage = strings.Replace(templatedMessage, templateStr, templateVal, -1)
163163
}
164164

165165
return templatedMessage

src/keybaseca/bot/bot_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ func TestBuildAnnouncement(t *testing.T) {
1212
require.Equal(t, "no templates",
1313
buildAnnouncement("no templates", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
1414
require.Equal(t, "repeated my_username my_username my_username",
15-
buildAnnouncement("repeated $USERNAME $USERNAME $USERNAME", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
15+
buildAnnouncement("repeated {USERNAME} {USERNAME} {USERNAME}", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
1616
require.Equal(t, "all my_username my_cur_team my_team1, my_team2",
17-
buildAnnouncement("all $USERNAME $CURRENT_TEAM $TEAMS", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
18-
require.Equal(t, "bogus $FOO",
19-
buildAnnouncement("bogus $FOO", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
20-
require.Equal(t, "double-is-not-escape $my_username",
21-
buildAnnouncement("double-is-not-escape $$USERNAME", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
17+
buildAnnouncement("all {USERNAME} {CURRENT_TEAM} {TEAMS}", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
18+
require.Equal(t, "bogus {FOO}",
19+
buildAnnouncement("bogus {FOO}", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
20+
require.Equal(t, "double-is-not-escape {my_username}",
21+
buildAnnouncement("double-is-not-escape {{USERNAME}}", "my_username", "my_cur_team", []string{"my_team1", "my_team2"}))
2222
}

tests/envFiles/test_env_1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export KEYBASE_PAPERKEY="$BOT_PAPERKEY"
77
export KEYBASE_USERNAME="$BOT_USERNAME"
88
export CHAT_CHANNEL="$SUBTEAM.ssh#ssh-provision"
99
export CA_KEY_LOCATION="/shared/keybase-ca-key"
10-
export ANNOUNCEMENTS="Hello my name is $USERNAME. This is $CURRENT_TEAM and the configured teams are $TEAMS"
10+
export ANNOUNCEMENT='Hello my name is {USERNAME}. This is {CURRENT_TEAM} and the configured teams are {TEAMS}'

0 commit comments

Comments
 (0)