github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/warning.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  type Warning interface {
    11  	Warning() string
    12  	Warn(g *GlobalContext)
    13  }
    14  
    15  type StringWarning string
    16  
    17  func (s StringWarning) Warning() string {
    18  	return string(s)
    19  }
    20  
    21  func Warningf(format string, a ...interface{}) Warning {
    22  	return StringWarning(fmt.Sprintf(format, a...))
    23  }
    24  
    25  func (s StringWarning) Warn(g *GlobalContext) {
    26  	g.Log.Warning(string(s))
    27  }
    28  
    29  func ErrorToWarning(e error) Warning {
    30  	if e == nil {
    31  		return nil
    32  	}
    33  	return StringWarning(e.Error())
    34  }
    35  
    36  type Warnings struct {
    37  	w []Warning
    38  }
    39  
    40  func (w Warnings) Warnings() []Warning {
    41  	return w.w
    42  }
    43  
    44  func (w Warnings) IsEmpty() bool {
    45  	return w.w == nil || len(w.w) == 0
    46  }
    47  
    48  func (w *Warnings) Push(e Warning) {
    49  	w.w = append(w.w, e)
    50  }
    51  
    52  func (w Warnings) Warn(g *GlobalContext) {
    53  	for _, e := range w.w {
    54  		e.Warn(g)
    55  	}
    56  }