feat: tag command and force cleanup
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>main v0.0.6
parent
b49dbe94b9
commit
fe0e2e5707
|
@ -11,6 +11,8 @@ import (
|
||||||
|
|
||||||
func (h *Handler) Cleanup() *ffcli.Command {
|
func (h *Handler) Cleanup() *ffcli.Command {
|
||||||
fs := flag.NewFlagSet("cleanup", flag.ContinueOnError)
|
fs := flag.NewFlagSet("cleanup", flag.ContinueOnError)
|
||||||
|
forceFlag := fs.Bool("force", false, "Force cleanup")
|
||||||
|
fs.BoolVar(forceFlag, "f", *forceFlag, "--force")
|
||||||
return &ffcli.Command{
|
return &ffcli.Command{
|
||||||
Name: "cleanup",
|
Name: "cleanup",
|
||||||
FlagSet: fs,
|
FlagSet: fs,
|
||||||
|
@ -23,7 +25,7 @@ func (h *Handler) Cleanup() *ffcli.Command {
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if err := removeWorktree(h, ctx, arg); err != nil {
|
if err := removeWorktree(h, ctx, arg, *forceFlag); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +50,7 @@ func (h *Handler) Cleanup() *ffcli.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rm := range remove {
|
for _, rm := range remove {
|
||||||
if err := removeWorktree(h, ctx, rm); err != nil {
|
if err := removeWorktree(h, ctx, rm, *forceFlag); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,8 +60,13 @@ func (h *Handler) Cleanup() *ffcli.Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeWorktree(h *Handler, ctx context.Context, name string) error {
|
func removeWorktree(h *Handler, ctx context.Context, name string, force bool) error {
|
||||||
if err := h.run(ctx, "git", "worktree", "remove", name); err != nil {
|
args := []string{"worktree", "remove"}
|
||||||
|
if force {
|
||||||
|
args = append(args, "--force")
|
||||||
|
}
|
||||||
|
args = append(args, name)
|
||||||
|
if err := h.run(ctx, "git", args...); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return h.run(ctx, "git", "branch", "-D", name)
|
return h.run(ctx, "git", "branch", "-D", name)
|
||||||
|
|
|
@ -53,6 +53,7 @@ func New() (*ffcli.Command, error) {
|
||||||
handler.Init(),
|
handler.Init(),
|
||||||
handler.Post(),
|
handler.Post(),
|
||||||
handler.PR(),
|
handler.PR(),
|
||||||
|
handler.Tag(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
c.Exec = func(_ context.Context, _ []string) error {
|
c.Exec = func(_ context.Context, _ []string) error {
|
||||||
|
|
|
@ -3,10 +3,11 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
|
||||||
"github.com/peterbourgon/ff/v3/ffcli"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
"github.com/peterbourgon/ff/v3/ffcli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) IDE() *ffcli.Command {
|
func (h *Handler) IDE() *ffcli.Command {
|
||||||
|
|
|
@ -0,0 +1,113 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/peterbourgon/ff/v3/ffcli"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
treeURL = "https://github.com/go-gitea/gitea/tree/%s"
|
||||||
|
versionRe = regexp.MustCompile(`v\d+\.\d+\.\d+`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) Tag() *ffcli.Command {
|
||||||
|
fs := flag.NewFlagSet("tag", flag.ContinueOnError)
|
||||||
|
return &ffcli.Command{
|
||||||
|
Name: "tag",
|
||||||
|
FlagSet: fs,
|
||||||
|
ShortUsage: "tag <branch>",
|
||||||
|
ShortHelp: "tag makes a signed tag for `branch`",
|
||||||
|
Exec: func(ctx context.Context, args []string) error {
|
||||||
|
if err := h.checkInit(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.fetch(ctx)
|
||||||
|
|
||||||
|
var branch string
|
||||||
|
if len(args) > 0 {
|
||||||
|
branch = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
if branch == "" {
|
||||||
|
var branches []string
|
||||||
|
remote, err := h.repo().Remote("upstream")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
refs, err := remote.List(&git.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, ref := range refs {
|
||||||
|
if ref.Name().IsBranch() {
|
||||||
|
branches = append(branches, strings.TrimPrefix(ref.Name().String(), "refs/heads/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(branches)
|
||||||
|
if err := survey.AskOne(&survey.Select{
|
||||||
|
Message: "Branch",
|
||||||
|
Options: branches,
|
||||||
|
}, &branch, survey.WithValidator(survey.Required)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(branch, "release") {
|
||||||
|
if !strings.HasPrefix(branch, "v") {
|
||||||
|
branch = fmt.Sprintf("v%s", branch)
|
||||||
|
}
|
||||||
|
branch = fmt.Sprintf("release/%s", branch)
|
||||||
|
}
|
||||||
|
|
||||||
|
var version string
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "Version",
|
||||||
|
}, &version, survey.WithValidator(func(ans any) error {
|
||||||
|
if !versionRe.MatchString(fmt.Sprint(ans)) {
|
||||||
|
return errors.New("version isn't valid v$maj.$min.$")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceName := strings.ReplaceAll(branch, "/", "-")
|
||||||
|
if err := h.Branch().ParseAndRun(ctx, []string{"--base", branch, workspaceName}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var changelog string
|
||||||
|
if err := survey.AskOne(&survey.Editor{
|
||||||
|
Message: "Changelog",
|
||||||
|
FileName: "*.md",
|
||||||
|
}, &changelog, survey.WithValidator(survey.Required)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := os.Create(filepath.Join(h.Config.WorkspaceBranch(workspaceName), "release.notes"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := fi.WriteString(changelog); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := fi.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
run(ctx, h.Config.WorkspaceBranch(workspaceName), "git", "tag", "-s", "-F", "release.notes", version)
|
||||||
|
|
||||||
|
fmt.Printf("cd %s && git push upstream %s \n", h.Config.WorkspaceBranch(workspaceName), version)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue