refactor(cmd): use factories instead of global cobra command structs

This enables us to create new commands for e2e tests.
This commit is contained in:
Julian Tölle 2025-06-15 16:27:31 +02:00
parent fc1ee70c28
commit f3269de8fe
4 changed files with 104 additions and 95 deletions

View file

@ -7,15 +7,12 @@ import (
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
)
var logger *slog.Logger
var rootCmd = &cobra.Command{
func NewRootCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "rp",
Short: "",
Long: ``,
@ -24,6 +21,11 @@ var rootCmd = &cobra.Command{
SilenceErrors: true, // We log manually with slog
}
cmd.AddCommand(newRunCommand())
return cmd
}
func version() string {
vcsrevision := "unknown"
vcsdirty := ""
@ -66,24 +68,13 @@ func Execute() {
// Make sure to stop listening on signals after receiving the first signal to hand control of the signal back
// to the runtime. The Go runtime implements a "force shutdown" if the signal is received again.
<-ctx.Done()
logger.InfoContext(ctx, "Received shutdown signal, stopping...")
slog.InfoContext(ctx, "Received shutdown signal, stopping...")
stop()
}()
err := rootCmd.ExecuteContext(ctx)
err := NewRootCmd().ExecuteContext(ctx)
if err != nil {
logger.ErrorContext(ctx, err.Error())
slog.ErrorContext(ctx, err.Error())
os.Exit(1)
}
}
func init() {
logger = slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.RFC3339,
}),
)
slog.SetDefault(logger)
}

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"log/slog"
"strings"
"github.com/spf13/cobra"
@ -15,11 +16,7 @@ import (
"github.com/apricote/releaser-pleaser/internal/versioning"
)
var runCmd = &cobra.Command{
Use: "run",
RunE: run,
}
func newRunCommand() *cobra.Command {
var (
flagForge string
flagBranch string
@ -28,22 +25,14 @@ var (
flagExtraFiles string
)
func init() {
rootCmd.AddCommand(runCmd)
runCmd.PersistentFlags().StringVar(&flagForge, "forge", "", "")
runCmd.PersistentFlags().StringVar(&flagBranch, "branch", "main", "")
runCmd.PersistentFlags().StringVar(&flagOwner, "owner", "", "")
runCmd.PersistentFlags().StringVar(&flagRepo, "repo", "", "")
runCmd.PersistentFlags().StringVar(&flagExtraFiles, "extra-files", "", "")
}
func run(cmd *cobra.Command, _ []string) error {
var cmd = &cobra.Command{
Use: "run",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
var err error
logger.DebugContext(ctx, "run called",
slog.DebugContext(ctx, "run called",
"forge", flagForge,
"branch", flagBranch,
"owner", flagOwner,
@ -59,18 +48,18 @@ func run(cmd *cobra.Command, _ []string) error {
switch flagForge {
case "gitlab":
logger.DebugContext(ctx, "using forge GitLab")
f, err = gitlab.New(logger, &gitlab.Options{
slog.DebugContext(ctx, "using forge GitLab")
f, err = gitlab.New(slog.Default(), &gitlab.Options{
Options: forgeOptions,
Path: fmt.Sprintf("%s/%s", flagOwner, flagRepo),
})
if err != nil {
logger.ErrorContext(ctx, "failed to create client", "err", err)
slog.ErrorContext(ctx, "failed to create client", "err", err)
return fmt.Errorf("failed to create gitlab client: %w", err)
}
case "github":
logger.DebugContext(ctx, "using forge GitHub")
f = github.New(logger, &github.Options{
slog.DebugContext(ctx, "using forge GitHub")
f = github.New(slog.Default(), &github.Options{
Options: forgeOptions,
Owner: flagOwner,
Repo: flagRepo,
@ -83,15 +72,25 @@ func run(cmd *cobra.Command, _ []string) error {
releaserPleaser := rp.New(
f,
logger,
slog.Default(),
flagBranch,
conventionalcommits.NewParser(logger),
conventionalcommits.NewParser(slog.Default()),
versioning.SemVer,
extraFiles,
[]updater.NewUpdater{updater.Generic},
)
return releaserPleaser.Run(ctx)
},
}
cmd.PersistentFlags().StringVar(&flagForge, "forge", "", "")
cmd.PersistentFlags().StringVar(&flagBranch, "branch", "main", "")
cmd.PersistentFlags().StringVar(&flagOwner, "owner", "", "")
cmd.PersistentFlags().StringVar(&flagRepo, "repo", "", "")
cmd.PersistentFlags().StringVar(&flagExtraFiles, "extra-files", "", "")
return cmd
}
func parseExtraFiles(input string) []string {

View file

@ -2,6 +2,7 @@ package main
import (
"github.com/apricote/releaser-pleaser/cmd/rp/cmd"
_ "github.com/apricote/releaser-pleaser/internal/log"
)
func main() {

18
internal/log/log.go Normal file
View file

@ -0,0 +1,18 @@
package log
import (
"log/slog"
"os"
"time"
"github.com/lmittmann/tint"
)
func init() {
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.RFC3339,
}),
))
}