From 5e8fed4ca13f136ea41271fc065f391d422de6f2 Mon Sep 17 00:00:00 2001 From: Tobias Fried Date: Mon, 30 Sep 2024 19:33:36 -0600 Subject: [PATCH] feat(lk): refer to projects by slug-name (#432) * chore(lk): unhide app subcommands * feat(lk): use allow referring to project by slug-names * chore(lk): bump version --- cmd/lk/app.go | 1 - cmd/lk/project.go | 6 +++++- cmd/lk/utils.go | 2 +- pkg/config/config.go | 17 +++++++++++++++++ version.go | 2 +- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/lk/app.go b/cmd/lk/app.go index 6078defe..754991e7 100644 --- a/cmd/lk/app.go +++ b/cmd/lk/app.go @@ -40,7 +40,6 @@ var ( project *config.ProjectConfig AppCommands = []*cli.Command{ { - Hidden: true, Name: "app", Category: "Core", Commands: []*cli.Command{ diff --git a/cmd/lk/project.go b/cmd/lk/project.go index d55dd41d..e41c05ae 100644 --- a/cmd/lk/project.go +++ b/cmd/lk/project.go @@ -259,7 +259,11 @@ func listProjects(ctx context.Context, cmd *cli.Command) error { }). Headers("Name", "URL", "API Key", "Default") for _, p := range cliConfig.Projects { - table.Row(p.Name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject)) + name, err := p.URLSafeName() + if err != nil { + return err + } + table.Row(name, p.URL, p.APIKey, fmt.Sprint(p.Name == cliConfig.DefaultProject)) } fmt.Println(table) diff --git a/cmd/lk/utils.go b/cmd/lk/utils.go index 11108e1a..0da9584e 100644 --- a/cmd/lk/utils.go +++ b/cmd/lk/utils.go @@ -228,7 +228,7 @@ func loadProjectDetails(c *cli.Command, opts ...loadOption) (*config.ProjectConf if err != nil { return nil, err } - fmt.Println("Using project:", c.String("project")) + fmt.Println("Using project [" + theme.Focused.Title.Render(c.String("project")) + "]") logDetails(c, pc) return pc, nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index f098af33..de3b6ad4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,6 +17,7 @@ package config import ( "errors" "fmt" + "net/url" "os" "path" "strings" @@ -38,6 +39,19 @@ 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 { @@ -66,6 +80,9 @@ 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") diff --git a/version.go b/version.go index 39388c68..6af0fde6 100644 --- a/version.go +++ b/version.go @@ -15,5 +15,5 @@ package livekitcli const ( - Version = "2.1.5" + Version = "2.2.0" )