-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gender-equality-community/configurability
Autogenerate auto responses
- Loading branch information
Showing
11 changed files
with
215 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This file is used to generate the file `config.gen.go`; we compile these phrases into the binary | ||
# (rather than allowing for a separate runtime configuration options) purely because it makes | ||
# distribution and start up a little easier for us. | ||
# | ||
# The only reason to have this seperate file, rather than storing this in source, is so these | ||
# things can be edited and maintained by people who feel less confident editing source code. | ||
|
||
[responses] | ||
# Greeting Response is sent when a recipient sends a message that looks vaguely like | ||
# a greeting. | ||
# | ||
# To understand what this might look like, take a look in phrases.go | ||
greeting = "Hello, and welcome to the Anonymous GEC Advisor. What's on your mind?" | ||
|
||
# Thank You response is sent when a recipient sends us a message. | ||
# | ||
# To keep this from spamming the hell out of people, we only send a maximum of 1 | ||
# response per 30 minutes. | ||
# | ||
# Essentially, when a message comes in, we check whether we've responded in the last | ||
# 30 minutes and if we haven't then we send another | ||
thankyou = "Thank you for your message, please provide as much information as you're comfortable sharing and we'll get back to you as soon as we can." | ||
|
||
# Disclaimer response is sent to ensure recipients don't send us things we can't, or aren't allowed to, deal with. | ||
# | ||
# In much the same way as we only send a Thank You every 30 mins or less, we only send the | ||
# disclaimer once every 24 hours. | ||
disclaimer = "DISCLAIMER: This is not an incident reporting service. If you believe you're being subjected to bullying, harassment, or misconduct then we cannot escalate on your behalf but we can advise you on your next steps." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/dave/jennifer/jen" | ||
) | ||
|
||
const output = "config.gen.go" | ||
|
||
var config = flag.String("f", "config.toml", "configuration file to load") | ||
|
||
// Responses holds our default responses | ||
type Responses struct { | ||
Greeting string | ||
Thankyou string | ||
Disclaimer string | ||
} | ||
|
||
// Config holds the loaded configuration from our input file | ||
type Config struct { | ||
Responses Responses | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
c, err := readToml(*config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
f, err := generate(c) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(f) | ||
} | ||
|
||
func readToml(f string) (c Config, err error) { | ||
//#nosec | ||
cf, err := os.ReadFile(f) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = toml.Decode(string(cf), &c) | ||
|
||
return | ||
} | ||
|
||
func generate(c Config) (out string, err error) { | ||
f := jen.NewFile("main") | ||
f.HeaderComment("Code generated from gen/main.go DO NOT EDIT ") | ||
|
||
f.Comment("Greeting response is sent when a recipient sends a message sends us a greeting") | ||
f.Const().Id("greetingResponse").Op("=").Lit(c.Responses.Greeting) | ||
|
||
f.Comment("Thank You response is sent when a recipient sends us a message and is capped at a max of 1 per 30 mins") | ||
f.Const().Id("thankyouResponse").Op("=").Lit(c.Responses.Thankyou) | ||
|
||
f.Comment("Disclaimer response is sent to ensure recipients don't send us stuff we can't deal with.") | ||
f.Const().Id("disclaimerResponse").Op("=").Lit(c.Responses.Disclaimer) | ||
|
||
buf := strings.Builder{} | ||
|
||
err = f.Render(&buf) | ||
out = buf.String() | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
var ( | ||
happyConfig = Config{ | ||
Responses: Responses{ | ||
Greeting: "Hi there!", | ||
Thankyou: "Thanks!", | ||
Disclaimer: "We can't do that!", | ||
}, | ||
} | ||
) | ||
|
||
func TestReadToml(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
fn string | ||
expect Config | ||
expectError bool | ||
}{ | ||
{"Config file does not exist", "testdata/nonsuch.toml", Config{}, true}, | ||
{"Config file exists but is empty", "testdata/empty.toml", Config{}, false}, | ||
{"Config file exists and has content", "testdata/config.toml", happyConfig, false}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
got, err := readToml(test.fn) | ||
if err != nil && !test.expectError { | ||
t.Errorf("unexpected error: %v", err) | ||
} else if err == nil && test.expectError { | ||
t.Error("expected error") | ||
} | ||
|
||
if !reflect.DeepEqual(test.expect, got) { | ||
t.Errorf("expected %#v, received %#v", test.expect, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenerate(t *testing.T) { | ||
expect := `// Code generated from gen/main.go DO NOT EDIT | ||
package main | ||
// Greeting response is sent when a recipient sends a message sends us a greeting | ||
const greetingResponse = "Hi there!" | ||
// Thank You response is sent when a recipient sends us a message and is capped at a max of 1 per 30 mins | ||
const thankyouResponse = "Thanks!" | ||
// Disclaimer response is sent to ensure recipients don't send us stuff we can't deal with. | ||
const disclaimerResponse = "We can't do that!" | ||
` | ||
got, err := generate(happyConfig) | ||
if err != nil { | ||
t.Errorf("expected error") | ||
} | ||
|
||
if expect != got { | ||
t.Errorf(cmp.Diff(expect, got)) | ||
} | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
t.Error("expected error") | ||
} | ||
}() | ||
|
||
main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[responses] | ||
greeting = "Hi there!" | ||
thankyou = "Thanks!" | ||
disclaimer = "We can't do that!" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters