releaser-pleaser/cmd/rp/cmd/run.go

96 lines
2.1 KiB
Go
Raw Normal View History

2024-07-12 14:51:24 +02:00
package cmd
import (
"strings"
2024-07-12 14:51:24 +02:00
"github.com/spf13/cobra"
rp "github.com/apricote/releaser-pleaser"
"github.com/apricote/releaser-pleaser/internal/commitparser/conventionalcommits"
"github.com/apricote/releaser-pleaser/internal/forge"
"github.com/apricote/releaser-pleaser/internal/forge/github"
"github.com/apricote/releaser-pleaser/internal/updater"
"github.com/apricote/releaser-pleaser/internal/versioning"
2024-07-12 14:51:24 +02:00
)
var runCmd = &cobra.Command{
Use: "run",
RunE: run,
}
var (
flagForge string
flagBranch string
flagOwner string
flagRepo string
flagExtraFiles string
2024-07-12 14:51:24 +02:00
)
func init() {
rootCmd.AddCommand(runCmd)
runCmd.PersistentFlags().StringVar(&flagForge, "forge", "", "")
2024-07-27 09:34:22 +02:00
runCmd.PersistentFlags().StringVar(&flagBranch, "branch", "main", "")
runCmd.PersistentFlags().StringVar(&flagOwner, "owner", "", "")
2024-07-12 14:51:24 +02:00
runCmd.PersistentFlags().StringVar(&flagRepo, "repo", "", "")
runCmd.PersistentFlags().StringVar(&flagExtraFiles, "extra-files", "", "")
2024-07-12 14:51:24 +02:00
}
2024-08-04 21:22:22 +02:00
func run(cmd *cobra.Command, _ []string) error {
2024-07-27 09:34:22 +02:00
ctx := cmd.Context()
2024-08-04 21:22:22 +02:00
logger.DebugContext(ctx, "run called",
"forge", flagForge,
"branch", flagBranch,
"owner", flagOwner,
"repo", flagRepo,
)
var f forge.Forge
2024-07-12 14:51:24 +02:00
forgeOptions := forge.Options{
2024-07-12 14:51:24 +02:00
Repository: flagRepo,
2024-07-27 09:34:22 +02:00
BaseBranch: flagBranch,
2024-07-12 14:51:24 +02:00
}
2024-08-23 22:35:06 +02:00
switch flagForge { // nolint:gocritic // Will become a proper switch once gitlab is added
// case "gitlab":
// f = rp.NewGitLab(forgeOptions)
2024-07-12 14:51:24 +02:00
case "github":
2024-08-04 21:22:22 +02:00
logger.DebugContext(ctx, "using forge GitHub")
f = github.New(logger, &github.Options{
Options: forgeOptions,
Owner: flagOwner,
Repo: flagRepo,
2024-07-27 09:34:22 +02:00
})
2024-07-12 14:51:24 +02:00
}
extraFiles := parseExtraFiles(flagExtraFiles)
releaserPleaser := rp.New(
f,
logger,
flagBranch,
conventionalcommits.NewParser(),
versioning.SemVerNextVersion,
extraFiles,
[]updater.NewUpdater{updater.Generic},
)
2024-07-12 14:51:24 +02:00
return releaserPleaser.Run(ctx)
2024-07-12 14:51:24 +02:00
}
func parseExtraFiles(input string) []string {
lines := strings.Split(input, "\n")
extraFiles := make([]string, 0, len(lines))
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) > 0 {
extraFiles = append(extraFiles, line)
}
}
return extraFiles
}