Skip to content

Commit

Permalink
fix publish
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Nov 11, 2024
1 parent 5469139 commit 843b5de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
74 changes: 51 additions & 23 deletions cmd/substreams/registry-login.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"bufio"
"errors"
"fmt"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"os"
"path/filepath"

Expand All @@ -20,37 +21,64 @@ var registryLoginCmd = &cobra.Command{
var registryTokenFilename = filepath.Join(os.Getenv("HOME"), ".config", "substreams", "registry-token")

func init() {
registryLoginCmd.Flags().String("registry", "https://substreams.dev", "Substreams registry URL")

registryCmd.AddCommand(registryLoginCmd)
}

func runRegistryLoginE(cmd *cobra.Command, args []string) error {
registryURL, err := cmd.Flags().GetString("registry")
if err != nil {
return fmt.Errorf("could not get registry URL: %w", err)
registryURL := "https://substreams.dev"
if newValue := os.Getenv("SUBSTREAMS_REGISTRY_ENDPOINT"); newValue != "" {
registryURL = newValue
}

loginRegistryPage := fmt.Sprintf("%s/me", registryURL)

fmt.Printf("Paste the token found on %s below\n", loginRegistryPage)
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("")

scanner := bufio.NewScanner(os.Stdin)
var token string
for scanner.Scan() {
token = scanner.Text()
break
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)
}

fmt.Println("")

isFileExists := checkFileExists(registryTokenFilename)
if isFileExists {
fmt.Println("Token already saved to registry-token")
fmt.Printf("Do you want to overwrite it? [y/N] ")
scanner.Scan()
if scanner.Text() == "y" {
err = writeRegistryToken(token)
var confirmOverwrite bool
form = huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title("Token already saved to registry-token").
Value(&confirmOverwrite).
Affirmative("Yes").
Negative("No"),
),
)

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

if confirmOverwrite {
err := writeRegistryToken(token)
if err != nil {
return fmt.Errorf("could not write token to registry: %w", err)
}
Expand All @@ -59,15 +87,15 @@ func runRegistryLoginE(cmd *cobra.Command, args []string) error {
}

} else {
err = writeRegistryToken(token)
err := writeRegistryToken(token)
if err != nil {
return fmt.Errorf("could not write token to registry: %w", err)
}

}

fmt.Printf("Publish packages with SUBSTREAMS_REGISTRY_TOKEN=%s\n", token)
fmt.Printf("Token %s saved to registry-token\n", token)
fmt.Printf("All set! Token written to ~/.config/substreams/registry-token")

return nil
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/substreams/registry-publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {
apiKey = substreamsRegistryToken
} else {
linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
//Let's redirect the user to substreams.dev to get a registry token and let them paste in the terminal
fmt.Println("`SUBSTREAMS_REGISTRY_TOKEN` env variable is missing...")
fmt.Println("You can get a token using the following link: ")
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("")
Expand All @@ -72,7 +73,7 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {
huh.NewGroup(
huh.NewInput().
EchoMode(huh.EchoModePassword).
Title("After retrieving your registry token, paste it here:").
Title("Paste the token here:").
Inline(true).
Value(&token).
Validate(func(s string) error {
Expand Down

0 comments on commit 843b5de

Please sign in to comment.