use a red colored stderr writer for the cobra cmd errors
This commit is contained in:
		
							parent
							
								
									070a1cd6d9
								
							
						
					
					
						commit
						5b94aced3a
					
				| 
						 | 
					@ -107,9 +107,6 @@ func (p *plugin) updateCmd() *cobra.Command {
 | 
				
			||||||
	command := &cobra.Command{
 | 
						command := &cobra.Command{
 | 
				
			||||||
		Use:          "update",
 | 
							Use:          "update",
 | 
				
			||||||
		Short:        "Automatically updates the current PocketBase executable with the latest available version",
 | 
							Short:        "Automatically updates the current PocketBase executable with the latest available version",
 | 
				
			||||||
		// @todo remove after logs generalization
 | 
					 | 
				
			||||||
		// prevents printing the error log twice
 | 
					 | 
				
			||||||
		SilenceErrors: true,
 | 
					 | 
				
			||||||
		SilenceUsage: true,
 | 
							SilenceUsage: true,
 | 
				
			||||||
		RunE: func(command *cobra.Command, args []string) error {
 | 
							RunE: func(command *cobra.Command, args []string) error {
 | 
				
			||||||
			var needConfirm bool
 | 
								var needConfirm bool
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -122,8 +122,6 @@ func (p *plugin) createCommand() *cobra.Command {
 | 
				
			||||||
		Short:        "Executes app DB migration scripts",
 | 
							Short:        "Executes app DB migration scripts",
 | 
				
			||||||
		Long:         cmdDesc,
 | 
							Long:         cmdDesc,
 | 
				
			||||||
		ValidArgs:    []string{"up", "down", "create", "collections"},
 | 
							ValidArgs:    []string{"up", "down", "create", "collections"},
 | 
				
			||||||
		// prevents printing the error log twice
 | 
					 | 
				
			||||||
		SilenceErrors: true,
 | 
					 | 
				
			||||||
		SilenceUsage: true,
 | 
							SilenceUsage: true,
 | 
				
			||||||
		RunE: func(command *cobra.Command, args []string) error {
 | 
							RunE: func(command *cobra.Command, args []string) error {
 | 
				
			||||||
			cmd := ""
 | 
								cmd := ""
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,14 @@
 | 
				
			||||||
package pocketbase
 | 
					package pocketbase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/fatih/color"
 | 
				
			||||||
	"github.com/pocketbase/pocketbase/cmd"
 | 
						"github.com/pocketbase/pocketbase/cmd"
 | 
				
			||||||
	"github.com/pocketbase/pocketbase/core"
 | 
						"github.com/pocketbase/pocketbase/core"
 | 
				
			||||||
	"github.com/pocketbase/pocketbase/tools/list"
 | 
						"github.com/pocketbase/pocketbase/tools/list"
 | 
				
			||||||
| 
						 | 
					@ -98,6 +100,9 @@ func NewWithConfig(config Config) *PocketBase {
 | 
				
			||||||
		hideStartBanner:   config.HideStartBanner,
 | 
							hideStartBanner:   config.HideStartBanner,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// replace with a colored stderr writer
 | 
				
			||||||
 | 
						pb.RootCmd.SetErr(newErrWriter())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// parse base flags
 | 
						// parse base flags
 | 
				
			||||||
	// (errors are ignored, since the full flags parsing happens on Execute())
 | 
						// (errors are ignored, since the full flags parsing happens on Execute())
 | 
				
			||||||
	pb.eagerParseFlags(&config)
 | 
						pb.eagerParseFlags(&config)
 | 
				
			||||||
| 
						 | 
					@ -152,9 +157,8 @@ func (pb *PocketBase) Execute() error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// execute the root command
 | 
						// execute the root command
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		if err := pb.RootCmd.Execute(); err != nil {
 | 
							// leave to the commands to decide whether to print their error or not
 | 
				
			||||||
			pb.Logger().Error("rootCmd.Execute error", "error", err)
 | 
							pb.RootCmd.Execute()
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		done <- true
 | 
							done <- true
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
| 
						 | 
					@ -246,3 +250,25 @@ func inspectRuntime() (baseDir string, withGoRun bool) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// newErrWriter returns a red colored stderr writter.
 | 
				
			||||||
 | 
					func newErrWriter() *coloredWriter {
 | 
				
			||||||
 | 
						return &coloredWriter{
 | 
				
			||||||
 | 
							w: os.Stderr,
 | 
				
			||||||
 | 
							c: color.New(color.FgRed),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// coloredWriter is a small wrapper struct to construct a [color.Color] writter.
 | 
				
			||||||
 | 
					type coloredWriter struct {
 | 
				
			||||||
 | 
						w io.Writer
 | 
				
			||||||
 | 
						c *color.Color
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Write writes the p bytes using the colored writer.
 | 
				
			||||||
 | 
					func (colored *coloredWriter) Write(p []byte) (n int, err error) {
 | 
				
			||||||
 | 
						colored.c.SetWriter(colored.w)
 | 
				
			||||||
 | 
						defer colored.c.UnsetWriter(colored.w)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return colored.c.Print(string(p))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue