mirror of
https://github.com/jimeh/build-emacs-for-macos.git
synced 2026-02-19 13:06:38 +00:00
feat(cask): add cask update command to manage cask formula
This will be used by the jimeh/homebrew-emacs-builds brew tap repository in combination with brew livecheck to automatically update cask formulas to the latest nightly builds from the jimeh/emacs-builds repository.
This commit is contained in:
149
pkg/cli/cask.go
Normal file
149
pkg/cli/cask.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/jimeh/build-emacs-for-macos/pkg/cask"
|
||||
"github.com/jimeh/build-emacs-for-macos/pkg/repository"
|
||||
cli2 "github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type caskOptions struct {
|
||||
BuildsRepo *repository.Repository
|
||||
GithubToken string
|
||||
}
|
||||
|
||||
func caskCmd() *cli2.Command {
|
||||
tokenDefaultText := ""
|
||||
if len(os.Getenv("GITHUB_TOKEN")) > 0 {
|
||||
tokenDefaultText = "***"
|
||||
}
|
||||
|
||||
return &cli2.Command{
|
||||
Name: "cask",
|
||||
Usage: "manage Homebrew Cask formula",
|
||||
Flags: []cli2.Flag{
|
||||
&cli2.StringFlag{
|
||||
Name: "builds-repository",
|
||||
Aliases: []string{"builds-repo", "b"},
|
||||
Usage: "owner/name of GitHub repo for containing builds",
|
||||
EnvVars: []string{"EMACS_BUILDS_REPOSITORY"},
|
||||
Value: "jimeh/emacs-builds",
|
||||
},
|
||||
&cli2.StringFlag{
|
||||
Name: "github-token",
|
||||
Usage: "GitHub API Token",
|
||||
EnvVars: []string{"GITHUB_TOKEN"},
|
||||
DefaultText: tokenDefaultText,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Subcommands: []*cli2.Command{
|
||||
caskUpdateCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func caskActionWrapper(
|
||||
f func(*cli2.Context, *Options, *caskOptions) error,
|
||||
) func(*cli2.Context) error {
|
||||
return actionWrapper(func(c *cli2.Context, opts *Options) error {
|
||||
rOpts := &caskOptions{
|
||||
GithubToken: c.String("github-token"),
|
||||
}
|
||||
|
||||
if r := c.String("builds-repository"); r != "" {
|
||||
var err error
|
||||
rOpts.BuildsRepo, err = repository.NewGitHub(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return f(c, opts, rOpts)
|
||||
})
|
||||
}
|
||||
|
||||
func caskUpdateCmd() *cli2.Command {
|
||||
return &cli2.Command{
|
||||
Name: "update",
|
||||
Usage: "update casks based on brew livecheck result in JSON format",
|
||||
ArgsUsage: "<livecheck.json>",
|
||||
Flags: []cli2.Flag{
|
||||
&cli2.StringFlag{
|
||||
Name: "ref",
|
||||
Usage: "git ref to create/update casks on top of in the " +
|
||||
"tap repository",
|
||||
EnvVars: []string{"GITHUB_REF"},
|
||||
},
|
||||
&cli2.StringFlag{
|
||||
Name: "output",
|
||||
Aliases: []string{"o"},
|
||||
Usage: "directory to write cask files to",
|
||||
},
|
||||
&cli2.StringFlag{
|
||||
Name: "tap-repository",
|
||||
Aliases: []string{"tap"},
|
||||
Usage: "owner/name of GitHub repo for Homebrew Tap to " +
|
||||
"commit changes to if --output is not set",
|
||||
EnvVars: []string{"GITHUB_REPOSITORY"},
|
||||
},
|
||||
&cli2.StringFlag{
|
||||
Name: "templates-dir",
|
||||
Aliases: []string{"t"},
|
||||
Usage: "path to directory of cask templates",
|
||||
EnvVars: []string{"CASK_TEMPLATE_DIR"},
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: caskActionWrapper(caskUpdateAction),
|
||||
}
|
||||
}
|
||||
|
||||
func caskUpdateAction(
|
||||
c *cli2.Context,
|
||||
opts *Options,
|
||||
cOpts *caskOptions,
|
||||
) error {
|
||||
updateOpts := &cask.UpdateOptions{
|
||||
BuildsRepo: cOpts.BuildsRepo,
|
||||
GithubToken: cOpts.GithubToken,
|
||||
Ref: c.String("ref"),
|
||||
OutputDir: c.String("output"),
|
||||
TemplatesDir: c.String("templates-dir"),
|
||||
}
|
||||
|
||||
if r := c.String("tap-repository"); r != "" {
|
||||
var err error
|
||||
updateOpts.TapRepo, err = repository.NewGitHub(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
arg := c.Args().First()
|
||||
if arg == "" {
|
||||
return errors.New("no livecheck argument given")
|
||||
}
|
||||
|
||||
if arg == "-" {
|
||||
err := json.NewDecoder(c.App.Reader).Decode(&updateOpts.LiveChecks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
f, err := os.Open(arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.NewDecoder(f).Decode(&updateOpts.LiveChecks)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return cask.Update(c.Context, updateOpts)
|
||||
}
|
||||
@@ -49,6 +49,7 @@ func New(version, commit, date string) *CLI {
|
||||
notarizeCmd(),
|
||||
packageCmd(),
|
||||
releaseCmd(),
|
||||
caskCmd(),
|
||||
{
|
||||
Name: "version",
|
||||
Usage: "print the version",
|
||||
|
||||
Reference in New Issue
Block a user