Skip to content

Commit

Permalink
Fixed GCP login crash
Browse files Browse the repository at this point in the history
  • Loading branch information
aurc committed Aug 1, 2022
1 parent 926362b commit 776f698
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 7 additions & 0 deletions cmd/gcpstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ THE SOFTWARE.
package cmd

import (
"context"
"log"
"strconv"
"time"

"github.com/aurc/loggo/internal/loggo"
"github.com/aurc/loggo/internal/reader"
Expand Down Expand Up @@ -91,6 +93,11 @@ from a given selected project and GCP logging filters:
if len(projectName) == 0 {
log.Fatal("--project flag is required.")
}
err := reader.CheckAuth(context.Background(), projectName)
if err != nil {
log.Fatal("Unable to obtain GCP credentials. ", err)
}
time.Sleep(time.Second)
reader := reader.MakeGCPReader(projectName, filter, reader.ParseFrom(from), nil)
app := loggo.NewLoggoApp(reader, templateFile)
app.Run()
Expand Down
2 changes: 2 additions & 0 deletions internal/loggo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package loggo
import (
"github.com/aurc/loggo/internal/config"
"github.com/aurc/loggo/internal/reader"
"github.com/aurc/loggo/internal/util"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -71,6 +72,7 @@ func (a *LoggoApp) Run() {
SetRoot(a.pages, true).
EnableMouse(true).
Run(); err != nil {
util.Log().Error(err)
panic(err)
}
}
24 changes: 15 additions & 9 deletions internal/reader/gcp_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,20 @@ func MakeGCPReader(project, filter, freshness string, strChan chan string) *gcpS
}
}

func (s *gcpStream) StreamInto() error {
func (s *gcpStream) StreamInto() (err error) {
defer func() {
r := recover()
if r != nil {
if e, ok := r.(error); ok {
err = e
} else {
err = fmt.Errorf("%+v", r)
}
}
}()
ctx := context.Background()
err := s.checkAuth(ctx)
if err != nil {
return err
}

c, err := logging.NewClient(ctx)
var c *logging.Client
c, err = logging.NewClient(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -196,11 +202,11 @@ func (s *gcpStream) Close() {
close(s.strChan)
}

func (s *gcpStream) checkAuth(ctx context.Context) error {
func CheckAuth(ctx context.Context, projectID string) error {
c, err := logging.NewClient(ctx)
if err == nil {
it := c.ListLogs(ctx, &loggingpb.ListLogsRequest{
ResourceNames: []string{"projects/" + s.projectID},
ResourceNames: []string{"projects/" + projectID},
PageSize: 1,
})
_, err = it.Next()
Expand Down

0 comments on commit 776f698

Please sign in to comment.