Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created July 31, 2023 22:04
Show Gist options
  • Save kylelemons/07433e799a9c897a2d19731ead081927 to your computer and use it in GitHub Desktop.
Save kylelemons/07433e799a9c897a2d19731ead081927 to your computer and use it in GitHub Desktop.
Template examples for Go
lines := bufio.NewScanner(os.Stdin)
var (
newRepoName string
exportedName, rpcServiceName string
slugName, orgSlugName string
githubHandle string
teamName, costTeam, costProduct string
supportChannel string
)
for _, param := range []struct {
query string
out *string
validate *regexp.Regexp
help string
}{
{
query: "Repo name? (e.g. service-your-service-name)",
out: &newRepoName,
validate: validServiceName,
help: "Repo names must be kebab case and start with 'service-'",
},
{
query: "Service struct name? (e.g. YourServiceName)",
out: &exportedName,
validate: validCamelCase,
help: "Struct names must be camel case",
},
{
query: "Slug (short) name? (e.g. yourservice)",
out: &slugName,
validate: validSlugName,
help: "Slug names must be all lower case one word",
},
{
query: "Org Slug (short) name for proto package? (e.g. coreplatform)",
out: &orgSlugName,
validate: validSlugName,
help: "Slug names must be all lower case one word",
},
{
query: "RPC service name? (e.g. FooService)",
out: &rpcServiceName,
validate: validRPCServiceName,
help: "RPC Service names must be camel case and have the suffix 'Service'",
},
{
query: "GitHub Team name? (e.g. @org/your-team)",
out: &githubHandle,
validate: validGitHubHandle,
help: "Must be either a team name @org/your-team or indivudal @your-name",
},
{
query: "Team name? (e.g. Your Team)",
out: &teamName,
validate: anythingGoes,
help: "I'm not picky, give me something",
},
{
query: "Support / team channel? (e.g. team-support)",
out: &supportChannel,
validate: validSlackName,
help: "Must be a valid slack channel, without the leading `#`",
},
{
query: "Cost allocation team? (e.g. Cost Center)",
out: &costTeam,
validate: anythingGoes,
help: "Give me something, anything",
},
{
query: "Cost product team? (e.g. Cost Center Product)",
out: &costProduct,
validate: anythingGoes,
help: "Give me something, anything",
},
} {
fmt.Println(param.query)
for lines.Scan() {
line := lines.Text()
if !param.validate.MatchString(line) {
fmt.Printf("TRY AGAIN: %s (i.e. /%s/), got %q\n", param.help, param.validate, line)
fmt.Println(param.query) // print the question again in case they forgot or lost it in scrollback
continue
}
*param.out = line
break
}
}
if err := lines.Err(); err != nil {
ctxlog.For(ctx).Fatalf("Failed to read input: %s", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment