Skip to content

Commit

Permalink
fix goreleaser workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Nov 8, 2024
1 parent b038cb7 commit f296044
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 100 deletions.
132 changes: 34 additions & 98 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/adrg/xdg"
Expand All @@ -18,14 +17,14 @@ import (
func NewCmdLogs() *cobra.Command {
var flags struct {
json bool
app string
}

cmd := &cobra.Command{
Use: "logs",
Aliases: []string{"log"},
Short: "View app logs",
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
ValidArgsFunction: completeApp(),
Use: "logs",
Aliases: []string{"log"},
Short: "View app logs",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
logPath := filepath.Join(xdg.CacheHome, "smallweb", "http.log")
if _, err := os.Stat(logPath); err != nil {
Expand All @@ -36,31 +35,27 @@ func NewCmdLogs() *cobra.Command {
return err
}

hosts := make(map[string]struct{})
if flags.app != "" {
hosts[fmt.Sprintf("%s.%s", flags.app, k.String("domain"))] = struct{}{}

for domain, app := range k.StringMap("customDomains") {
if app != flags.app {
continue
}

hosts[domain] = struct{}{}
}
}

// Open the log file
f, err := os.Open(logPath)
if err != nil {
return err
}
defer f.Close()

// Get the last 10 lines of the file
lines, err := tailFile(f, 10)
if err != nil {
return err
}
for _, line := range lines {
if flags.json {
fmt.Println(line)
continue
}

msg, err := formatLine(line)
if err != nil {
return fmt.Errorf("failed to format log line: %w", err)
}
fmt.Println(msg)
}

f.Seek(0, io.SeekEnd)
// Stream new lines as they are added
reader := bufio.NewReader(f)
for {
Expand All @@ -73,12 +68,23 @@ func NewCmdLogs() *cobra.Command {
return err
}

var log utils.HttpLog
if err := json.Unmarshal([]byte(line), &log); err != nil {
return fmt.Errorf("failed to unmarshal log line: %w", err)
}

if flags.json {
fmt.Println(line)
continue
}

msg, err := formatLine(line)
if len(hosts) > 0 {
if _, ok := hosts[log.Request.Host]; !ok {
continue
}
}

msg, err := formatLog(log)
if err != nil {
return fmt.Errorf("failed to format log line: %w", err)
}
Expand All @@ -89,82 +95,12 @@ func NewCmdLogs() *cobra.Command {
}

cmd.Flags().BoolVar(&flags.json, "json", false, "output logs in JSON format")
cmd.Flags().StringVar(&flags.app, "app", "", "app to view logs for")
cmd.RegisterFlagCompletionFunc("app", completeApp())

return cmd
}

func formatLine(line string) (string, error) {
var log utils.HttpLog
if err := json.Unmarshal([]byte(line), &log); err != nil {
return "", err
}

func formatLog(log utils.HttpLog) (string, error) {
return fmt.Sprintf("%s %s %s %d %d", log.Time.Format(time.RFC3339), log.Request.Method, log.Request.Url, log.Response.Status, log.Response.Bytes), nil
}

// tailFile reads the last `n` lines from the file, optimized for large files
func tailFile(f *os.File, n int) ([]string, error) {
const bufferSize = 256
var lines []string
stat, err := f.Stat()
if err != nil {
return nil, err
}

// Start reading from the end of the file
offset := stat.Size()
buf := make([]byte, bufferSize)
var currentLine strings.Builder
lineCount := 0

for offset > 0 && lineCount < n {
// Move the offset backwards by `bufferSize` each time
readSize := int64(bufferSize)
if offset < readSize {
readSize = offset
}

offset -= readSize
f.Seek(offset, io.SeekStart)
read, err := f.Read(buf[:readSize])
if err != nil {
return nil, err
}

// Process buffer from end to start
for i := read - 1; i >= 0; i-- {
if buf[i] == '\n' {
if currentLine.Len() > 0 {
// Add line in reverse order to the start of `lines`
lines = append([]string{reverseString(currentLine.String())}, lines...)
lineCount++
currentLine.Reset()
if lineCount >= n {
break
}
}
} else {
currentLine.WriteByte(buf[i])
}
}
}

// Append any remaining line if it exists and we still need lines
if currentLine.Len() > 0 && lineCount < n {
lines = append([]string{reverseString(currentLine.String())}, lines...)
}

// seek to the end of the file
f.Seek(0, io.SeekEnd)

return lines, nil
}

// reverseString reverses a string
func reverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
2 changes: 0 additions & 2 deletions goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ before:
- sh -c 'go run . completion "bash" >./completions/{{ .ProjectName }}.bash'
- sh -c 'go run . completion "zsh" >./completions/{{ .ProjectName }}.zsh'
- sh -c 'go run . completion "fish" >./completions/{{ .ProjectName }}.fish'
- sh -c 'go run . openapi >openapi.json'
changelog:
disable: true
builds:
Expand Down Expand Up @@ -39,7 +38,6 @@ snapshot:
release:
prerelease: auto
extra_files:
- glob: openapi.json
- glob: schemas/*.schema.json
github:
owner: pomdtr
Expand Down

0 comments on commit f296044

Please sign in to comment.