Skip to content

Commit

Permalink
A few bits of formatting. SUBSTREAMS_REGISTRY_TOKEN takes precedence …
Browse files Browse the repository at this point in the history
…over the

presence of the registry-token file.
  • Loading branch information
Alexandre Bourget committed Nov 13, 2024
1 parent 180b2ec commit 51c1f85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
3 changes: 2 additions & 1 deletion cmd/substreams/registry-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func runRegistryLoginE(cmd *cobra.Command, args []string) error {

}

fmt.Printf("All set! Token written to ~/.config/substreams/registry-token")
fmt.Printf("All set! Token written to ~/.config/substreams/registry-token\n")
fmt.Println()

return nil
}
Expand Down
62 changes: 32 additions & 30 deletions cmd/substreams/registry-publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,15 @@ var registryPublish = &cobra.Command{
RunE: runRegistryPublish,
}

func runRegistryPublish(cmd *cobra.Command, args []string) error {
func runRegistryPublish(cmd *cobra.Command, args []string) (err error) {
apiEndpoint := getSubstreamsRegistryEndpoint()

var apiKey string
registryTokenBytes, err := os.ReadFile(registryTokenFilename)
token, err := getRegistryToken(apiEndpoint)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read registry token: %w", err)
}
}

registryTokenBytes = bytes.TrimSpace(registryTokenBytes)

substreamsRegistryToken := os.Getenv("SUBSTREAMS_REGISTRY_TOKEN")
apiKey = string(registryTokenBytes)
if apiKey == "" {
if substreamsRegistryToken != "" {
apiKey = substreamsRegistryToken
} else {
fmt.Println("No registry token found...")
fmt.Println()
fmt.Println()
linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
token, err := copyPasteTokenForm(apiEndpoint, linkStyle)
if err != nil {
return fmt.Errorf("creating copy, paste token form %w", err)
}

// Set the API_KEY using the input token
apiKey = token
}
return fmt.Errorf("getting registry token: %w", err)
}

zlog.Debug("loaded api key", zap.String("api_key", apiKey))
zlog.Debug("loaded api key", zap.String("token", token))

var manifestPath string
switch len(args) {
Expand Down Expand Up @@ -146,7 +121,7 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-Api-Key", apiKey)
req.Header.Set("X-Api-Key", token)

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -183,6 +158,33 @@ func runRegistryPublish(cmd *cobra.Command, args []string) error {
return nil
}

func getRegistryToken(apiEndpoint string) (string, error) {
token := os.Getenv("SUBSTREAMS_REGISTRY_TOKEN")
if token == "" {
registryTokenBytes, err := os.ReadFile(registryTokenFilename)
if err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("failed to read registry token: %w", err)
}
}

token = strings.TrimSpace(string(registryTokenBytes))

if token == "" {
fmt.Println("No registry token found...")
fmt.Println()
linkStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
inputtedToken, err := copyPasteTokenForm(apiEndpoint, linkStyle)
if err != nil {
return "", fmt.Errorf("creating copy, paste token form %w", err)
}

token = inputtedToken
}
}
return token, nil
}

func copyPasteTokenForm(endpoint string, linkStyle lipgloss.Style) (string, error) {
fmt.Printf("Login to the Substreams registry.")
fmt.Println()
Expand Down

0 comments on commit 51c1f85

Please sign in to comment.