Log Refactor Phase 2
This commit is contained in:
+45
-2
@@ -31,6 +31,7 @@ type context struct {
|
|||||||
stderr io.Writer
|
stderr io.Writer
|
||||||
cwd string
|
cwd string
|
||||||
args []string
|
args []string
|
||||||
|
logLevel logLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
type spinner struct {
|
type spinner struct {
|
||||||
@@ -230,9 +231,12 @@ func Run(args []string) (int, error) {
|
|||||||
printUsage(ctx.stdout)
|
printUsage(ctx.stdout)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
default:
|
default:
|
||||||
|
cmdArgs, level := parseGlobalFlags(args[1:])
|
||||||
|
ctx.logLevel = level
|
||||||
|
ctx.args = append([]string{args[0]}, cmdArgs...)
|
||||||
for _, cmd := range commands {
|
for _, cmd := range commands {
|
||||||
if cmd.name == args[0] {
|
if cmd.name == args[0] {
|
||||||
spin.configure(ctx.stderr, isInteractiveTTY(ctx.stderr) && strings.TrimSpace(os.Getenv("CI")) == "")
|
spin.configure(ctx.stderr, isInteractiveTTY(ctx.stderr) && strings.TrimSpace(os.Getenv("CI")) == "" && ctx.logLevel == logLevelNormal)
|
||||||
spin.start("running " + cmd.name)
|
spin.start("running " + cmd.name)
|
||||||
if err := cmd.run(ctx); err != nil {
|
if err := cmd.run(ctx); err != nil {
|
||||||
spin.stop()
|
spin.stop()
|
||||||
@@ -247,6 +251,24 @@ func Run(args []string) (int, error) {
|
|||||||
return 1, fmt.Errorf("unknown command %q", args[0])
|
return 1, fmt.Errorf("unknown command %q", args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseGlobalFlags(args []string) ([]string, logLevel) {
|
||||||
|
level := logLevelNormal
|
||||||
|
var filtered []string
|
||||||
|
for _, arg := range args {
|
||||||
|
switch arg {
|
||||||
|
case "--quiet":
|
||||||
|
level = logLevelQuiet
|
||||||
|
case "--verbose":
|
||||||
|
level = logLevelVerbose
|
||||||
|
case "--debug":
|
||||||
|
level = logLevelDebug
|
||||||
|
default:
|
||||||
|
filtered = append(filtered, arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered, level
|
||||||
|
}
|
||||||
|
|
||||||
func newContext() (context, error) {
|
func newContext() (context, error) {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -294,6 +316,9 @@ func runBuildModule(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := pipeline.BuildModuleWithProgress(p, func(message string) {
|
result, err := pipeline.BuildModuleWithProgress(p, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
spin.linebreak()
|
spin.linebreak()
|
||||||
fmt.Fprintf(ctx.stdout, "[build-module] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[build-module] %s\n", message)
|
||||||
})
|
})
|
||||||
@@ -530,6 +555,9 @@ func runBuildHAKs(ctx context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if ctx.logLevel > opts.logLevel {
|
||||||
|
opts.logLevel = ctx.logLevel
|
||||||
|
}
|
||||||
p, err = p.CloneWithHAKNames(opts.filteredHAKs)
|
p, err = p.CloneWithHAKNames(opts.filteredHAKs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -953,6 +981,9 @@ func runBuildTopData(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := topdata.BuildAndPackageWithOptions(p, opts, func(message string) {
|
result, err := topdata.BuildAndPackageWithOptions(p, opts, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Fprintf(ctx.stdout, "[build-topdata] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[build-topdata] %s\n", message)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -993,6 +1024,9 @@ func runBuildTopPackage(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := topdata.BuildPackage(p, func(message string) {
|
result, err := topdata.BuildPackage(p, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Fprintf(ctx.stdout, "[build-top-package] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[build-top-package] %s\n", message)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1041,6 +1075,9 @@ func runCompareTopData(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := topdata.Compare(p, func(message string) {
|
result, err := topdata.Compare(p, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Fprintf(ctx.stdout, "[compare-topdata] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[compare-topdata] %s\n", message)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1077,6 +1114,9 @@ func runBuildWiki(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := topdata.BuildWikiWithOptions(p, opts, func(message string) {
|
result, err := topdata.BuildWikiWithOptions(p, opts, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Fprintf(ctx.stdout, "[build-wiki] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[build-wiki] %s\n", message)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1117,6 +1157,9 @@ func runDeployWiki(ctx context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := topdata.DeployWikiWithOptions(p, opts, func(message string) {
|
result, err := topdata.DeployWikiWithOptions(p, opts, func(message string) {
|
||||||
|
if ctx.logLevel == logLevelQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Fprintf(ctx.stdout, "[deploy-wiki] %s\n", message)
|
fmt.Fprintf(ctx.stdout, "[deploy-wiki] %s\n", message)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1384,7 +1427,7 @@ func loadProject(ctx context) (*project.Project, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if ctx.stderr != nil {
|
if ctx.stderr != nil && ctx.logLevel != logLevelQuiet {
|
||||||
fmt.Fprintf(ctx.stderr, "Loaded config: %s\n", p.ConfigSource.Name)
|
fmt.Fprintf(ctx.stderr, "Loaded config: %s\n", p.ConfigSource.Name)
|
||||||
if p.ConfigSource.Legacy {
|
if p.ConfigSource.Legacy {
|
||||||
fmt.Fprintf(ctx.stderr, "Warning: %s is legacy repository configuration; migrate to %s.\n", project.LegacyConfigFile, project.ConfigFile)
|
fmt.Fprintf(ctx.stderr, "Warning: %s is legacy repository configuration; migrate to %s.\n", project.LegacyConfigFile, project.ConfigFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user