mirror of
https://github.com/jimeh/build-emacs-for-macos.git
synced 2026-02-19 06:06:40 +00:00
feat(cli): add basis for new "emacs-builder" CLI tool written in Go
This will eventually replace the build-emacs-for-macos script that's written in Ruby. The migration of functionality will happen bit by bit over time.
This commit is contained in:
89
pkg/cli/cli.go
Normal file
89
pkg/cli/cli.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
cli2 "github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type CLI struct {
|
||||
App *cli2.App
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
}
|
||||
|
||||
func New(version, commit, date string) *CLI {
|
||||
if version == "" {
|
||||
version = "0.0.0-dev"
|
||||
}
|
||||
|
||||
c := &CLI{
|
||||
Version: version,
|
||||
Commit: commit,
|
||||
Date: date,
|
||||
App: &cli2.App{
|
||||
Name: "emacs-builder",
|
||||
Usage: "Tool to build emacs",
|
||||
Version: version,
|
||||
EnableBashCompletion: true,
|
||||
Flags: []cli2.Flag{
|
||||
&cli2.StringFlag{
|
||||
Name: "log-level",
|
||||
Usage: "set log level",
|
||||
Aliases: []string{"l"},
|
||||
Value: "info",
|
||||
},
|
||||
&cli2.BoolFlag{
|
||||
Name: "quiet",
|
||||
Usage: "silence noisy output",
|
||||
Aliases: []string{"q"},
|
||||
Value: false,
|
||||
},
|
||||
cli2.VersionFlag,
|
||||
},
|
||||
Commands: []*cli2.Command{
|
||||
{
|
||||
Name: "version",
|
||||
Usage: "print the version",
|
||||
Aliases: []string{"v"},
|
||||
Action: func(c *cli2.Context) error {
|
||||
cli2.VersionPrinter(c)
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cli2.VersionPrinter = c.VersionPrinter
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (s *CLI) VersionPrinter(c *cli2.Context) {
|
||||
version := c.App.Version
|
||||
if version == "" {
|
||||
version = "0.0.0-dev"
|
||||
}
|
||||
|
||||
extra := []string{}
|
||||
if len(s.Commit) >= 7 {
|
||||
extra = append(extra, s.Commit[0:7])
|
||||
}
|
||||
if s.Date != "" {
|
||||
extra = append(extra, s.Date)
|
||||
}
|
||||
var extraOut string
|
||||
if len(extra) > 0 {
|
||||
extraOut += " (" + strings.Join(extra, ", ") + ")"
|
||||
}
|
||||
|
||||
fmt.Printf("%s%s\n", version, extraOut)
|
||||
}
|
||||
|
||||
func (s *CLI) Run(args []string) error {
|
||||
return s.App.Run(args)
|
||||
}
|
||||
49
pkg/cli/options.go
Normal file
49
pkg/cli/options.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/go-hclog"
|
||||
cli2 "github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
quiet bool
|
||||
}
|
||||
|
||||
func actionWrapper(
|
||||
f func(*cli2.Context, *Options) error,
|
||||
) func(*cli2.Context) error {
|
||||
return func(c *cli2.Context) error {
|
||||
opts := &Options{
|
||||
quiet: c.Bool("quiet"),
|
||||
}
|
||||
|
||||
levelStr := c.String("log-level")
|
||||
level := hclog.LevelFromString(levelStr)
|
||||
if level == hclog.NoLevel {
|
||||
return fmt.Errorf("invalid log level \"%s\"", levelStr)
|
||||
}
|
||||
|
||||
// Prevent things from logging if they weren't explicitly given a
|
||||
// logger.
|
||||
hclog.SetDefault(hclog.NewNullLogger())
|
||||
|
||||
// Create custom logger.
|
||||
logr := hclog.New(&hclog.LoggerOptions{
|
||||
Level: level,
|
||||
Output: os.Stderr,
|
||||
Mutex: &sync.Mutex{},
|
||||
TimeFormat: time.RFC3339,
|
||||
Color: hclog.ColorOff,
|
||||
})
|
||||
|
||||
ctx := hclog.WithContext(c.Context, logr)
|
||||
c.Context = ctx
|
||||
|
||||
return f(c, opts)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user