You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
code improvements
This commit is contained in:
@@ -5,13 +5,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/goreleaser/releaser/config"
|
"github.com/goreleaser/releaser/config"
|
||||||
|
"github.com/goreleaser/releaser/split"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const formulae = `class {{ .Name }} < Formula
|
const formulae = `class {{ .Name }} < Formula
|
||||||
@@ -45,38 +46,26 @@ func (Pipe) Work(config config.ProjectConfig) error {
|
|||||||
if config.Brew.Repo == "" {
|
if config.Brew.Repo == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Println("Updating brew formulae...")
|
|
||||||
ts := oauth2.StaticTokenSource(
|
ts := oauth2.StaticTokenSource(
|
||||||
&oauth2.Token{AccessToken: config.Token},
|
&oauth2.Token{AccessToken: config.Token},
|
||||||
)
|
)
|
||||||
tc := oauth2.NewClient(context.Background(), ts)
|
tc := oauth2.NewClient(context.Background(), ts)
|
||||||
client := github.NewClient(tc)
|
client := github.NewClient(tc)
|
||||||
parts := strings.Split(config.Brew.Repo, "/")
|
|
||||||
|
|
||||||
data, err := dataFor(config, client)
|
owner, repo := split.OnSlash(config.Brew.Repo)
|
||||||
|
name := config.BinaryName + ".rb"
|
||||||
|
|
||||||
|
log.Println("Updating", name, "on", config.Repo, "...")
|
||||||
|
out, err := buildFormulae(config, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
sha, err := sha(client, owner, repo, name, out)
|
||||||
out, err := buildFormulae(data)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var sha *string
|
|
||||||
file, _, _, err := client.Repositories.GetContents(
|
|
||||||
parts[0], parts[1], config.BinaryName+".rb", &github.RepositoryContentGetOptions{},
|
|
||||||
)
|
|
||||||
if err == nil {
|
|
||||||
sha = file.SHA
|
|
||||||
} else {
|
|
||||||
sha = github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes())))
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, err = client.Repositories.UpdateFile(
|
_, _, err = client.Repositories.UpdateFile(
|
||||||
parts[0],
|
owner, repo, name, &github.RepositoryContentFileOptions{
|
||||||
parts[1],
|
|
||||||
config.BinaryName+".rb",
|
|
||||||
&github.RepositoryContentFileOptions{
|
|
||||||
Committer: &github.CommitAuthor{
|
Committer: &github.CommitAuthor{
|
||||||
Name: github.String("goreleaserbot"),
|
Name: github.String("goreleaserbot"),
|
||||||
Email: github.String("bot@goreleaser"),
|
Email: github.String("bot@goreleaser"),
|
||||||
@@ -88,9 +77,25 @@ func (Pipe) Work(config config.ProjectConfig) error {
|
|||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
func sha(client *github.Client, owner, repo, name string, out bytes.Buffer) (*string, error) {
|
||||||
|
var sha *string
|
||||||
|
file, _, _, err := client.Repositories.GetContents(
|
||||||
|
owner, repo, name, &github.RepositoryContentGetOptions{},
|
||||||
|
)
|
||||||
|
if err == nil {
|
||||||
|
sha = file.SHA
|
||||||
|
} else {
|
||||||
|
sha = github.String(fmt.Sprintf("%s", sha256.Sum256(out.Bytes())))
|
||||||
|
}
|
||||||
|
return sha, err
|
||||||
|
}
|
||||||
|
|
||||||
func buildFormulae(data templateData) (bytes.Buffer, error) {
|
func buildFormulae(config config.ProjectConfig, client *github.Client) (bytes.Buffer, error) {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
data, err := dataFor(config, client)
|
||||||
|
if err != nil {
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
tmpl, err := template.New(data.BinaryName).Parse(formulae)
|
tmpl, err := template.New(data.BinaryName).Parse(formulae)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return out, err
|
return out, err
|
||||||
@@ -102,8 +107,8 @@ func buildFormulae(data templateData) (bytes.Buffer, error) {
|
|||||||
func dataFor(config config.ProjectConfig, client *github.Client) (result templateData, err error) {
|
func dataFor(config config.ProjectConfig, client *github.Client) (result templateData, err error) {
|
||||||
var homepage string
|
var homepage string
|
||||||
var description string
|
var description string
|
||||||
parts := strings.Split(config.Repo, "/")
|
owner, repo := split.OnSlash(config.Repo)
|
||||||
rep, _, err := client.Repositories.Get(parts[0], parts[1])
|
rep, _, err := client.Repositories.Get(owner, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
@@ -5,10 +5,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"github.com/goreleaser/releaser/config"
|
"github.com/goreleaser/releaser/config"
|
||||||
|
"github.com/goreleaser/releaser/split"
|
||||||
"github.com/goreleaser/releaser/uname"
|
"github.com/goreleaser/releaser/uname"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
@@ -28,8 +28,7 @@ func (Pipe) Work(config config.ProjectConfig) error {
|
|||||||
tc := oauth2.NewClient(context.Background(), ts)
|
tc := oauth2.NewClient(context.Background(), ts)
|
||||||
client := github.NewClient(tc)
|
client := github.NewClient(tc)
|
||||||
|
|
||||||
owner := strings.Split(config.Repo, "/")[0]
|
owner, repo := split.OnSlash(config.Repo)
|
||||||
repo := strings.Split(config.Repo, "/")[1]
|
|
||||||
r, _, err := client.Repositories.CreateRelease(owner, repo, &github.RepositoryRelease{
|
r, _, err := client.Repositories.CreateRelease(owner, repo, &github.RepositoryRelease{
|
||||||
Name: github.String(config.Git.CurrentTag),
|
Name: github.String(config.Git.CurrentTag),
|
||||||
TagName: github.String(config.Git.CurrentTag),
|
TagName: github.String(config.Git.CurrentTag),
|
||||||
|
8
split/split.go
Normal file
8
split/split.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package split
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func OnSlash(pair string) (string, string) {
|
||||||
|
parts := strings.Split(pair, "/")
|
||||||
|
return parts[0], parts[1]
|
||||||
|
}
|
Reference in New Issue
Block a user