Skip to content

Commit

Permalink
windows CSI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 30, 2024
1 parent 0a44710 commit 88af598
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
5 changes: 3 additions & 2 deletions cmd/crowdsec-cli/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"os"

"github.com/aquasecurity/table"
isatty "github.com/mattn/go-isatty"

"github.com/crowdsecurity/go-cs-lib/cstty"
)

func shouldWeColorize() bool {
Expand All @@ -16,7 +17,7 @@ func shouldWeColorize() bool {
if csConfig.Cscli.Color == "no" {
return false
}
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
return cstty.IsTTY(os.Stdout.Fd())
}

func newTable(out io.Writer) *table.Table {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607
github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26
github.com/crowdsecurity/go-cs-lib v0.0.6
github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9
github.com/crowdsecurity/grokky v0.2.1
github.com/crowdsecurity/machineid v1.0.2
github.com/davecgh/go-spew v1.1.1
Expand Down Expand Up @@ -60,7 +60,6 @@ require (
github.com/jarcoal/httpmock v1.1.0
github.com/jszwec/csvutil v1.5.1
github.com/lithammer/dedent v1.1.0
github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-sqlite3 v1.14.16
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/nxadm/tail v1.4.8
Expand Down Expand Up @@ -157,6 +156,7 @@ require (
github.com/magefile/mage v1.15.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607 h1:hyrYw3h
github.com/crowdsecurity/coraza/v3 v3.0.0-20240108124027-a62b8d8e5607/go.mod h1:br36fEqurGYZQGit+iDYsIzW0FF6VufMbDzyyLxEuPA=
github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26 h1:r97WNVC30Uen+7WnLs4xDScS/Ex988+id2k6mDf8psU=
github.com/crowdsecurity/dlog v0.0.0-20170105205344-4fb5f8204f26/go.mod h1:zpv7r+7KXwgVUZnUNjyP22zc/D7LKjyoY02weH2RBbk=
github.com/crowdsecurity/go-cs-lib v0.0.6 h1:Ef6MylXe0GaJE9vrfvxEdbHb31+JUP1os+murPz7Pos=
github.com/crowdsecurity/go-cs-lib v0.0.6/go.mod h1:8FMKNGsh3hMZi2SEv6P15PURhEJnZV431XjzzBSuf0k=
github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9 h1:xHCBAGYm34lFc/brTurSYUn6o4yxvOBX8L8HeUuwydA=
github.com/crowdsecurity/go-cs-lib v0.0.7-0.20240130143103-452318e69ef9/go.mod h1:xA3j72N5Vd+pXKAshebmwNiJn53jOi7CfZhEDrMTJsI=
github.com/crowdsecurity/grokky v0.2.1 h1:t4VYnDlAd0RjDM2SlILalbwfCrQxtJSMGdQOR0zwkE4=
github.com/crowdsecurity/grokky v0.2.1/go.mod h1:33usDIYzGDsgX1kHAThCbseso6JuWNJXOzRQDGXHtWM=
github.com/crowdsecurity/machineid v1.0.2 h1:wpkpsUghJF8Khtmn/tg6GxgdhLA1Xflerh5lirI+bdc=
Expand Down
9 changes: 7 additions & 2 deletions pkg/acquisition/progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import (
"os"
"time"

isatty "github.com/mattn/go-isatty"
progressbar "github.com/schollz/progressbar/v3"
tomb "gopkg.in/tomb.v2"

"github.com/crowdsecurity/go-cs-lib/cstty"

"github.com/crowdsecurity/crowdsec/pkg/types"
)

// injectProgressBar will inject a progress bar in the acquisition pipeline if stderr is a terminal
func injectProgressBar(output chan types.Event, acquisTomb *tomb.Tomb) chan types.Event {
if !isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd()) {
// assume we are logging to stderr
if !cstty.IsTTY(os.Stderr.Fd()) {
return output
}

// windows may need this
_ = cstty.EnableVirtualTerminalProcessing(os.Stderr.Fd())

ret := make(chan types.Event)

go func() {
Expand Down
37 changes: 17 additions & 20 deletions pkg/types/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@ package types

import (
"fmt"
"os"
"path/filepath"
"time"

log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/crowdsecurity/go-cs-lib/cslog"
"github.com/crowdsecurity/go-cs-lib/cstty"
)

var logFormatter log.Formatter
var LogOutput *lumberjack.Logger //io.Writer
var logLevel log.Level

type CustomFormatter struct {
log.TextFormatter
}

func (f *CustomFormatter) Format(entry *log.Entry) ([]byte, error) {
const clearLine = "\r\033[K"

result, err := f.TextFormatter.Format(entry)
if err != nil {
return nil, err
}
return append([]byte(clearLine), result...), nil
}

func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
/*Configure logs*/
if cfgMode == "file" {
switch cfgMode {
case "file":
_maxsize := 500
if maxSize != 0 {
_maxsize = maxSize
Expand All @@ -55,13 +45,20 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
Compress: _compress,
}
log.SetOutput(LogOutput)
} else if cfgMode != "stdout" {
logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}
case "stdout":
if cstty.IsTTY(os.Stderr.Fd()) {
logFormatter = &cslog.ClearLineFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}}
} else {
logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}
}
default:
return fmt.Errorf("log mode '%s' unknown", cfgMode)

Check warning on line 56 in pkg/types/logging.go

View check run for this annotation

Codecov / codecov/patch

pkg/types/logging.go#L49-L56

Added lines #L49 - L56 were not covered by tests
}
logLevel = cfgLevel
log.SetLevel(logLevel)
logFormatter = &CustomFormatter{TextFormatter: log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}}

log.SetLevel(cfgLevel)
log.SetFormatter(logFormatter)

return nil
}

Expand Down

0 comments on commit 88af598

Please sign in to comment.