diff --git a/cmd/open.go b/cmd/open.go index 01a4458..c7fbad5 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -3,10 +3,11 @@ package cmd import ( "errors" "fmt" - "github.com/skratchdot/open-golang/open" - "github.com/urfave/cli/v2" "strconv" "strings" + + "github.com/skratchdot/open-golang/open" + "github.com/urfave/cli/v2" ) var Open = cli.Command{ diff --git a/cmd/release.go b/cmd/release.go index 8758de7..564a7cc 100644 --- a/cmd/release.go +++ b/cmd/release.go @@ -21,6 +21,7 @@ var Release = cli.Command{ Action: doRelease, Subcommands: []*cli.Command{ &ReleaseCreate, + &ReleaseAttach, }, Flags: []cli.Flag{ &cli.StringFlag{ diff --git a/cmd/release_attach.go b/cmd/release_attach.go new file mode 100644 index 0000000..0f31205 --- /dev/null +++ b/cmd/release_attach.go @@ -0,0 +1,117 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "gitea.com/jolheiser/sip/modules/sdk" + + "code.gitea.io/sdk/gitea" + "github.com/AlecAivazis/survey/v2" + "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" + "go.jolheiser.com/beaver/color" +) + +var ReleaseAttach = cli.Command{ + Name: "attach", + Usage: "Attach files to a release", + Action: doReleaseAttach, +} + +func doReleaseAttach(ctx *cli.Context) error { + client, err := getClient(ctx, true) + if err != nil { + return err + } + + releases, err := sdk.GetReleases(client, ctx.String("owner"), ctx.String("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 + } + + questions := []*survey.Question{ + { + Name: "release", + Prompt: &survey.Select{Message: "Release", Options: releaseNames}, + Validate: survey.Required, + }, + { + Name: "attachments", + Prompt: &survey.Multiline{Message: "Attachments", Help: "Enter file globs, each new line being a separate glob"}, + }, + } + answers := struct { + Release string + Attachments string + }{} + + if err := survey.Ask(questions, &answers); err != nil { + return err + } + + release := releaseMap[answers.Release] + files, err := fileGlobs(answers.Attachments) + if err != nil { + return err + } + if err := attachFiles(ctx, client, release.ID, files); err != nil { + return err + } + + info := color.Info + cyan := color.New(color.FgCyan) + fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("updated!")) + fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/tag/%s", + ctx.String("url"), ctx.String("owner"), ctx.String("repo"), release.TagName))) + return nil +} + +func fileGlobs(globList string) ([]string, error) { + files := make([]string, 0) + for _, glob := range strings.Split(globList, "\n") { + glob = strings.TrimSpace(glob) + matches, err := filepath.Glob(glob) + if err != nil { + return nil, err + } + files = append(files, matches...) + } + return files, nil +} + +func attachFiles(ctx *cli.Context, 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 + } + + if confirm { + for _, file := range files { + fi, err := os.Open(file) + if err != nil { + return err + } + + if _, _, err := client.CreateReleaseAttachment(ctx.String("owner"), ctx.String("repo"), releaseID, fi, fi.Name()); err != nil { + return err + } + + if err := fi.Close(); err != nil { + return err + } + } + } + return nil +} diff --git a/cmd/release_create.go b/cmd/release_create.go index 35727f3..dd61c87 100644 --- a/cmd/release_create.go +++ b/cmd/release_create.go @@ -54,14 +54,19 @@ func doReleaseCreate(ctx *cli.Context) error { Prompt: &survey.Confirm{Message: "Pre-Release", Default: false}, Validate: survey.Required, }, + { + Name: "attachments", + Prompt: &survey.Multiline{Message: "Attachments", Help: "Enter file globs, each new line being a separate glob"}, + }, } answers := struct { - Tag string - Target string - Title string - Note string - Draft bool - Pre bool + Tag string + Target string + Title string + Note string + Draft bool + Pre bool + Attachments string }{} if err := survey.Ask(questions, &answers); err != nil { @@ -80,10 +85,20 @@ func doReleaseCreate(ctx *cli.Context) error { return err } + if answers.Attachments != "" { + files, err := fileGlobs(answers.Attachments) + if err != nil { + return err + } + if err := attachFiles(ctx, client, release.ID, files); err != nil { + return err + } + } + info := color.Info cyan := color.New(color.FgCyan) fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("created!")) - // TODO Change to specific release page once supported - fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/", ctx.String("url"), ctx.String("owner"), ctx.String("repo")))) + fmt.Println(cyan.Format(fmt.Sprintf("%s/%s/%s/releases/tag/%s", + ctx.String("url"), ctx.String("owner"), ctx.String("repo"), release.TagName))) return nil }