kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/reporter/reporter.go (about)

     1  // Copyright 2022 The KCL Authors. All rights reserved.
     2  
     3  package reporter
     4  
     5  import (
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  // Init the log.
    16  func InitReporter() {
    17  	log.SetFlags(0)
    18  	logrus.SetLevel(logrus.ErrorLevel)
    19  }
    20  
    21  // Report prints to the logger.
    22  // Arguments are handled in the manner of fmt.Println.
    23  func Report(v ...any) {
    24  	log.Println(v...)
    25  }
    26  
    27  // ExitWithReport prints to the logger and exit with 0.
    28  // Arguments are handled in the manner of fmt.Println.
    29  func ExitWithReport(v ...any) {
    30  	log.Println(v...)
    31  	os.Exit(0)
    32  }
    33  
    34  // Fatal prints to the logger and exit with 1.
    35  // Arguments are handled in the manner of fmt.Println.
    36  func Fatal(v ...any) {
    37  	log.Fatal(v...)
    38  }
    39  
    40  // Event is the interface that specifies the event used to show logs to users.
    41  type Event interface {
    42  	Event() string
    43  }
    44  
    45  type EventType int
    46  
    47  const (
    48  	Default EventType = iota
    49  
    50  	// errors event type means the event is an error.
    51  	InvalidRepo
    52  	FailedNewOciClient
    53  	RepoNotFound
    54  	FailedLoadSettings
    55  	FailedLoadCredential
    56  	FailedCreateOciClient
    57  	FailedSelectLatestVersion
    58  	FailedSelectLatestCompatibleVersion
    59  	FailedGetReleases
    60  	FailedTopologicalSort
    61  	FailedGetVertexProperties
    62  	FailedGenerateSource
    63  	FailedGetPackageVersions
    64  	FailedCreateStorePath
    65  	FailedPush
    66  	FailedGetPkg
    67  	FailedVendor
    68  	FailedAccessPkgPath
    69  	UnKnownPullWhat
    70  	UnknownEnv
    71  	InvalidKclPkg
    72  	FailedUntarKclPkg
    73  	FailedLoadKclMod
    74  	FailedLoadKclModLock
    75  	FailedCreateFile
    76  	FailedPackage
    77  	FailedLogin
    78  	FailedLogout
    79  	FileExists
    80  	CheckSumMismatch
    81  	CalSumFailed
    82  	InvalidKpmHomeInCurrentPkg
    83  	InvalidCmd
    84  	InvalidPkgRef
    85  	InvalidGitUrl
    86  	WithoutGitTag
    87  	FailedCloneFromGit
    88  	FailedHashPkg
    89  	FailedUpdatingBuildList
    90  	Bug
    91  
    92  	// normal event type means the event is a normal event.
    93  	PullingStarted
    94  	PullingFinished
    95  	Pulling
    96  	InvalidFlag
    97  	Adding
    98  	WaitingLock
    99  	IsNotUrl
   100  	IsNotRef
   101  	UrlSchemeNotOci
   102  	UnsupportOciUrlScheme
   103  	SelectLatestVersion
   104  	DownloadingFromOCI
   105  	DownloadingFromGit
   106  	LocalPathNotExist
   107  	PathIsEmpty
   108  	ConflictPkgName
   109  	AddItselfAsDep
   110  	PkgTagExists
   111  	DependencyNotFound
   112  	CircularDependencyExist
   113  	RemoveDep
   114  	AddDep
   115  	KclModNotFound
   116  	CompileFailed
   117  	FailedParseVersion
   118  )
   119  
   120  // KpmEvent is the event used to show kpm logs to users.
   121  type KpmEvent struct {
   122  	errType EventType
   123  	msg     string
   124  	err     error
   125  }
   126  
   127  // Type returns the event type.
   128  func (e *KpmEvent) Type() EventType {
   129  	return e.errType
   130  }
   131  
   132  // Error makes KpmEvent can be used as an error.
   133  func (e *KpmEvent) Error() string {
   134  	result := ""
   135  	if e.msg != "" {
   136  		// append msg
   137  		result = fmt.Sprintf("%s\n", e.msg)
   138  	}
   139  	if e.err != nil {
   140  		result = fmt.Sprintf("%s%s\n", result, e.err.Error())
   141  	}
   142  	return result
   143  }
   144  
   145  // Event returns the msg of the event without error message.
   146  func (e *KpmEvent) Event() string {
   147  	if e.msg != "" {
   148  		return fmt.Sprintf("%s\n", e.msg)
   149  	}
   150  	return ""
   151  }
   152  
   153  // NewErrorEvent returns a new KpmEvent with error.
   154  func NewErrorEvent(errType EventType, err error, args ...string) *KpmEvent {
   155  	return &KpmEvent{
   156  		errType: errType,
   157  		msg:     strings.Join(args, ""),
   158  		err:     err,
   159  	}
   160  }
   161  
   162  // NewEvent returns a new KpmEvent without error.
   163  func NewEvent(errType EventType, args ...string) *KpmEvent {
   164  	return &KpmEvent{
   165  		errType: errType,
   166  		msg:     strings.Join(args, ""),
   167  		err:     nil,
   168  	}
   169  }
   170  
   171  // ReportEventToStdout reports the event to users to stdout.
   172  func ReportEventToStdout(event *KpmEvent) {
   173  	fmt.Fprintf(os.Stdout, "%v", event.Event())
   174  }
   175  
   176  // ReportEventToStderr reports the event to users to stderr.
   177  func ReportEventToStderr(event *KpmEvent) {
   178  	fmt.Fprintf(os.Stderr, "%v", event.Event())
   179  }
   180  
   181  // ReportEvent reports the event to users to stdout.
   182  func ReportEventTo(event *KpmEvent, w io.Writer) {
   183  	if w != nil {
   184  		fmt.Fprintf(w, "%v", event.Event())
   185  	}
   186  }
   187  
   188  func ReportMsgTo(msg string, w io.Writer) {
   189  	if w != nil {
   190  		fmt.Fprintf(w, "%s\n", msg)
   191  	}
   192  }