diff --git a/cmd/release_attach.go b/cmd/release_attach.go index f5ce367..427efc0 100644 --- a/cmd/release_attach.go +++ b/cmd/release_attach.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" "gitea.com/jolheiser/sip/flag" @@ -20,6 +21,9 @@ var ReleaseAttach = cli.Command{ Name: "attach", Usage: "Attach files to a release", Action: doReleaseAttach, + Subcommands: []*cli.Command{ + &ReleaseAttachRemove, + }, } func doReleaseAttach(_ *cli.Context) error { @@ -71,7 +75,7 @@ func doReleaseAttach(_ *cli.Context) error { info := color.Info cyan := color.New(color.FgCyan) - fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("updated!")) + fmt.Println(info.Format("Added"), cyan.Format(strconv.Itoa(len(files))), info.Format("attachments.")) fmt.Println(cyan.Format(fmt.Sprintf("%s/releases/tag/%s", flag.FullURL(), release.TagName))) return nil } @@ -91,7 +95,6 @@ func fileGlobs(globList string) ([]string, error) { func attachFiles(client *gitea.Client, releaseID int64, files []string) error { beaver.Infof("Attachments:\n\t%s", strings.Join(files, "\n\t")) - var confirm bool if err := survey.AskOne(&survey.Confirm{Message: "The above files will be attached, is the list correct?"}, &confirm); err != nil { return err diff --git a/cmd/release_attach_remove.go b/cmd/release_attach_remove.go new file mode 100644 index 0000000..52645b6 --- /dev/null +++ b/cmd/release_attach_remove.go @@ -0,0 +1,79 @@ +package cmd + +import ( + "fmt" + "strconv" + + "gitea.com/jolheiser/sip/flag" + "gitea.com/jolheiser/sip/sdk" + + "code.gitea.io/sdk/gitea" + "github.com/AlecAivazis/survey/v2" + "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver/color" +) + +var ReleaseAttachRemove = cli.Command{ + Name: "remove", + Aliases: []string{"delete", "rm"}, + Usage: "Remove attachments from a release", + Action: doReleaseAttachRemove, +} + +func doReleaseAttachRemove(_ *cli.Context) error { + client, err := getClient(true) + if err != nil { + return err + } + + releases, err := sdk.GetReleases(client, flag.Owner, flag.Repo, gitea.ListReleasesOptions{}) + if err != nil { + return err + } + + releaseNames := make([]string, len(releases)) + releaseMap := make(map[string]*gitea.Release) + for idx, rel := range releases { + releaseNames[idx] = rel.TagName + releaseMap[rel.TagName] = rel + } + + releaseQ := &survey.Select{Message: "Release", Options: releaseNames} + var releaseA string + if err := survey.AskOne(releaseQ, &releaseA); err != nil { + return err + } + + release := releaseMap[releaseA] + attachments, err := sdk.GetReleaseAttachments(client, flag.Owner, flag.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{}) + if err != nil { + return err + } + + attachmentMap := make(map[string]*gitea.Attachment) + attachmentOptions := make([]string, len(attachments)) + for idx, attachment := range attachments { + display := fmt.Sprintf("%s (%s)", attachment.Name, attachment.UUID) + attachmentOptions[idx] = display + attachmentMap[display] = attachment + } + + attachmentQ := &survey.MultiSelect{Message: "Delete Attachments", Options: attachmentOptions} + var attachmentA []string + if err := survey.AskOne(attachmentQ, &attachmentA); err != nil { + return err + } + + for _, a := range attachmentA { + attachment := attachmentMap[a] + if _, err := client.DeleteReleaseAttachment(flag.Owner, flag.Repo, release.ID, attachment.ID); err != nil { + return err + } + } + + info := color.Info + cyan := color.New(color.FgCyan) + fmt.Println(info.Format("Removed"), cyan.Format(strconv.Itoa(len(attachmentA))), info.Format("attachments.")) + fmt.Println(cyan.Format(fmt.Sprintf("%s/releases/tag/%s", flag.FullURL(), release.TagName))) + return nil +} diff --git a/sdk/releases.go b/sdk/releases.go index bc44019..161674e 100644 --- a/sdk/releases.go +++ b/sdk/releases.go @@ -1,6 +1,8 @@ package sdk -import "code.gitea.io/sdk/gitea" +import ( + "code.gitea.io/sdk/gitea" +) // GetReleases returns all matching Releases from a Gitea instance func GetReleases(client *gitea.Client, owner, repo string, opts gitea.ListReleasesOptions) ([]*gitea.Release, error) { @@ -21,3 +23,24 @@ func GetReleases(client *gitea.Client, owner, repo string, opts gitea.ListReleas } return releases, nil } + +// GetReleaseAttachments returns all attachments from a release +func GetReleaseAttachments(client *gitea.Client, owner, repo string, releaseID int64, opts gitea.ListReleaseAttachmentsOptions) ([]*gitea.Attachment, error) { + attachments := make([]*gitea.Attachment, 0) + p := 1 + for { + opts.Page = p + list, _, err := client.ListReleaseAttachments(owner, repo, releaseID, opts) + if err != nil { + return attachments, err + } + p++ + attachments = append(attachments, list...) + + // FIXME Seems paging doesn't want to work with release attachments + if len(list) == 0 || len(list) < 50 { + break + } + } + return attachments, nil +}