Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize safe names on project auth #437

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/lk/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {

// poll for keys
fmt.Printf("Please confirm access by visiting:\n\n %s\n\n", authURL.String())
browser.OpenURL(authURL.String()) // discard result; this will fail in headless environments
_ = browser.OpenURL(authURL.String()) // discard result; this will fail in headless environments

var ak *ClaimAccessKeyResponse
var pollErr error
Expand Down Expand Up @@ -325,7 +325,10 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
}

// make sure name is unique
var name = ak.ProjectName
name, err := URLSafeName(ak.URL)
if err != nil {
return err
}
if cliConfig.ProjectExists(name) {
if err := huh.NewInput().
Title("Project name already exists, please choose a different name").
Expand Down
11 changes: 2 additions & 9 deletions cmd/lk/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ func listProjects(ctx context.Context, cmd *cli.Command) error {
}).
Headers("Name", "URL", "API Key", "Default")
for _, p := range cliConfig.Projects {
name, err := p.URLSafeName()
if err != nil {
return err
}
table.Row(name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject))
}
fmt.Println(table)

Expand All @@ -288,10 +284,7 @@ func setDefaultProject(ctx context.Context, cmd *cli.Command) error {

for _, p := range cliConfig.Projects {
if p.Name != name {
slug, err := p.URLSafeName()
if err != nil || slug != name {
continue
}
continue
}

cliConfig.DefaultProject = p.Name
Expand Down
17 changes: 16 additions & 1 deletion cmd/lk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/livekit/protocol/utils/interceptors"
"github.com/twitchtv/twirp"
"github.com/urfave/cli/v3"

"github.com/livekit/protocol/utils/interceptors"

"github.com/livekit/livekit-cli/pkg/config"
)

Expand Down Expand Up @@ -283,3 +285,16 @@ func loadProjectDetails(c *cli.Command, opts ...loadOption) (*config.ProjectConf
// cannot happen
return pc, nil
}

func URLSafeName(projectURL string) (string, error) {
parsed, err := url.Parse(projectURL)
if err != nil {
return "", errors.New("invalid URL")
}
subdomain := strings.Split(parsed.Hostname(), ".")[0]
lastHyphen := strings.LastIndex(subdomain, "-")
if lastHyphen == -1 {
return subdomain, nil
}
return subdomain[:lastHyphen], nil
}
17 changes: 0 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package config
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"strings"
Expand All @@ -39,19 +38,6 @@ type ProjectConfig struct {
APISecret string `yaml:"api_secret"`
}

func (p *ProjectConfig) URLSafeName() (string, error) {
parsed, err := url.Parse(p.URL)
if err != nil {
return "", errors.New("invalid URL")
}
subdomain := strings.Split(parsed.Hostname(), ".")[0]
lastHyphen := strings.LastIndex(subdomain, "-")
if lastHyphen == -1 {
return subdomain, nil
}
return subdomain[:lastHyphen], nil
}

func LoadDefaultProject() (*ProjectConfig, error) {
conf, err := LoadOrCreate()
if err != nil {
Expand Down Expand Up @@ -80,9 +66,6 @@ func LoadProject(name string) (*ProjectConfig, error) {
if p.Name == name {
return &p, nil
}
if prefix, _ := p.URLSafeName(); prefix == name {
return &p, nil
}
}

return nil, errors.New("project not found")
Expand Down