Skip to content

Commit

Permalink
Write result to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed May 13, 2024
1 parent b32413b commit cc4a1b8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
.env
changelog.json
1 change: 1 addition & 0 deletions changelog/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.
Name: release.Name,
Component: repository,
Description: release.Description,
Date: release.CreatedAt,
}
slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description.len", len(entry.Description))
changelog.AddEntry(entry)
Expand Down
1 change: 1 addition & 0 deletions changelog/output/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (c *Changelog) AddEntry(entry Entry) error {
Name: entry.Component,
Title: entry.Name,
Description: entry.Description,
Date: entry.Date,
})
break
}
Expand Down
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ const (
keyRepositoriesEnvironment = "REPOSITORIES"
keyOrganization = "organization"
keyOrganizationEnvironment = "ORGANIZATION"
keyOutputFile = "output-file"
keyOutputFileEnvironment = "OUTPUT_FILE"
)

type Config struct {
Verbose *int
GithubToken string
Repositories string
Organization string
OutputFile string
}

func New() (*Config, error) {
Expand All @@ -33,6 +36,7 @@ func New() (*Config, error) {
flag.StringVar(&c.GithubToken, keyGithubToken, lookupEnvOrString(keyGithubTokenEnvironment, ""), "The GitHub Token to use for authentication.")
flag.StringVar(&c.Repositories, keyRepositories, lookupEnvOrString(keyRepositoriesEnvironment, ""), "The repositories to generate changelog for.")
flag.StringVar(&c.Organization, keyOrganization, lookupEnvOrString(keyOrganizationEnvironment, ""), "The organization to generate changelog for.")
flag.StringVar(&c.OutputFile, keyOutputFile, lookupEnvOrString(keyOutputFileEnvironment, ""), "The output file to write the changelog to.")
flag.Parse()

level := slog.LevelError
Expand Down Expand Up @@ -62,6 +66,10 @@ func New() (*Config, error) {
return nil, fmt.Errorf("missing required environment variable: %s", keyOrganization)
}

if c.OutputFile == "" {
return nil, fmt.Errorf("missing required environment variable: %s", keyOutputFile)
}

return &c, nil
}

Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func main() {
os.Exit(1)
}

_, err = os.Stdout.Write(output)
// write changelog to file
err = os.WriteFile(c.OutputFile, output, 0644)
if err != nil {
slog.Error("unable to write changelog", "error", err)
slog.Error("unable to write changelog to file", "error", err)
os.Exit(1)
}
}

0 comments on commit cc4a1b8

Please sign in to comment.