Skip to content

Commit 26bf733

Browse files
committed
Support for multiple template and output files
1 parent 8f1a1dd commit 26bf733

File tree

4 files changed

+73
-57
lines changed

4 files changed

+73
-57
lines changed

config/config.go

+27-20
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,40 @@ import (
99
)
1010

1111
const (
12-
keyAction = "ACTION"
13-
keyEnterprise = "ENTERPRISE"
14-
keyGithubToken = "GITHUB_TOKEN"
15-
keyTemplateFile = "TEMPLATE_FILE"
16-
keyVerbose = "VERBOSE"
17-
keyOutputFile = "OUTPUT_FILE"
18-
keyOwnDomains = "OWN_DOMAINS"
12+
keyAction = "action"
13+
kkeyActionEnvironment = "ACTION"
14+
keyEnterprise = "enterprise"
15+
keyEnterpriseEnvironment = "ENTERPRISE"
16+
keyGithubToken = "githubToken"
17+
keyGithubTokenEnvironment = "GITHUB_TOKEN"
18+
keyTemplateFiles = "template-files"
19+
keyTemplateFilesEnvironment = "TEMPLATE_FILES"
20+
keyOutputFiles = "output-files"
21+
keyOutputFilesEnvironment = "OUTPUT_FILES"
22+
keyVerbose = "verbose"
23+
keyVerboseEnvironment = "VERBOSE"
24+
keyOwnDomains = "own-domains"
25+
keyOwnDomainsEnvironment = "OWN_DOMAINS"
1926
)
2027

2128
type Config struct {
22-
Action string
23-
Enterprise string
24-
GithubToken string
25-
TemplateFile string
26-
OutputFile string
27-
OwnDomains string
29+
Action string
30+
Enterprise string
31+
GithubToken string
32+
TemplateFiles string
33+
OutputFiles string
34+
OwnDomains string
2835
}
2936

3037
func New() (*Config, error) {
3138
c := Config{}
32-
flag.StringVar(&c.Action, keyAction, lookupEnvOrString("ACTION", ""), "The action to perform.")
33-
flag.StringVar(&c.Enterprise, keyEnterprise, lookupEnvOrString("ENTERPRISE", ""), "The GitHub Enterprise to query for repositories.")
34-
flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString("GITHUB_TOKEN", ""), "The GitHub Token to use for authentication.")
35-
flag.StringVar(&c.TemplateFile, keyTemplateFile, lookupEnvOrString("TEMPLATE_FILE", "template/members.tpl"), "The template file to use for rendering the result.")
36-
flag.StringVar(&c.OutputFile, keyOutputFile, lookupEnvOrString("OUTPUT_FILE", ""), "The output file to write the result to.")
37-
flag.StringVar(&c.OwnDomains, keyOwnDomains, lookupEnvOrString("OWN_DOMAINS", ""), "The comma separated list of domains to consider as own domains.")
38-
verbose := flag.Int("verbose", lookupEnvOrInt(keyVerbose, 0), "Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE.")
39+
flag.StringVar(&c.Action, keyAction, lookupEnvOrString(kkeyActionEnvironment, ""), "The action to perform.")
40+
flag.StringVar(&c.Enterprise, keyEnterprise, lookupEnvOrString(keyEnterpriseEnvironment, ""), "The GitHub Enterprise to query for repositories.")
41+
flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString(keyGithubTokenEnvironment, ""), "The GitHub Token to use for authentication.")
42+
flag.StringVar(&c.TemplateFiles, keyTemplateFiles, lookupEnvOrString(keyTemplateFilesEnvironment, "template/members.tpl"), "The template file to use for rendering the result.")
43+
flag.StringVar(&c.OutputFiles, keyOutputFiles, lookupEnvOrString(keyOutputFilesEnvironment, ""), "The output file to write the result to.")
44+
flag.StringVar(&c.OwnDomains, keyOwnDomains, lookupEnvOrString(keyOwnDomainsEnvironment, ""), "The comma separated list of domains to consider as own domains.")
45+
verbose := flag.Int(keyVerbose, lookupEnvOrInt(keyVerboseEnvironment, 0), "Verbosity level, 0=info, 1=debug. Overrides the environment variable VERBOSE.")
3946

4047
level := slog.LevelInfo
4148
if *verbose > 0 {

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func main() {
1818
userlist.WithAction(c.Action),
1919
userlist.WithEnterprise(c.Enterprise),
2020
userlist.WithGithubToken(c.GithubToken),
21-
userlist.WithTemplateFile(c.TemplateFile),
22-
userlist.WithOutputFile(c.OutputFile),
21+
userlist.WithTemplateFiles(c.TemplateFiles),
22+
userlist.WithOutputFiles(c.OutputFiles),
2323
userlist.WithOwnDomains(c.OwnDomains),
2424
)
2525

userlist/constructor.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func WithAction(action string) func(*UserListConfig) {
1919
}
2020
}
2121

22-
func WithTemplateFile(templateFile string) func(*UserListConfig) {
22+
func WithTemplateFiles(templateFiles string) func(*UserListConfig) {
2323
return func(config *UserListConfig) {
24-
config.templateFile = templateFile
24+
config.templateFiles = strings.Split(templateFiles, ",")
2525
}
2626
}
2727

@@ -37,9 +37,9 @@ func WithGithubToken(githubToken string) func(*UserListConfig) {
3737
}
3838
}
3939

40-
func WithOutputFile(outputFile string) func(*UserListConfig) {
40+
func WithOutputFiles(outputFiles string) func(*UserListConfig) {
4141
return func(config *UserListConfig) {
42-
config.outputFile = outputFile
42+
config.outputFiles = strings.Split(outputFiles, ",")
4343
}
4444
}
4545

userlist/userlist.go

+40-31
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ const (
1717
)
1818

1919
type UserListConfig struct {
20-
action string
21-
templateFile string
22-
outputFile string
23-
enterprise string
24-
githubToken string
25-
validated bool
26-
loaded bool
27-
userList UserList
28-
ownDomains []string
20+
action string
21+
templateFiles []string
22+
outputFiles []string
23+
enterprise string
24+
githubToken string
25+
validated bool
26+
loaded bool
27+
userList UserList
28+
ownDomains []string
2929
}
3030

3131
type UserList struct {
@@ -64,25 +64,29 @@ func (c *UserListConfig) Validate() error {
6464
if c.action == "" {
6565
return errors.New("Action is required")
6666
}
67-
if c.templateFile == "" {
67+
if len(c.templateFiles) == 0 {
6868
return errors.New("Template is required")
6969
}
70+
if len(c.outputFiles) == 0 {
71+
return errors.New("Output File is required")
72+
}
7073
if c.enterprise == "" {
7174
return errors.New("Enterprise is required")
7275
}
7376
if c.githubToken == "" {
7477
return errors.New("Github Token is required")
7578
}
76-
if c.outputFile == "" {
77-
return errors.New("Output File is required")
79+
if len(c.templateFiles) != len(c.outputFiles) {
80+
return errors.New("Template and Output Files must have the same length")
7881
}
82+
7983
c.validated = true
8084
slog.Debug("Validated userlist",
8185
"action", c.action,
8286
"enterprise", c.enterprise,
83-
"template", c.templateFile,
87+
"templateFiles", c.templateFiles,
8488
"githubToken", "***",
85-
"outputFile", c.outputFile,
89+
"outputFiles", c.outputFiles,
8690
slog.Any("ownDomains", c.ownDomains))
8791
return nil
8892
}
@@ -120,25 +124,30 @@ func (ul *UserListConfig) Render() error {
120124
if !ul.loaded {
121125
return errors.New("UserList not loaded")
122126
}
123-
slog.Info("Rendering userlist", "template", ul.templateFile)
124-
templateFile, err := os.ReadFile(ul.templateFile)
125-
if err != nil {
126-
slog.Error("Unable to read template file", "error", err, "file", ul.templateFile)
127-
return err
128-
}
129127

130-
tmpl := template.Must(template.New("userlist").Parse(string(templateFile)))
131-
var buffer bytes.Buffer
132-
err = tmpl.Execute(&buffer, ul.userList)
133-
if err != nil {
134-
slog.Error("Unable to render userlist", "error", err)
135-
return err
136-
}
128+
for i, templateFileName := range ul.templateFiles {
129+
outputFileName := ul.outputFiles[i]
137130

138-
err = os.WriteFile(ul.outputFile, buffer.Bytes(), 0644)
139-
if err != nil {
140-
slog.Error("Unable to write userlist", "error", err, "file", ul.outputFile)
141-
return err
131+
slog.Info("Rendering userlist", "templateFile", templateFileName, "outputFile", outputFileName)
132+
templateFile, err := os.ReadFile(templateFileName)
133+
if err != nil {
134+
slog.Error("Unable to read template file", "error", err, "file", templateFileName)
135+
return err
136+
}
137+
138+
tmpl := template.Must(template.New("userlist").Parse(string(templateFile)))
139+
var buffer bytes.Buffer
140+
err = tmpl.Execute(&buffer, ul.userList)
141+
if err != nil {
142+
slog.Error("Unable to render userlist", "error", err)
143+
return err
144+
}
145+
146+
err = os.WriteFile(outputFileName, buffer.Bytes(), 0644)
147+
if err != nil {
148+
slog.Error("Unable to write userlist", "error", err, "file", outputFileName)
149+
return err
150+
}
142151
}
143152
return nil
144153
}

0 commit comments

Comments
 (0)