refactor(diary): share the console types through internal/consolekit so diary no longer links bonfire (IN-07, quick-261028-rqd)
This commit is contained in:
@@ -2,6 +2,8 @@ package bonfire
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.golem15.com/golem15/summercms/internal/consolekit"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -9,24 +11,16 @@ import (
|
||||
)
|
||||
|
||||
// maxVerbosity caps -v, -vv and -vvv (and repeated --verbose).
|
||||
const maxVerbosity = 3
|
||||
const maxVerbosity = consolekit.MaxVerbosity
|
||||
|
||||
// Globals are the global console options every command accepts: -v, -vv and
|
||||
// -vvv (--verbose), -q (--quiet), --ansi and --no-ansi, -n
|
||||
// (--no-interaction) and --env.
|
||||
type Globals struct {
|
||||
// Verbosity is 0 by default and 1 to 3 for -v, -vv and -vvv.
|
||||
Verbosity int
|
||||
// Quiet suppresses decoration and info lines; it implies NoInteraction.
|
||||
Quiet bool
|
||||
// Color is ColorAlways for --ansi, ColorNever for --no-ansi and
|
||||
// ColorAuto otherwise; the last of the two flags wins.
|
||||
Color ColorMode
|
||||
// NoInteraction makes prompts return their defaults without asking.
|
||||
NoInteraction bool
|
||||
// Env is the --env value: the environment the command runs under.
|
||||
Env string
|
||||
}
|
||||
// (--no-interaction) and --env. The fields are Verbosity (0, or 1 to 3 for
|
||||
// -v to -vvv), Quiet (implies NoInteraction), Color (ColorAlways for
|
||||
// --ansi, ColorNever for --no-ansi, ColorAuto otherwise; the last of the
|
||||
// two flags wins), NoInteraction and Env. Its Args method re-serialises
|
||||
// them as flags for forwarding to another program.
|
||||
type Globals = consolekit.Globals
|
||||
|
||||
// Global option names and shorthands, reserved on every command.
|
||||
const (
|
||||
@@ -76,7 +70,7 @@ func PeekGlobals(args []string) Globals {
|
||||
}
|
||||
switch {
|
||||
case a == "--":
|
||||
return g.normalize()
|
||||
return normalize(g)
|
||||
case a == "--"+flagVerbose:
|
||||
g.Verbosity++
|
||||
case a == "-q" || a == "--"+flagQuiet:
|
||||
@@ -93,10 +87,10 @@ func PeekGlobals(args []string) Globals {
|
||||
i++
|
||||
}
|
||||
case isShorthandCluster(a):
|
||||
g.peekShorthands(a[1:])
|
||||
peekShorthands(&g, a[1:])
|
||||
}
|
||||
}
|
||||
return g.normalize()
|
||||
return normalize(g)
|
||||
}
|
||||
|
||||
// peekCount applies a --verbose=N or -v=N value the way pflag's count flag
|
||||
@@ -138,7 +132,7 @@ func isShorthandCluster(a string) bool {
|
||||
// -q -n, -vvq is -v -v -q, -v=3 sets the count, -qn=false turns -n off). It
|
||||
// stops at the first letter that is not a global shorthand: that may be a
|
||||
// command flag whose value is the rest of the token.
|
||||
func (g *Globals) peekShorthands(cluster string) {
|
||||
func peekShorthands(g *Globals, cluster string) {
|
||||
for i := 0; i < len(cluster); i++ {
|
||||
r := cluster[i]
|
||||
if i+1 < len(cluster) && cluster[i+1] == '=' {
|
||||
@@ -167,38 +161,9 @@ func (g *Globals) peekShorthands(cluster string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (g Globals) normalize() Globals {
|
||||
g.Verbosity = min(max(g.Verbosity, 0), maxVerbosity)
|
||||
if g.Quiet {
|
||||
g.NoInteraction = true
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
// Args re-serialises the globals as command-line flags, for forwarding to
|
||||
// another program: -v, -vv or -vvv, -q, -n, --ansi or --no-ansi and
|
||||
// --env=<value>. The zero value gives no arguments.
|
||||
func (g Globals) Args() []string {
|
||||
var out []string
|
||||
if v := min(g.Verbosity, maxVerbosity); v > 0 {
|
||||
out = append(out, "-"+strings.Repeat("v", v))
|
||||
}
|
||||
if g.Quiet {
|
||||
out = append(out, "-q")
|
||||
}
|
||||
if g.NoInteraction && !g.Quiet {
|
||||
out = append(out, "-n")
|
||||
}
|
||||
switch g.Color {
|
||||
case ColorAlways:
|
||||
out = append(out, "--"+flagANSI)
|
||||
case ColorNever:
|
||||
out = append(out, "--"+flagNoANSI)
|
||||
}
|
||||
if g.Env != "" {
|
||||
out = append(out, "--"+flagEnv+"="+g.Env)
|
||||
}
|
||||
return out
|
||||
// normalize caps the verbosity and lets Quiet imply NoInteraction.
|
||||
func normalize(g Globals) Globals {
|
||||
return consolekit.Normalize(g)
|
||||
}
|
||||
|
||||
// globalFlags holds the parsed persistent flags of one root.
|
||||
@@ -250,13 +215,13 @@ func (gf *globalFlags) register(fs *pflag.FlagSet) {
|
||||
}
|
||||
|
||||
func (gf *globalFlags) globals() Globals {
|
||||
return Globals{
|
||||
return normalize(Globals{
|
||||
Verbosity: gf.verbosity,
|
||||
Quiet: gf.quiet,
|
||||
Color: gf.color,
|
||||
NoInteraction: gf.noInteraction,
|
||||
Env: gf.env,
|
||||
}.normalize()
|
||||
})
|
||||
}
|
||||
|
||||
// resetFlags puts every persistent flag of root back to its default, so a
|
||||
|
||||
Reference in New Issue
Block a user