Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
michurin committed Jun 15, 2024
1 parent 42007ba commit 1321cee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
3 changes: 3 additions & 0 deletions cmd/pplog/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const buffSize = 32768
30 changes: 16 additions & 14 deletions cmd/pplog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ import (
"github.com/michurin/human-readable-json-logging/slogtotext"
)

//nolint:gochecknoglobals

Check failure on line 20 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:gochecknoglobals` is unused for linter "gochecknoglobals" (nolintlint)

Check failure on line 20 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:gochecknoglobals` is unused for linter "gochecknoglobals" (nolintlint)
var (
debugFlag = false
showVersionFlag = false
childModeFlag = false
)

func init() {
flag.BoolVar(&debugFlag, "d", false, "debug mode")
flag.BoolVar(&showVersionFlag, "v", false, "show version and exit")
flag.BoolVar(&childModeFlag, "c", false, "child mode. pplog runs as child of target process")
flag.Parse()
}

func deb(m string) {
if debugFlag {
fmt.Println("DEBUG: " + m)
fmt.Println("DEBUG: " + m) //nolint:forbidigo

Check failure on line 29 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)

Check failure on line 29 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)
}
}

Expand Down Expand Up @@ -75,10 +69,10 @@ func normLine(t string) string {
func showBuildInfo() {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("Cannot get build info")
fmt.Println("Cannot get build info") //nolint:forbidigo

Check failure on line 72 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)

Check failure on line 72 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)
return
}
fmt.Println(info.String())
fmt.Println(info.String()) //nolint:forbidigo

Check failure on line 75 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)

Check failure on line 75 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo` is unused for linter "forbidigo" (nolintlint)
}

func readEnvs() (string, string, bool) {
Expand Down Expand Up @@ -117,21 +111,29 @@ func readEnvs() (string, string, bool) {

func runPipeMode(lineFmt, errFmt func([]slogtotext.Pair) error) {
deb("run pipe mode")
err := slogtotext.Read(os.Stdin, lineFmt, errFmt, 32768)
err := slogtotext.Read(os.Stdin, lineFmt, errFmt, buffSize)
if err != nil {
printError(err)
return
}
}

func main() {
flag.BoolVar(&debugFlag, "d", false, "debug mode")
flag.BoolVar(&showVersionFlag, "v", false, "show version and exit")
flag.BoolVar(&childModeFlag, "c", false, "child mode. pplog runs as child of target process")
flag.Parse()

deb(fmt.Sprintf("flags: debug=%t, showVersion=%t, childMode=%t", debugFlag, showVersionFlag, childModeFlag))

if showVersionFlag {
showBuildInfo()
return
}

lineTemplate, errTemplate, childMode := readEnvs()
outputStream := os.Stdout // TODO make it tunable

if flag.NArg() >= 1 {
if childModeFlag || childMode {
runSubprocessModeChild()
Expand All @@ -146,16 +148,16 @@ func main() {
func printError(err error) { // TODO reconsider
pe := new(os.PathError)
if errors.As(err, &pe) {
if pe.Err == syscall.EBADF { // fragile code; somehow syscall.Errno.Is doesn't recognize EBADF, so we unable to use errors.As
if pe.Err == syscall.EBADF { //nolint:errorlint // fragile code; somehow syscall.Errno.Is doesn't recognize EBADF, so we unable to use errors.As

Check failure on line 151 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:errorlint // fragile code; somehow syscall.Errno.Is doesn't recognize EBADF, so we unable to use errors.As` is unused for linter "errorlint" (nolintlint)

Check failure on line 151 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:errorlint // fragile code; somehow syscall.Errno.Is doesn't recognize EBADF, so we unable to use errors.As` is unused for linter "errorlint" (nolintlint)
// maybe it is good idea just ignore SIGPIPE
fmt.Fprintf(os.Stderr, "PPLog: It seems output descriptor has been closed\n") // trying to report it to stderr
return
}
}
xe := new(exec.ExitError)
if errors.As(err, &xe) {
fmt.Printf("exit code = %d: %s\n", xe.ExitCode(), xe.Error()) // just for information
fmt.Printf("exit code = %d: %s\n", xe.ExitCode(), xe.Error()) //nolint:forbidigo // just for information

Check failure on line 159 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo // just for information` is unused for linter "forbidigo" (nolintlint)

Check failure on line 159 in cmd/pplog/main.go

View workflow job for this annotation

GitHub Actions / Unit test and lint

directive `//nolint:forbidigo // just for information` is unused for linter "forbidigo" (nolintlint)
return
}
fmt.Printf("Error: %s\n", err.Error())
fmt.Printf("Error: %s\n", err.Error()) //nolint:forbidigo
}
2 changes: 1 addition & 1 deletion cmd/pplog/run_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func runSubprocessMode(lineFmt, errFmt func([]slogtotext.Pair) error) {
done := make(chan struct{})

go func() {
err := slogtotext.Read(rd, lineFmt, errFmt, 32768)
err := slogtotext.Read(rd, lineFmt, errFmt, buffSize)
if err != nil {
deb("reader is finished with err: " + err.Error())
return
Expand Down

0 comments on commit 1321cee

Please sign in to comment.