Skip to content

Commit

Permalink
Remove deprecated packages strings.Title and golang/proto
Browse files Browse the repository at this point in the history
  • Loading branch information
prnvkv committed Aug 11, 2023
1 parent 724040e commit edfde52
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 102 deletions.
40 changes: 22 additions & 18 deletions pkg/controller/eapps/eappslog.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//Package eapps provides primitives for searching and processing data
//in Log files of apps.
// Package eapps provides primitives for searching and processing data
// in Log files of apps.
package eapps

import (
Expand All @@ -15,13 +15,15 @@ import (
"github.com/lf-edge/eve/api/go/logs"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"google.golang.org/protobuf/encoding/protojson"
)

//LogCheckerMode is InfoExist, InfoNew and InfoAny
// LogCheckerMode is InfoExist, InfoNew and InfoAny
type LogCheckerMode int

//LogTail returns LogCheckerMode for process only defined count of last messages
// LogTail returns LogCheckerMode for process only defined count of last messages
func LogTail(count uint) LogCheckerMode {
return LogCheckerMode(count)
}
Expand All @@ -33,21 +35,22 @@ const (
LogAny LogCheckerMode = -1 // use both mechanisms
)

//ParseLogEntry unmarshal LogEntry
// ParseLogEntry unmarshal LogEntry
func ParseLogEntry(data []byte) (logEntry *logs.LogEntry, err error) {
var le logs.LogEntry
err = protojson.Unmarshal(data, &le)
return &le, err
}

//LogItemFind find LogItem records by reqexps in 'query' corresponded to LogItem structure.
// LogItemFind find LogItem records by reqexps in 'query' corresponded to LogItem structure.
func LogItemFind(le *logs.LogEntry, query map[string]string) bool {
matched := true
for k, v := range query {
// Uppercase of filed's name first letter
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(k, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -68,15 +71,15 @@ func LogItemFind(le *logs.LogEntry, query map[string]string) bool {
return matched
}

//HandleFactory implements HandlerFunc which prints log in the provided format
// HandleFactory implements HandlerFunc which prints log in the provided format
func HandleFactory(format types.OutputFormat, once bool) HandlerFunc {
return func(le *logs.LogEntry) bool {
LogPrn(le, format)
return once
}
}

//LogPrn print Log data
// LogPrn print Log data
func LogPrn(le *logs.LogEntry, format types.OutputFormat) {
switch format {
case types.OutputFormatJSON:
Expand All @@ -99,13 +102,14 @@ func LogPrn(le *logs.LogEntry, format types.OutputFormat) {
}
}

//LogItemPrint find LogItem elements by paths in 'query'
// LogItemPrint find LogItem elements by paths in 'query'
func LogItemPrint(le *logs.LogEntry, _ types.OutputFormat, query []string) *types.PrintResult {
result := make(types.PrintResult)
for _, v := range query {
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(v, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -116,8 +120,8 @@ func LogItemPrint(le *logs.LogEntry, _ types.OutputFormat, query []string) *type
return &result
}

//HandlerFunc must process LogItem and return true to exit
//or false to continue
// HandlerFunc must process LogItem and return true to exit
// or false to continue
type HandlerFunc func(*logs.LogEntry) bool

func logProcess(query map[string]string, handler HandlerFunc) loaders.ProcessFunction {
Expand All @@ -135,19 +139,19 @@ func logProcess(query map[string]string, handler HandlerFunc) loaders.ProcessFun
}
}

//LogWatch monitors the change of Log files in the 'filepath' directory
//according to the 'query' reqexps and processing using the 'handler' function.
// LogWatch monitors the change of Log files in the 'filepath' directory
// according to the 'query' reqexps and processing using the 'handler' function.
func LogWatch(loader loaders.Loader, query map[string]string, handler HandlerFunc, timeoutSeconds time.Duration) error {
return loader.ProcessStream(logProcess(query, handler), types.AppsType, timeoutSeconds)
}

//LogLast function process Log files in the 'filepath' directory
//according to the 'query' reqexps and return last founded item
// LogLast function process Log files in the 'filepath' directory
// according to the 'query' reqexps and return last founded item
func LogLast(loader loaders.Loader, query map[string]string, handler HandlerFunc) error {
return loader.ProcessExisting(logProcess(query, handler), types.AppsType)
}

//LogChecker check logs by pattern from existence files with LogLast and use LogWatchWithTimeout with timeout for observe new files
// LogChecker check logs by pattern from existence files with LogLast and use LogWatchWithTimeout with timeout for observe new files
func LogChecker(loader loaders.Loader, devUUID uuid.UUID, appUUID uuid.UUID, q map[string]string, handler HandlerFunc, mode LogCheckerMode, timeout time.Duration) (err error) {
loader.SetUUID(devUUID)
loader.SetAppUUID(appUUID)
Expand Down
40 changes: 22 additions & 18 deletions pkg/controller/eflowlog/eflowlog.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//Package eflowlog provides primitives for searching and processing data
//in FlowMessage files.
// Package eflowlog provides primitives for searching and processing data
// in FlowMessage files.
package eflowlog

import (
Expand All @@ -17,13 +17,15 @@ import (
"github.com/lf-edge/eve/api/go/flowlog"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"google.golang.org/protobuf/proto"
)

//FlowLogCheckerMode is FlowLogExist, FlowLogNew and FlowLogAny
// FlowLogCheckerMode is FlowLogExist, FlowLogNew and FlowLogAny
type FlowLogCheckerMode int

//FlowLogTail returns FlowLogCheckerMode for process only defined count of last messages
// FlowLogTail returns FlowLogCheckerMode for process only defined count of last messages
func FlowLogTail(count uint) FlowLogCheckerMode {
return FlowLogCheckerMode(count)
}
Expand All @@ -35,21 +37,22 @@ const (
FlowLogAny FlowLogCheckerMode = -1 // use both mechanisms
)

//ParseFullLogEntry unmarshal FlowMessage
// ParseFullLogEntry unmarshal FlowMessage
func ParseFullLogEntry(data []byte) (*flowlog.FlowMessage, error) {
var lb flowlog.FlowMessage
err := proto.Unmarshal(data, &lb)
return &lb, err
}

//FlowLogItemPrint find FlowMessage elements by paths in 'query'
// FlowLogItemPrint find FlowMessage elements by paths in 'query'
func FlowLogItemPrint(le *flowlog.FlowMessage, query []string) *types.PrintResult {
result := make(types.PrintResult)
for _, v := range query {
// Uppercase of filed's name first letter
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(v, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -60,14 +63,15 @@ func FlowLogItemPrint(le *flowlog.FlowMessage, query []string) *types.PrintResul
return &result
}

//FlowLogItemFind find FlowMessage records by reqexps in 'query' corresponded to FlowMessage structure.
// FlowLogItemFind find FlowMessage records by reqexps in 'query' corresponded to FlowMessage structure.
func FlowLogItemFind(le *flowlog.FlowMessage, query map[string]string) bool {
matched := true
for k, v := range query {
// Uppercase of filed's name first letter
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(k, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -88,15 +92,15 @@ func FlowLogItemFind(le *flowlog.FlowMessage, query map[string]string) bool {
return matched
}

//HandleFactory implements HandlerFunc which prints log in the provided format
// HandleFactory implements HandlerFunc which prints log in the provided format
func HandleFactory(format types.OutputFormat, once bool) HandlerFunc {
return func(le *flowlog.FlowMessage) bool {
FlowLogPrn(le, format)
return once
}
}

//FlowLogPrn print FlowMessage data
// FlowLogPrn print FlowMessage data
func FlowLogPrn(le *flowlog.FlowMessage, format types.OutputFormat) {
switch format {
case types.OutputFormatJSON:
Expand All @@ -113,8 +117,8 @@ func FlowLogPrn(le *flowlog.FlowMessage, format types.OutputFormat) {
}
}

//HandlerFunc must process FlowMessage and return true to exit
//or false to continue
// HandlerFunc must process FlowMessage and return true to exit
// or false to continue
type HandlerFunc func(*flowlog.FlowMessage) bool

func flowLogProcess(query map[string]string, handler HandlerFunc) loaders.ProcessFunction {
Expand Down Expand Up @@ -143,19 +147,19 @@ func flowLogProcess(query map[string]string, handler HandlerFunc) loaders.Proces
}
}

//FlowLogWatch monitors the change of FlowLog files in the 'filepath' directory
//according to the 'query' reqexps and processing using the 'handler' function.
// FlowLogWatch monitors the change of FlowLog files in the 'filepath' directory
// according to the 'query' reqexps and processing using the 'handler' function.
func FlowLogWatch(loader loaders.Loader, query map[string]string, handler HandlerFunc, timeoutSeconds time.Duration) error {
return loader.ProcessStream(flowLogProcess(query, handler), types.FlowLogType, timeoutSeconds)
}

//FlowLogLast function process FlowLog files in the 'filepath' directory
//according to the 'query' reqexps and return last founded item
// FlowLogLast function process FlowLog files in the 'filepath' directory
// according to the 'query' reqexps and return last founded item
func FlowLogLast(loader loaders.Loader, query map[string]string, handler HandlerFunc) error {
return loader.ProcessExisting(flowLogProcess(query, handler), types.FlowLogType)
}

//FlowLogChecker check logs by pattern from existence files with FlowLogLast and use FlowLogWatchWithTimeout with timeout for observe new files
// FlowLogChecker check logs by pattern from existence files with FlowLogLast and use FlowLogWatchWithTimeout with timeout for observe new files
func FlowLogChecker(loader loaders.Loader, devUUID uuid.UUID, q map[string]string, handler HandlerFunc, mode FlowLogCheckerMode, timeout time.Duration) (err error) {
loader.SetUUID(devUUID)
done := make(chan error)
Expand Down
42 changes: 23 additions & 19 deletions pkg/controller/einfo/einfo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//Package einfo provides primitives for searching and processing data
//in Info files.
// Package einfo provides primitives for searching and processing data
// in Info files.
package einfo

import (
Expand All @@ -15,26 +15,28 @@ import (
"github.com/lf-edge/eve/api/go/info"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

//HandlerFunc must process info.ZInfoMsg and return true to exit
//or false to continue
// HandlerFunc must process info.ZInfoMsg and return true to exit
// or false to continue
type HandlerFunc func(im *info.ZInfoMsg) bool

//QHandlerFunc must process info.ZInfoMsg with query parameters
//and return true to exit or false to continue
// QHandlerFunc must process info.ZInfoMsg with query parameters
// and return true to exit or false to continue
type QHandlerFunc func(im *info.ZInfoMsg, query map[string]string) bool

//ParseZInfoMsg unmarshal ZInfoMsg
// ParseZInfoMsg unmarshal ZInfoMsg
func ParseZInfoMsg(data []byte) (ZInfoMsg *info.ZInfoMsg, err error) {
var zi info.ZInfoMsg
err = proto.Unmarshal(data, &zi)
return &zi, err
}

//ZInfoPrn print data from ZInfoMsg structure
// ZInfoPrn print data from ZInfoMsg structure
func ZInfoPrn(im *info.ZInfoMsg, format types.OutputFormat) {
switch format {
case types.OutputFormatJSON:
Expand All @@ -54,7 +56,7 @@ func ZInfoPrn(im *info.ZInfoMsg, format types.OutputFormat) {
}
}

//HandleFactory implements HandlerFunc which prints info in the provided format
// HandleFactory implements HandlerFunc which prints info in the provided format
func HandleFactory(format types.OutputFormat, once bool) HandlerFunc {
return func(le *info.ZInfoMsg) bool {
ZInfoPrn(le, format)
Expand All @@ -67,8 +69,9 @@ func processElem(value reflect.Value, query map[string]string) bool {
for k, v := range query {
// Uppercase of filed's name first letter
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(k, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -92,14 +95,15 @@ func processElem(value reflect.Value, query map[string]string) bool {
return matched
}

//ZInfoPrintFiltered finds ZInfoMsg records by path in 'query'
// ZInfoPrintFiltered finds ZInfoMsg records by path in 'query'
func ZInfoPrintFiltered(im *info.ZInfoMsg, query []string) *types.PrintResult {
result := make(types.PrintResult)
for _, v := range query {
// Uppercase of filed's name first letter
var n []string
caser := cases.Title(language.English)
for _, pathElement := range strings.Split(v, ".") {
n = append(n, strings.Title(pathElement))
n = append(n, caser.String(pathElement))
}
var clb = func(inp reflect.Value) {
f := fmt.Sprint(inp)
Expand All @@ -113,19 +117,19 @@ func ZInfoPrintFiltered(im *info.ZInfoMsg, query []string) *types.PrintResult {
return &result
}

//ZInfoFind finds ZInfoMsg records with 'devid' and ZInfoDevSWF structure fields
//by reqexps in 'query'
// ZInfoFind finds ZInfoMsg records with 'devid' and ZInfoDevSWF structure fields
// by reqexps in 'query'
func ZInfoFind(im *info.ZInfoMsg, query map[string]string) bool {
if processElem(reflect.ValueOf(im), query) {
return true
}
return false
}

//InfoCheckerMode is InfoExist, InfoNew and InfoAny
// InfoCheckerMode is InfoExist, InfoNew and InfoAny
type InfoCheckerMode int

//InfoTail returns InfoCheckerMode for process only defined count of last messages
// InfoTail returns InfoCheckerMode for process only defined count of last messages
func InfoTail(count uint) InfoCheckerMode {
return InfoCheckerMode(count)
}
Expand All @@ -152,17 +156,17 @@ func infoProcess(query map[string]string, qhandler QHandlerFunc, handler Handler
}
}

//InfoLast search Info files in the 'filepath' directory according to the 'query' parameters accepted by the 'qhandler' function and subsequent process using the 'handler' function.
// InfoLast search Info files in the 'filepath' directory according to the 'query' parameters accepted by the 'qhandler' function and subsequent process using the 'handler' function.
func InfoLast(loader loaders.Loader, query map[string]string, qhandler QHandlerFunc, handler HandlerFunc) error {
return loader.ProcessExisting(infoProcess(query, qhandler, handler), types.InfoType)
}

//InfoWatch monitors the change of Info files in the 'filepath' directory according to the 'query' parameters accepted by the 'qhandler' function and subsequent processing using the 'handler' function with 'timeoutSeconds'.
// InfoWatch monitors the change of Info files in the 'filepath' directory according to the 'query' parameters accepted by the 'qhandler' function and subsequent processing using the 'handler' function with 'timeoutSeconds'.
func InfoWatch(loader loaders.Loader, query map[string]string, qhandler QHandlerFunc, handler HandlerFunc, timeoutSeconds time.Duration) error {
return loader.ProcessStream(infoProcess(query, qhandler, handler), types.InfoType, timeoutSeconds)
}

//InfoChecker checks the information in the regular expression pattern 'query' and processes the info.ZInfoMsg found by the function 'handler' from existing files (mode=InfoExist), new files (mode=InfoNew) or any of them (mode=InfoAny) with timeout (0 for infinite).
// InfoChecker checks the information in the regular expression pattern 'query' and processes the info.ZInfoMsg found by the function 'handler' from existing files (mode=InfoExist), new files (mode=InfoNew) or any of them (mode=InfoAny) with timeout (0 for infinite).
func InfoChecker(loader loaders.Loader, devUUID uuid.UUID, query map[string]string, handler HandlerFunc, mode InfoCheckerMode, timeout time.Duration) (err error) {
loader.SetUUID(devUUID)
done := make(chan error)
Expand Down
Loading

0 comments on commit edfde52

Please sign in to comment.