Skip to content

Commit

Permalink
Merge pull request #67 from logfire-ai/repo-sync/cli-private/main
Browse files Browse the repository at this point in the history
🔄 synced file(s) with logfire-sh/cli-private
  • Loading branch information
Quelaan1 authored Nov 30, 2023
2 parents debc9cb + 83d1d51 commit f35c3fc
Show file tree
Hide file tree
Showing 17 changed files with 2,774 additions and 719 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
Expand All @@ -61,6 +62,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
Expand Down Expand Up @@ -249,6 +250,7 @@ github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
Expand Down
79 changes: 41 additions & 38 deletions gui/gui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gui

import (
"context"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -41,6 +42,8 @@ type UI struct {

StartDateUnParsed string
EndDateUnParsed string

Ctx context.Context
}

type LivetailStatus struct {
Expand All @@ -51,8 +54,6 @@ var livetailStatus = &LivetailStatus{
LivetailEnabled: false,
}

var stop = make(chan error)

func NewUI(cfg config.Config) *UI {

displayInstance := NewDisplay(cfg)
Expand All @@ -76,20 +77,22 @@ func NewUI(cfg config.Config) *UI {

// go checkWaitingForLogs(ui, ui.Livetail)

ui.Ctx = context.Background()

ui.Livetail.CreateConnection()

time.Sleep(200 * time.Millisecond)

RunLivetail(ui, livetailStatus, stop)
RunLivetail(ui, livetailStatus)
return ui
}

func display(u *UI, l *livetail.Livetail, stop chan error) {
func display(u *UI, l *livetail.Livetail) {
var numDots int

for {
select {
case <-stop:
case <-u.Ctx.Done():
return
default:
u.app.QueueUpdateDraw(func() {
Expand Down Expand Up @@ -160,8 +163,8 @@ func splitFieldFilterValue(input string) (field, operator, value string) {
var sourceNamesList []string
var sourceIds []string

func (u *UI) runRootCmd() {
StopLivetail(u, livetailStatus, stop)
func (u *UI) runQuitCmd() {
StopLivetail(u, livetailStatus)
u.app.Stop()
}

Expand All @@ -186,36 +189,36 @@ func (u *UI) SetDisplayCapture() {
}
}

StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

u.SourceFilter = sourceIds

time.Sleep(500 * time.Millisecond)

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)

} else if strings.Split(input, "=")[0] == "start-date" {
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

u.StartDateTimeFilter = filters.ShortDateTimeToGoDate(strings.Split(input, "=")[1])
u.StartDateUnParsed = strings.Split(input, "=")[1]

time.Sleep(500 * time.Millisecond)

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)

} else if strings.Split(input, "=")[0] == "end-date" {
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

u.EndDateTimeFilter = filters.ShortDateTimeToGoDate(strings.Split(input, "=")[1])
u.EndDateUnParsed = strings.Split(input, "=")[1]

time.Sleep(500 * time.Millisecond)

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)

} else if strings.Split(input, "=")[0] == "field-filter" {
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

field, operator, value := splitFieldFilterValue(input)

Expand All @@ -225,7 +228,7 @@ func (u *UI) SetDisplayCapture() {

time.Sleep(500 * time.Millisecond)

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)

} else if strings.Split(input, "=")[0] == "save-view" {
name := strings.Split(input, "=")[1]
Expand Down Expand Up @@ -276,12 +279,12 @@ func (u *UI) SetDisplayCapture() {
}

if livetailStatus.LivetailEnabled {
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

time.Sleep(200 * time.Millisecond)
}

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)
}
}
return nil
Expand All @@ -291,18 +294,18 @@ func (u *UI) SetDisplayCapture() {
case true:
switch input {
case "start":
RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)
case "stop":
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)
case "q":
u.runRootCmd()
u.runQuitCmd()
case "quit":
u.runRootCmd()
u.runQuitCmd()
case "exit":
u.runRootCmd()
u.runQuitCmd()
case "1":
case "2":
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

ResetFilters(u)

Expand All @@ -328,7 +331,7 @@ func (u *UI) SetDisplayCapture() {
case "7":
u.Display.input.SetText("save-view=")
case "9":
u.runRootCmd()
u.runQuitCmd()
default:
u.Display.BottomHelp.SetPlaceholder(" Invalid command").SetPlaceholderTextColor(tcell.ColorRed)

Expand All @@ -342,23 +345,23 @@ func (u *UI) SetDisplayCapture() {
case false:
switch input {
case "start":
RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)
case "stop":
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)
case "q":
u.runRootCmd()
u.runQuitCmd()
case "quit":
u.runRootCmd()
u.runQuitCmd()
case "exit":
u.runRootCmd()
u.runQuitCmd()
case "1":
StopLivetail(u, livetailStatus, stop)
StopLivetail(u, livetailStatus)

ResetFilters(u)

time.Sleep(200 * time.Millisecond)

RunLivetail(u, livetailStatus, stop)
RunLivetail(u, livetailStatus)

u.Display.Livetail = true
u.Display.TopHelp.SetPlaceholder(" Stream > Livetail | 1. livetail 2. view 9.QUIT [q | quit | exit]").
Expand All @@ -370,7 +373,7 @@ func (u *UI) SetDisplayCapture() {
u.Display.input.SetText("view=")
u.Display.input.Autocomplete()
case "9":
u.runRootCmd()
u.runQuitCmd()
default:
u.Display.BottomHelp.SetPlaceholder(" Invalid command").SetPlaceholderTextColor(tcell.ColorRed)

Expand All @@ -391,7 +394,7 @@ func (u *UI) SetDisplayCapture() {

var mu sync.Mutex

func RunLivetail(u *UI, livetailStatus *LivetailStatus, stop chan error) {
func RunLivetail(u *UI, livetailStatus *LivetailStatus) {
mu.Lock()
defer mu.Unlock()

Expand All @@ -405,20 +408,20 @@ func RunLivetail(u *UI, livetailStatus *LivetailStatus, stop chan error) {
u.Display.View.SetTextAlign(tview.AlignLeft)

u.Livetail.ApplyFilter(u.Config, u.SourceFilter, u.StartDateTimeFilter, u.EndDateTimeFilter, u.FieldBasedFilterName, u.FieldBasedFilterValue, u.FieldBasedFilterCondition)
go u.Livetail.GenerateLogs(stop, u.Config)
go display(u, u.Livetail, stop)
go u.Livetail.GenerateLogs(u.Ctx, u.Config)
go display(u, u.Livetail)
u.Display.View.ScrollToEnd()
}
}

func StopLivetail(u *UI, livetailStatus *LivetailStatus, stop chan error) {
func StopLivetail(u *UI, livetailStatus *LivetailStatus) {
mu.Lock()
defer mu.Unlock()

if livetailStatus.LivetailEnabled {
livetailStatus.LivetailEnabled = false
stop <- errors.New("stop")
stop <- errors.New("stop")
_, cancel := context.WithCancel(u.Ctx)
defer cancel()
}
}

Expand Down
12 changes: 7 additions & 5 deletions livetail/livetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var request = &pb.FilterRequest{
FieldBasedFilters: []*pb.FieldBasedFilter{},
SearchQueries: []string{},
Sources: []*pb.Source{},
BatchSize: 100,
BatchSize: 15,
IsScrollDown: true,
}

Expand Down Expand Up @@ -106,18 +106,20 @@ func (l *Livetail) CreateConnection() {
l.FilterService = grpcutil.NewFilterService()
}

func (l *Livetail) GenerateLogs(stop chan error, cfg config.Config) {
func (l *Livetail) GenerateLogs(ctx context.Context, cfg config.Config) {
request.Sources = l.pbSources
theme := cfg.Get().Theme

for {
select {
case <-stop:
case <-ctx.Done():
return
default:
response, err := l.FilterService.Client.GetFilteredData(context.Background(), request)
if err != nil {
stop <- err
_, cancel := context.WithCancel(ctx)
defer cancel()

return
}

Expand Down Expand Up @@ -186,7 +188,7 @@ func addOffset(sources []*pb.Source, offset map[string]uint64) []*pb.Source {
}

// getFilteredData makes the actual grpc call to connect with flink-service.
// func getFilteredData(client pb.FlinkServiceClient, sources []*pb.Source) (*pb.FilteredRecords, error) {
// func getFilteredData(client pb.FilterServiceClient, sources []*pb.Source) (*pb.FilteredRecords, error) {
// // Invoke the gRPC method
// request.Sources = sources

Expand Down
24 changes: 24 additions & 0 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type LoginOptions struct {
Password string
Token string
Staging bool
Local bool
}

func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
Expand Down Expand Up @@ -83,6 +84,7 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
cmd.Flags().StringVarP(&opts.Password, "password", "p", "", "Password of the user.")
cmd.Flags().StringVarP(&opts.Token, "token", "t", "", "Single Sign in token of the user.")
cmd.Flags().BoolVarP(&opts.Staging, "staging", "s", false, "Use staging server?")
cmd.Flags().BoolVarP(&opts.Local, "local", "l", false, "Use local server?")
return cmd
}

Expand All @@ -106,6 +108,18 @@ func loginRun(opts *LoginOptions) {
}
}

if opts.Local {
endpoint := "http://localhost:8888/"
grpc_endpoint := "localhost:8888"
grpc_ingestion := "http://localhost:8888/logfire.sh"

err = cfg.UpdateConfig(nil, nil, nil, nil,
nil, nil, &endpoint, &grpc_endpoint, &grpc_ingestion, nil)
if err != nil {
return
}
}

var choiceList = []string{"Magic link", "Password"}

if opts.Interactive && opts.Token == "" && opts.Email == "" && opts.Password == "" {
Expand Down Expand Up @@ -284,6 +298,12 @@ func PasswordSignin(io *iostreams.IOStreams, cfg config.Config, cs *iostreams.Co
return err
}
fmt.Fprintf(io.Out, "\n%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(response.UserBody.Email))


if !response.UserBody.Onboarded {
fmt.Fprintf(io.Out, "\n%s Looks like your onboarding isn't done yet. Complete it now with `logfire bootstrap` for the full experience! \n \n", cs.WarningIcon())
}

return nil
}

Expand Down Expand Up @@ -353,5 +373,9 @@ func TokenSignin(IO *iostreams.IOStreams, cfg config.Config, cs *iostreams.Color

fmt.Fprintf(IO.Out, "%s Logged in as %s\n", cs.SuccessIcon(), cs.Bold(response.Email))

if !response.UserBody.Onboarded {
fmt.Fprintf(IO.Out, "\n%s Looks like your onboarding isn't done yet. Complete it now with `logfire bootstrap` for the full experience! \n", cs.WarningIcon())
}

return nil
}
Loading

0 comments on commit f35c3fc

Please sign in to comment.