code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/verify/reporter.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package verify
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/fatih/color"
    22  )
    23  
    24  var (
    25  	purple = color.New(color.FgMagenta).SprintFunc()
    26  	red    = color.New(color.FgRed).SprintFunc()
    27  	green  = color.New(color.FgGreen).SprintFunc()
    28  )
    29  
    30  type reporter struct {
    31  	file         string
    32  	hasError     bool
    33  	hasCurrError bool
    34  	reports      []string
    35  }
    36  
    37  func (r *reporter) HasError() bool {
    38  	return r.hasError
    39  }
    40  
    41  func (r *reporter) HasCurrError() bool {
    42  	return r.hasCurrError
    43  }
    44  
    45  func (r *reporter) Start(f string) {
    46  	r.file = f
    47  }
    48  
    49  func (r *reporter) Dump(result string) {
    50  	ok := green("OK")
    51  	if r.hasCurrError {
    52  		r.hasError = true
    53  		ok = red("NOT OK")
    54  	}
    55  	fmt.Printf("%v: %v\n", r.file, ok)
    56  	if len(result) > 0 {
    57  		fmt.Printf("%v\n", result)
    58  	}
    59  	for _, v := range r.reports {
    60  		fmt.Print(v)
    61  	}
    62  
    63  	r.reports = []string{}
    64  	r.hasCurrError = false
    65  	r.file = ""
    66  }
    67  
    68  func (r *reporter) Warn(s string, args ...interface{}) {
    69  	r.reports = append(r.reports, fmt.Sprintf(fmt.Sprintf("%v%v\n", fmt.Sprintf("%v: ", purple("warn")), s), args...))
    70  }
    71  
    72  func (r *reporter) Err(s string, args ...interface{}) {
    73  	r.hasCurrError = true
    74  	r.reports = append(r.reports, fmt.Sprintf(fmt.Sprintf("%v%v\n", fmt.Sprintf("%v: ", red("error")), s), args...))
    75  }