Skip to content

Commit

Permalink
Implementation of Logs Tail Lines (#122)
Browse files Browse the repository at this point in the history
* Implementation of logs taillines and some minor improvements

* Run go mod tidy

* PR feedback
  • Loading branch information
daniel-de-vera authored Jun 10, 2024
1 parent 78405f0 commit e5b5274
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249
github.com/oklog/run v1.1.0
github.com/panta/machineid v1.0.2
github.com/r3labs/sse/v2 v2.10.0
github.com/signadot/go-sdk v0.3.8-0.20240529233641-955e0237e964
github.com/signadot/libconnect v0.1.1-0.20240306100356-4c865b888453
github.com/spf13/cobra v1.6.0
Expand All @@ -38,7 +39,6 @@ require (
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/mwitkow/grpc-proxy v0.0.0-20230212185441-f345521cb9c9 // indirect
github.com/r3labs/sse/v2 v2.10.0 // indirect
golang.org/x/sync v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304212257-790db918fca8 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
Expand Down
26 changes: 0 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand Down Expand Up @@ -302,24 +300,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/signadot/go-sdk v0.3.7 h1:r8OuhbKzV9a55o7ZTkWuANGbwljfAcZBSwOqstN+tj8=
github.com/signadot/go-sdk v0.3.7/go.mod h1:O27LoqeYQzLmIUVCZ/dTukJSEtmZMnqP1Upp7I00tEg=
github.com/signadot/go-sdk v0.3.8-0.20240514115106-afd906e78099 h1:BjcaLV9xxB7J5F8UW72Y1bUUVe1k2p5XONpXbjPaEN8=
github.com/signadot/go-sdk v0.3.8-0.20240514115106-afd906e78099/go.mod h1:dgIOK+FoU5oEWHXYFI4diXJtBLVJTWsivUl7u8D5eCQ=
github.com/signadot/go-sdk v0.3.8-0.20240514201336-f8c37ac3c72e h1:yfgTUgk4TtvZx7OGrBxY8HN4WTi0098p6m//8/eiJrE=
github.com/signadot/go-sdk v0.3.8-0.20240514201336-f8c37ac3c72e/go.mod h1:dgIOK+FoU5oEWHXYFI4diXJtBLVJTWsivUl7u8D5eCQ=
github.com/signadot/go-sdk v0.3.8-0.20240516151729-61be7eb1919a h1:3fEzB8edKrTo4jldh7GvawMW0kwY2JrKXfbyxwgATU4=
github.com/signadot/go-sdk v0.3.8-0.20240516151729-61be7eb1919a/go.mod h1:dgIOK+FoU5oEWHXYFI4diXJtBLVJTWsivUl7u8D5eCQ=
github.com/signadot/go-sdk v0.3.8-0.20240516163659-87dfff7a3922 h1:IBlllSFc/DQk33JzVI0RkFZ9/KlaCa8RWiKAfnO78hM=
github.com/signadot/go-sdk v0.3.8-0.20240516163659-87dfff7a3922/go.mod h1:dgIOK+FoU5oEWHXYFI4diXJtBLVJTWsivUl7u8D5eCQ=
github.com/signadot/go-sdk v0.3.8-0.20240521210827-dc1d01e5e52b h1:0Qwmpo4x9vCtqnbMeoIirblxWfHOXL5y42HJepgZtwc=
github.com/signadot/go-sdk v0.3.8-0.20240521210827-dc1d01e5e52b/go.mod h1:LBc3zdVqtLQpXo78HN/DrMhf6PBcfyhlg/tVRrDL+sg=
github.com/signadot/go-sdk v0.3.8-0.20240522192013-3882db39d258 h1:ilo+iUvNMfCDxaS/msDJd28l2M5l+M2Tn28AnLG1wAI=
github.com/signadot/go-sdk v0.3.8-0.20240522192013-3882db39d258/go.mod h1:LBc3zdVqtLQpXo78HN/DrMhf6PBcfyhlg/tVRrDL+sg=
github.com/signadot/go-sdk v0.3.8-0.20240524154550-b1a43ad9a2c0 h1:bK8z1qD26+MfE4rxzHno6d06NzCqEx4JHrVfiIF5ZGc=
github.com/signadot/go-sdk v0.3.8-0.20240524154550-b1a43ad9a2c0/go.mod h1:LBc3zdVqtLQpXo78HN/DrMhf6PBcfyhlg/tVRrDL+sg=
github.com/signadot/go-sdk v0.3.8-0.20240529180930-5352d0cabf47 h1:av5aTGgRth05JoiRZwua5TPs0o9cD6OUHmhKrg8fN6A=
github.com/signadot/go-sdk v0.3.8-0.20240529180930-5352d0cabf47/go.mod h1:LBc3zdVqtLQpXo78HN/DrMhf6PBcfyhlg/tVRrDL+sg=
github.com/signadot/go-sdk v0.3.8-0.20240529233641-955e0237e964 h1:S8mXSuI/JcNTkaoXL2L0HDLGgakKffmMMswmZwrLSmM=
github.com/signadot/go-sdk v0.3.8-0.20240529233641-955e0237e964/go.mod h1:LBc3zdVqtLQpXo78HN/DrMhf6PBcfyhlg/tVRrDL+sg=
github.com/signadot/libconnect v0.1.1-0.20240306100356-4c865b888453 h1:omG9Iuz5vO0wNvpX/o1sAu+yuHnjHp6okvV9dDRCcd4=
Expand Down Expand Up @@ -369,18 +349,12 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg=
go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ=
go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30=
go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4=
go.opentelemetry.io/otel/metric v1.27.0 h1:hvj3vdEKyeCi4YaYfNjv2NUje8FqKqUY8IlF0FxV/ik=
go.opentelemetry.io/otel/metric v1.27.0/go.mod h1:mVFgmRlhljgBiuk/MP/oKylr4hs85GZAylncepAX/ak=
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
go.opentelemetry.io/otel/trace v1.27.0 h1:IqYb813p7cmbHk0a5y6pD5JPakbVfftRXABGt5/Rscw=
go.opentelemetry.io/otel/trace v1.27.0/go.mod h1:6RiD1hkAprV4/q+yd2ln1HG9GoPx39SuvvstaLBl+l4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
11 changes: 6 additions & 5 deletions internal/command/artifact/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package artifact

import (
"fmt"
"io"
"os"
"path"
"strings"

"github.com/go-openapi/runtime"
"github.com/signadot/cli/internal/config"
"github.com/signadot/go-sdk/client"
"github.com/signadot/go-sdk/client/artifacts"
"github.com/spf13/cobra"
"io"
"os"
"path"
"strings"
)

func newDownload(artifact *config.Artifact) *cobra.Command {
cfg := &config.ArtifactDownload{Artifact: artifact}

cmd := &cobra.Command{
Use: "download PATH",
Short: "Download job",
Short: "Download job artifacts",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return download(cfg, cmd.OutOrStdout(), args[0])
Expand Down
35 changes: 22 additions & 13 deletions internal/command/logs/command.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package logs

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"strconv"

"github.com/r3labs/sse/v2"
"github.com/signadot/cli/internal/config"
Expand All @@ -17,10 +18,10 @@ func New(api *config.API) *cobra.Command {

cmd := &cobra.Command{
Use: "logs",
Short: "Inspect and manipulate artifact",
Short: "Display job logs",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return list(cfg, cmd.OutOrStdout())
return display(cmd.Context(), cfg, cmd.OutOrStdout())
},
}

Expand All @@ -29,7 +30,11 @@ func New(api *config.API) *cobra.Command {
return cmd
}

func list(cfg *config.Logs, out io.Writer) error {
type log struct {
Message string `json:"message"`
}

func display(ctx context.Context, cfg *config.Logs, out io.Writer) error {
if err := cfg.InitAPIConfig(); err != nil {
return err
}
Expand All @@ -39,6 +44,9 @@ func list(cfg *config.Logs, out io.Writer) error {
}
u.Path = "/api/v2/orgs/" + cfg.Org + "/jobs/" + cfg.Job + "/attempts/0/logs/stream"
u.RawQuery = "type=" + cfg.Stream
if cfg.TailLines > 0 {
u.RawQuery += "&tailLines=" + strconv.FormatUint(uint64(cfg.TailLines), 10)
}

events := make(chan *sse.Event)

Expand All @@ -54,34 +62,35 @@ func list(cfg *config.Logs, out io.Writer) error {

for {
select {
case event := <-events:
type Log struct {
Message string `json:"message"`
case event, ok := <-events:
if !ok {
return nil
}

switch string(event.Event) {
case "message":
var log Log
var log log
if err := json.Unmarshal(event.Data, &log); err != nil {
return err
}
out.Write([]byte(log.Message))

fmt.Print(log.Message)
case "error":
return errors.New(string(event.Data))
case "signal":

case "signal":
switch string(event.Data) {
case "EOF":
fmt.Println()
return nil
case "RESTART":
fmt.Println("\n\n-----------")
out.Write([]byte("\n\n--------------------------------------------------"))
default:
return nil
}
}

case <-ctx.Done():
return nil
}
}

}
10 changes: 6 additions & 4 deletions internal/config/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
type Logs struct {
*API

Job string
Stream string
Job string
Stream string
TailLines uint
}

func (c *Logs) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&c.Job, "job", "j", "", "job name where to get the attempt Logs")
cmd.Flags().StringVarP(&c.Job, "job", "j", "", "job name whose log lines will be displayed")
cmd.MarkFlagRequired("job")

cmd.Flags().StringVarP(&c.Stream, "stream", "s", "stdout", "channel where get the logs stdout or stderr")
cmd.Flags().StringVarP(&c.Stream, "stream", "s", "stdout", "stream from where to display log lines (stdout or stderr)")
cmd.Flags().UintVarP(&c.TailLines, "tail", "t", 0, "lines of recent log file to display, defaults to 0, showing all log lines")
}

0 comments on commit e5b5274

Please sign in to comment.