|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/AxeForging/pipekit/services" |
| 9 | + |
| 10 | + "github.com/urfave/cli" |
| 11 | +) |
| 12 | + |
| 13 | +// ChangelogCommand returns release-note generation helpers. |
| 14 | +func ChangelogCommand() cli.Command { |
| 15 | + return cli.Command{ |
| 16 | + Name: "changelog", |
| 17 | + Usage: "generate release notes from git commits", |
| 18 | + Subcommands: []cli.Command{ |
| 19 | + { |
| 20 | + Name: "generate", |
| 21 | + Usage: "generate changelog markdown for a git range", |
| 22 | + Flags: []cli.Flag{ |
| 23 | + cli.StringFlag{Name: "from", Usage: "start ref (exclusive)"}, |
| 24 | + cli.StringFlag{Name: "to", Value: "HEAD", Usage: "end ref (inclusive)"}, |
| 25 | + cli.BoolFlag{Name: "conventional", Usage: "group conventional commits"}, |
| 26 | + cli.StringFlag{Name: "format, f", Value: "markdown", Usage: "markdown or json"}, |
| 27 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 28 | + outputKeyFlag(), |
| 29 | + }, |
| 30 | + Action: func(c *cli.Context) error { |
| 31 | + markdown, entries, err := services.GenerateChangelog(services.ChangelogOptions{ |
| 32 | + From: c.String("from"), |
| 33 | + To: c.String("to"), |
| 34 | + Conventional: c.Bool("conventional"), |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + out := markdown |
| 41 | + switch c.String("format") { |
| 42 | + case "markdown", "": |
| 43 | + case "json": |
| 44 | + data, err := json.MarshalIndent(entries, "", " ") |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + out = string(data) + "\n" |
| 49 | + default: |
| 50 | + return cli.NewExitError("unknown format: "+c.String("format"), 1) |
| 51 | + } |
| 52 | + |
| 53 | + if outputKey := c.String("to-github-output"); outputKey != "" { |
| 54 | + return services.WriteToGitHubOutputValue(outputKey, out) |
| 55 | + } |
| 56 | + if path := c.String("output"); path != "" { |
| 57 | + return os.WriteFile(path, []byte(out), 0644) |
| 58 | + } |
| 59 | + fmt.Print(out) |
| 60 | + return nil |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + Name: "since-tag", |
| 65 | + Usage: "generate changelog markdown since the latest reachable tag", |
| 66 | + Flags: []cli.Flag{ |
| 67 | + cli.BoolFlag{Name: "conventional", Usage: "group conventional commits"}, |
| 68 | + cli.StringFlag{Name: "output, o", Usage: "write output to this file"}, |
| 69 | + outputKeyFlag(), |
| 70 | + }, |
| 71 | + Action: func(c *cli.Context) error { |
| 72 | + tag, err := services.GitPreviousTag() |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + markdown, _, err := services.GenerateChangelog(services.ChangelogOptions{ |
| 77 | + From: tag, |
| 78 | + To: "HEAD", |
| 79 | + Conventional: c.Bool("conventional"), |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + if outputKey := c.String("to-github-output"); outputKey != "" { |
| 85 | + return services.WriteToGitHubOutputValue(outputKey, markdown) |
| 86 | + } |
| 87 | + if path := c.String("output"); path != "" { |
| 88 | + return os.WriteFile(path, []byte(markdown), 0644) |
| 89 | + } |
| 90 | + fmt.Print(markdown) |
| 91 | + return nil |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
0 commit comments