Skip to content

Commit 398aac2

Browse files
authored
fix(app): allow bootstrapping of templates without authenticating (#562)
* fix(app): allow bootstrapping of templates without authenticating * chore(cmd): bump version * refactor(pkg): factor out subdomain extraction fn
1 parent 9917df4 commit 398aac2

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

cmd/lk/app.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import (
3131
"github.com/urfave/cli/v3"
3232
)
3333

34+
var (
35+
ErrNoProjectSelected = errors.New("no project selected")
36+
)
37+
3438
var (
3539
template *bootstrap.Template
3640
templateName string
@@ -49,7 +53,6 @@ var (
4953
{
5054
Name: "create",
5155
Usage: "Bootstrap a new application from a template or through guided creation",
52-
Before: requireProject,
5356
Action: setupTemplate,
5457
ArgsUsage: "`APP_NAME`",
5558
Flags: []cli.Flag{
@@ -150,7 +153,7 @@ func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, err
150153
if len(cliConfig.Projects) > 0 {
151154
var options []huh.Option[*config.ProjectConfig]
152155
for _, p := range cliConfig.Projects {
153-
options = append(options, huh.NewOption(p.Name+" ["+p.APIKey+"]", &p))
156+
options = append(options, huh.NewOption(p.Name+" ["+util.ExtractSubdomain(p.URL)+"]", &p))
154157
}
155158
if err = huh.NewForm(
156159
huh.NewGroup(huh.NewSelect[*config.ProjectConfig]().
@@ -179,7 +182,7 @@ func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, err
179182
}
180183
return requireProject(ctx, cmd)
181184
} else {
182-
return nil, errors.New("no project selected")
185+
return nil, ErrNoProjectSelected
183186
}
184187
}
185188
}
@@ -413,12 +416,21 @@ func manageEnv(ctx context.Context, cmd *cli.Command) error {
413416
}
414417

415418
func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addlEnv *map[string]string, exampleFile string) (map[string]string, error) {
416-
env := map[string]string{
417-
"LIVEKIT_API_KEY": project.APIKey,
418-
"LIVEKIT_API_SECRET": project.APISecret,
419-
"LIVEKIT_URL": project.URL,
420-
"NEXT_PUBLIC_LIVEKIT_URL": project.URL,
419+
env := map[string]string{}
420+
if _, err := requireProject(ctx, cmd); err != nil {
421+
if !errors.Is(err, ErrNoProjectSelected) {
422+
return nil, err
423+
}
424+
// if no project is selected, we prompt for all environment variables including LIVEKIT_ ones
425+
} else {
426+
env = map[string]string{
427+
"LIVEKIT_API_KEY": project.APIKey,
428+
"LIVEKIT_API_SECRET": project.APISecret,
429+
"LIVEKIT_URL": project.URL,
430+
"NEXT_PUBLIC_LIVEKIT_URL": project.URL,
431+
}
421432
}
433+
422434
if addlEnv != nil {
423435
for k, v := range *addlEnv {
424436
env[k] = v

pkg/config/config.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"fmt"
2020
"os"
2121
"path"
22-
"regexp"
2322
"strings"
2423

24+
"github.com/livekit/livekit-cli/v2/pkg/util"
2525
"gopkg.in/yaml.v3"
2626
)
2727

@@ -70,17 +70,8 @@ func LoadProjectBySubdomain(subdomain string) (*ProjectConfig, error) {
7070

7171
fmt.Println("Loading project by subdomain", subdomain)
7272

73-
extractSubdomain := func(url string) string {
74-
subdomainPattern := regexp.MustCompile(`^(?:https?|wss?)://([^.]+)\.`)
75-
matches := subdomainPattern.FindStringSubmatch(url)
76-
if len(matches) > 1 {
77-
return matches[1]
78-
}
79-
return ""
80-
}
81-
8273
for _, p := range conf.Projects {
83-
projectSubdomain := extractSubdomain(p.URL)
74+
projectSubdomain := util.ExtractSubdomain(p.URL)
8475
if projectSubdomain == subdomain {
8576
return &p, nil
8677
}

pkg/util/url.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package util
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func ExtractSubdomain(url string) string {
8+
subdomainPattern := regexp.MustCompile(`^(?:https?|wss?)://([^.]+)\.`)
9+
matches := subdomainPattern.FindStringSubmatch(url)
10+
if len(matches) > 1 {
11+
return matches[1]
12+
}
13+
return ""
14+
}

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
package livekitcli
1616

1717
const (
18-
Version = "2.4.5"
18+
Version = "2.4.6"
1919
)

0 commit comments

Comments
 (0)