Skip to content

Commit

Permalink
remove deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Nov 11, 2024
1 parent 1d49606 commit 7342748
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 55 deletions.
34 changes: 4 additions & 30 deletions cmd/substreams/registry-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"os"
"path/filepath"

Expand All @@ -30,40 +29,15 @@ func runRegistryLoginE(cmd *cobra.Command, args []string) error {
registryURL = newValue
}

linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
fmt.Printf("Login to the Substreams registry.")
fmt.Println()
fmt.Println()
fmt.Println("Navigate to: ")
fmt.Println()
fmt.Println(" " + linkStyle.Render(fmt.Sprintf("%s/me", registryURL)))
fmt.Println("")

var token string
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
EchoMode(huh.EchoModePassword).
Title("Paste the token here:").
Inline(true).
Value(&token).
Validate(func(s string) error {
if s == "" {
return errors.New("token cannot be empty")
}
return nil
}),
),
)

if err := form.Run(); err != nil {
return fmt.Errorf("error running form: %w", err)
token, err := copyPasteTokenForm(registryURL)
if err != nil {
return fmt.Errorf("creating copy, paste token form %w", err)
}

isFileExists := checkFileExists(registryTokenFilename)
if isFileExists {
var confirmOverwrite bool
form = huh.NewForm(
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title("Token already saved to registry-token").
Expand Down
62 changes: 37 additions & 25 deletions cmd/substreams/registry-publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,12 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {
if substreamsRegistryToken != "" {
apiKey = substreamsRegistryToken
} else {
linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
fmt.Println("No registry token found...")
fmt.Println()
fmt.Println()
fmt.Println("Navigate to: ")
fmt.Println()
fmt.Println(" " + linkStyle.Render(fmt.Sprintf("%s/me", apiEndpoint)))
fmt.Println("")

var token string
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
EchoMode(huh.EchoModePassword).
Title("Paste the token here:").
Inline(true).
Value(&token).
Validate(func(s string) error {
if s == "" {
return errors.New("token cannot be empty")
}
return nil
}),
),
)

if err := form.Run(); err != nil {
return fmt.Errorf("error running form: %w", err)
token, err := copyPasteTokenForm(apiEndpoint)
if err != nil {
return fmt.Errorf("creating copy, paste token form %w", err)
}

// Set the API_KEY using the input token
Expand Down Expand Up @@ -201,3 +179,37 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {

return nil
}

func copyPasteTokenForm(endpoint string) (string, error){
linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
fmt.Printf("Login to the Substreams registry.")
fmt.Println()
fmt.Println()
fmt.Println("Navigate to: ")
fmt.Println()
fmt.Println(" " + linkStyle.Render(fmt.Sprintf("%s/me", endpoint)))
fmt.Println("")

var token string
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
EchoMode(huh.EchoModePassword).
Title("Paste the token here:").
Inline(true).
Value(&token).
Validate(func(s string) error {
if s == "" {
return errors.New("token cannot be empty")
}
return nil
}),
),
)

if err := form.Run(); err != nil {
return "", fmt.Errorf("error running form: %w", err)
}

return token, nil
}

0 comments on commit 7342748

Please sign in to comment.