Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 11, 2024
1 parent c376626 commit 7ee5b23
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"net/url"
"os"
Expand All @@ -14,7 +15,7 @@ import (
const version = "v3.7.2"

func usage() {
fmt.Fprint(os.Stderr, "Usage: changelog-from-release [flags]\n\n")
fmt.Fprint(os.Stderr, "Usage: changelog-from-release [flags] [FILE]\n\n")
flag.PrintDefaults()
}

Expand Down Expand Up @@ -97,8 +98,8 @@ func main() {
}
slog.Debug("Arguments parsed:", "config", cfg)

if flag.NArg() != 0 {
fail(fmt.Errorf("no argument is allowed but got %v", flag.Args()))
if flag.NArg() > 1 {
fail(fmt.Errorf("0 or 1 argument is allowed but got %v", flag.Args()))
}

url, err := remoteURL(*remote)
Expand All @@ -118,7 +119,22 @@ func main() {
fail(err)
}

if _, err := os.Stdout.Write(gen); err != nil {
var w io.Writer
if flag.NArg() == 0 {
slog.Debug("Writing the generated output to stdout")
w = os.Stdout
} else {
p := flag.Arg(0)
slog.Debug("Writing the generated output to file", "file", p)
f, err := os.Create(p)
if err != nil {
fail(err)
}
defer f.Close()
w = f
}

if _, err := w.Write(gen); err != nil {
fail(fmt.Errorf("could not write the generated changelog to stdout: %w", err))
}

Expand Down

0 comments on commit 7ee5b23

Please sign in to comment.