Skip to content

Commit

Permalink
Merge pull request #80 from logfire-ai/repo-sync/cli-private/main
Browse files Browse the repository at this point in the history
🔄 synced file(s) with logfire-ai/cli-private
  • Loading branch information
constient-altrova authored Jun 12, 2024
2 parents 857e9d1 + 42cc35a commit 648c25e
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 61 deletions.
27 changes: 11 additions & 16 deletions internal/prompter/tea_onboarding_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ func waitForLog(m *model) {
go grpcutil.GetLog(m.config, m.config.Get().Token, m.config.Get().EndPoint, m.config.Get().TeamId, m.config.Get().AccountId, m.sourceId, m.sourceToken, stop)
err := <-stop
if err != nil {
m.err = errors.New("we apologize for the inconvenience. There seems to be an error on our end or with our server.\nPlease try again later or contact our support team for assistance")
m.err = err
// m.err = errors.New("we apologize for the inconvenience. There seems to be an error on our end or with our server.\nPlease try again later or contact our support team for assistance")
m.nextInput()
}
subStep = "awesome"
Expand All @@ -294,25 +295,24 @@ func (m *model) handleKeyPres() (tea.Model, tea.Cmd) {
switch subStep {
case "email":
if m.inputs[email].Value() != "" {
msg, err := APICalls.SignupFlow(m.inputs[email].Value(), m.config.Get().EndPoint)
_, err := APICalls.SignupFlow(m.inputs[email].Value(), m.config.Get().EndPoint)
if err != nil {

m.err = err
return m, nil
} else if msg == "already registered user. Sent link to login" {
// m.err = errors.New("you are already a user, please use logfire commands")
// os.Exit(0)
// return m, nil
}
subStep = "token"
m.nextInput()
m.nextInput()
}
case "token":
if m.inputs[token].Value() != "" {
err := APICalls.TokenSignIn(m.config, m.inputs[token].Value(), m.config.Get().EndPoint)
err, onboarded := APICalls.TokenSignIn(m.config, m.inputs[token].Value(), m.config.Get().EndPoint)
if err != nil {
m.err = err
return m, nil
} else if onboarded {
m.err = errors.New("you have already onboarded. Please use `logfire stream` to start streaming logs or `logfire` to interact with the cli and create teams, sources and more")
return m, nil
}
step = "account-setup"
subStep = "firstName"
Expand Down Expand Up @@ -617,18 +617,15 @@ func (m model) View() string {
currentTime := time.Now()
formattedTime := currentTime.Format("2006-01-02 15:04:05")

fmt.Println("Thank you for signing up")

fmt.Printf(
`
curlCmd := fmt.Sprintf(`
%s
%s %s %s \
%s %s \
%s %s \
%s %s
%s
`,
`,
continueStyle.Render("******************************************************************************************"),
colorThree.Render(`curl`),
colorThree.Render(`--location`),
Expand All @@ -642,9 +639,7 @@ func (m model) View() string {
colorTwo.Render("\nOpen Web app or run `logfire stream` to start streaming logs, You can test the ingestion by copying the command and pasting it in any terminal"),
)

os.Exit(0)

return "Completed!"
return renderWelcome() + renderSection("Signup", true) + m.renderEmail() + m.renderToken() + renderSection("Account setup", true) + m.renderAccountSetup() + renderSection("Create a Team", true) + m.renderTeamName() + renderSection("Send logs", true) + m.renderSource() + m.renderCurlCommand() + awesomeLogReceived + renderSection("Config source", false) + m.renderConfig() + finishMessage + curlCmd + "Completed!"
}

return ""
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/alerts/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package alerts
import (
"errors"
"fmt"
"net/http"
"os"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
"github.com/logfire-sh/cli/pkg/cmd/alerts/alerts_create"
Expand All @@ -13,7 +16,6 @@ import (
"github.com/logfire-sh/cli/pkg/cmdutil"
"github.com/logfire-sh/cli/pkg/iostreams"
"github.com/spf13/cobra"
"net/http"
)

type PromptAlertOptions struct {
Expand All @@ -27,7 +29,7 @@ type PromptAlertOptions struct {
Choice string
}

var choices = []string{"Create", "List", "Delete", "Pause", "Update"}
var choices = []string{"Create", "List", "Delete", "Pause", "Update", "Exit"}

func NewCmdAlerts(f *cmdutil.Factory) *cobra.Command {
opts := &PromptAlertOptions{
Expand Down Expand Up @@ -60,6 +62,8 @@ func NewCmdAlerts(f *cmdutil.Factory) *cobra.Command {
alerts_pause.NewPauseAlertCmd(f).Run(cmd, []string{})
case choices[4]:
alerts_update.NewAlertUpdateCmd(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
}
},
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"os"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
Expand All @@ -27,7 +28,7 @@ type PromptIntegrationsOptions struct {
Choice string
}

var choices = []string{"Create", "List", "Delete", "Update"}
var choices = []string{"Create", "List", "Delete", "Update", "Exit"}

func NewCmdIntegrations(f *cmdutil.Factory) *cobra.Command {
opts := &PromptIntegrationsOptions{
Expand Down Expand Up @@ -58,6 +59,8 @@ func NewCmdIntegrations(f *cmdutil.Factory) *cobra.Command {
integrations_delete.NewDeleteIntegrationCmd(f).Run(cmd, []string{})
case choices[3]:
integrations_update.NewUpdateIntegrationsCmd(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
}
},
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewLoginCmd(f *cmdutil.Factory) *cobra.Command {
# authenticate against logfire.ai by magic link token
# First request a Magic link to your email address
$ logfire login --email <[email protected]>
# Second authenticate using the token received on your email address
$ logfire login --token <token>
`),
Expand Down Expand Up @@ -120,7 +120,7 @@ func loginRun(opts *LoginOptions) {
}
}

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

if opts.Interactive && opts.Token == "" && opts.Email == "" && opts.Password == "" {
choice, err := opts.Prompter.Select("Select login method (Default: Magic link)", "Magic link", choiceList)
Expand Down Expand Up @@ -187,6 +187,9 @@ func loginRun(opts *LoginOptions) {
fmt.Fprintf(opts.IO.ErrOut, "\n%s Failed to sign in\n", cs.FailureIcon())
return
}

case "Exit":
os.Exit(0)
}
} else {
isEmpty := func(s string) bool {
Expand Down
7 changes: 5 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
"fmt"
"os"

"github.com/logfire-sh/cli/pkg/cmd/settings"

Expand Down Expand Up @@ -40,7 +41,7 @@ type PromptRootOptions struct {
}

var choices = []string{"Reset password", "Logout", "Sources", "Teams",
"Tail", "Start Stream", "Views", "Alerts", "Integrations", "SQL", "Update profile", "Settings", "Round trip"}
"Tail", "Start Stream", "Views", "Alerts", "Integrations", "SQL", "Update profile", "Settings", "Round trip", "Exit"}

var NotLoggedInChoices = []string{"Signup", "Login"}

Expand All @@ -63,7 +64,7 @@ func NewCmdRoot(f *cmdutil.Factory, cmdCh chan bool) (*cobra.Command, error) {
Example: heredoc.Doc(`
$ logfire login
$ logfire stream livetail
`),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// require that the user is authenticated before running most commands
Expand Down Expand Up @@ -131,6 +132,8 @@ func NewCmdRoot(f *cmdutil.Factory, cmdCh chan bool) (*cobra.Command, error) {
settings.SettingsCmd(f).Run(cmd, []string{})
case choices[12]:
roundtrip.NewCmdRoundTrip(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
default:
break
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package settings
import (
"fmt"
"net/http"
"os"
"strings"

"github.com/MakeNowJust/heredoc"
Expand All @@ -16,7 +17,7 @@ import (
"github.com/spf13/cobra"
)

var choices = []string{"Change default team", "Change theme"}
var choices = []string{"Change default team", "Change theme", "Exit"}

type SettingsOptions struct {
IO *iostreams.IOStreams
Expand Down Expand Up @@ -95,6 +96,10 @@ func SettingsRun(opts *SettingsOptions) {
}
}

if opts.Interactive && opts.Choice == "Exit" {
os.Exit(0)
}

if opts.Interactive && opts.Choice == "Change theme" {
themeOptions := []string{"Dark", "Light"}

Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/sources/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"os"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
Expand All @@ -28,7 +29,7 @@ type PromptSourceOptions struct {
Choice string
}

var choices = []string{"Create", "List", "Delete", "Update", "Configuration"}
var choices = []string{"Create", "List", "Delete", "Update", "Configuration", "Exit"}

func NewCmdSource(f *cmdutil.Factory) *cobra.Command {
opts := &PromptSourceOptions{
Expand Down Expand Up @@ -61,6 +62,8 @@ func NewCmdSource(f *cmdutil.Factory) *cobra.Command {
source_update.NewSourceUpdateCmd(f).Run(cmd, []string{})
case choices[4]:
source_config.NewSourceConfigCmd(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
}
},
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/teams/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package teams
import (
"errors"
"fmt"
"net/http"
"os"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
"github.com/logfire-sh/cli/pkg/cmd/teams/member_invite"
Expand All @@ -16,7 +19,6 @@ import (
"github.com/logfire-sh/cli/pkg/cmdutil"
"github.com/logfire-sh/cli/pkg/iostreams"
"github.com/spf13/cobra"
"net/http"
)

type PromptTeamsOptions struct {
Expand All @@ -30,7 +32,7 @@ type PromptTeamsOptions struct {
Choice string
}

var choices = []string{"Create team", "List teams", "Delete team", "Update team", "Invite members", "List members", "Remove member", "Update member"}
var choices = []string{"Create team", "List teams", "Delete team", "Update team", "Invite members", "List members", "Remove member", "Update member", "Exit"}

func NewCmdTeam(f *cmdutil.Factory) *cobra.Command {
opts := &PromptTeamsOptions{
Expand Down Expand Up @@ -69,6 +71,8 @@ func NewCmdTeam(f *cmdutil.Factory) *cobra.Command {
member_remove.NewMemberRemoveCmd(f).Run(cmd, []string{})
case choices[7]:
member_update.NewMemberUpdateCmd(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
}
},
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package views
import (
"errors"
"fmt"
"net/http"
"os"

"github.com/logfire-sh/cli/internal/config"
"github.com/logfire-sh/cli/internal/prompter"
"github.com/logfire-sh/cli/pkg/cmd/views/views_delete"
"github.com/logfire-sh/cli/pkg/cmd/views/views_list"
"github.com/logfire-sh/cli/pkg/cmdutil"
"github.com/logfire-sh/cli/pkg/iostreams"
"github.com/spf13/cobra"
"net/http"
)

type PromptViewsOptions struct {
Expand All @@ -24,7 +26,7 @@ type PromptViewsOptions struct {
Choice string
}

var choices = []string{"List", "Delete"}
var choices = []string{"List", "Delete", "Exit"}

func NewCmdViews(f *cmdutil.Factory) *cobra.Command {
opts := &PromptViewsOptions{
Expand All @@ -51,6 +53,8 @@ func NewCmdViews(f *cmdutil.Factory) *cobra.Command {
views_list.NewViewListCmd(f).Run(cmd, []string{})
case choices[1]:
views_delete.NewDeleteCmd(f).Run(cmd, []string{})
case "Exit":
os.Exit(0)
}
},
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/cmdutil/APICalls/profile_api_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func OnboardingFlow(profileID, authToken, endpoint, firstName, lastName, role st
return nil
}

func TokenSignIn(cfg config.Config, token, endpoint string) error {
func TokenSignIn(cfg config.Config, token, endpoint string) (error, bool) {
var response LoginModels.Response

signinReq := LoginModels.SigninRequest{
Expand All @@ -381,14 +381,14 @@ func TokenSignIn(cfg config.Config, token, endpoint string) error {
reqBody, err := json.Marshal(signinReq)
if err != nil {
fmt.Printf("Failed to marshal request body: %v\n", err)
return err
return err, false
}

url := endpoint + "api/auth/signin"

req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return err
return err, false
}
req.Header.Set("User-Agent", "Logfire-cli")
req.Header.Add("Content-Type", "application/json")
Expand All @@ -400,33 +400,37 @@ func TokenSignIn(cfg config.Config, token, endpoint string) error {
os.Exit(1)
}

return err
return err, false
}

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body: %v\n", err)
return err
return err, false
}

err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("Failed to unmarshal JSON: %v\n", err)
return err
return err, false
}

if !response.IsSuccessful {
return errors.New(response.Message[0])
return errors.New(response.Message[0]), false
}

err = cfg.UpdateConfig(&response.UserBody.Email, &response.UserBody.Role, &response.BearerToken.AccessToken, &response.UserBody.ProfileID,
&response.BearerToken.RefreshToken, nil, &response.UserBody.AccountID, nil, nil, nil, nil)
if err != nil {
fmt.Printf("Failed to update config: %v\n", err)
return err
return err, false
}

return nil
if response.UserBody.Onboarded {
return nil, true
}

return nil, false
}

func UpdateFlag(cfg config.Config, profileID, teamId, endpoint string) error {
Expand Down
Loading

0 comments on commit 648c25e

Please sign in to comment.