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

refactor(cmd+pkg): upgrade urfave and reorganize some code #491

Merged
merged 3 commits into from
Dec 31, 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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
of your accepting any such warranty or additional liability.


Copyright 2022 LiveKit, Inc.
Copyright 2021-2024 LiveKit, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
44 changes: 22 additions & 22 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ var (
}
)

func requireProject(ctx context.Context, cmd *cli.Command) error {
func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, error) {
var err error
if project, err = loadProjectDetails(cmd); err != nil {
if err = loadProjectConfig(ctx, cmd); err != nil {
if _, err = loadProjectConfig(ctx, cmd); err != nil {
// something is wrong with config file
return err
return nil, err
}

// choose from existing credentials or authenticate
Expand All @@ -157,33 +157,33 @@ func requireProject(ctx context.Context, cmd *cli.Command) error {
Description("If you'd like to use a different project, run `lk cloud auth` to add credentials").
Options(options...).
Value(&project).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
return nil, err
}
} else {
shouldAuth := true
if err = huh.NewConfirm().
Title("No local projects found. Authenticate one now?").
Inline(true).
Value(&shouldAuth).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
return nil, err
}
if shouldAuth {
initAuth(ctx, cmd)
if err = tryAuthIfNeeded(ctx, cmd); err != nil {
return err
return nil, err
}
return requireProject(ctx, cmd)
} else {
return errors.New("no project selected")
return nil, errors.New("no project selected")
}
}
}

return err
return nil, err
}

func listTemplates(ctx context.Context, cmd *cli.Command) error {
Expand All @@ -196,11 +196,11 @@ func listTemplates(ctx context.Context, cmd *cli.Command) error {
util.PrintJSON(templates)
} else {
const maxDescLength = 64
table := CreateTable().Headers("Template", "Description").BorderRow(true)
table := util.CreateTable().Headers("Template", "Description").BorderRow(true)
for _, t := range templates {
desc := strings.Join(util.WrapToLines(t.Desc, maxDescLength), "\n")
url := theme.Focused.Title.Render(t.URL)
tags := theme.Help.ShortDesc.Render("#" + strings.Join(t.Tags, " #"))
url := util.Theme.Focused.Title.Render(t.URL)
tags := util.Theme.Help.ShortDesc.Render("#" + strings.Join(t.Tags, " #"))
table.Row(
t.Name,
desc+"\n\n"+url+"\n"+tags,
Expand Down Expand Up @@ -251,10 +251,10 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
templateSelect := huh.NewSelect[string]().
Title("Select Template").
Value(&templateURL).
WithTheme(theme)
WithTheme(util.Theme)
var options []huh.Option[string]
for _, t := range templateOptions {
descStyle := theme.Help.ShortDesc
descStyle := util.Theme.Help.ShortDesc
optionText := t.Name + " " + descStyle.Render("#"+strings.Join(t.Tags, " #"))
options = append(options, huh.NewOption(optionText, t.URL))
}
Expand Down Expand Up @@ -293,13 +293,13 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
}
return nil
}).
WithTheme(theme))
WithTheme(util.Theme))
}

if len(preinstallPrompts) > 0 {
group := huh.NewGroup(preinstallPrompts...)
if err := huh.NewForm(group).
WithTheme(theme).
WithTheme(util.Theme).
RunWithContext(ctx); err != nil {
return err
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) err
Action(func() {
stdout, stderr, cmdErr = bootstrap.CloneTemplate(url, tempName)
}).
Style(theme.Focused.Title).
Style(util.Theme.Focused.Title).
Run(); err != nil {
return err
}
Expand Down Expand Up @@ -424,7 +424,7 @@ func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addl
Title("Enter " + key + "?").
Placeholder(oldValue).
Value(&newValue).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil || newValue == "" {
return oldValue, err
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func doInstall(ctx context.Context, task bootstrap.KnownTask, rootPath string, v
if err := spinner.New().
Title("Installing...").
Action(func() { cmdErr = install() }).
Style(theme.Focused.Title).
Style(util.Theme.Focused.Title).
Accessible(true).
Run(); err != nil {
return err
Expand All @@ -512,7 +512,7 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
Title("Select Task").
Options(options...).
Value(&taskName).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
}
Expand All @@ -526,7 +526,7 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
if err := spinner.New().
Title("Running task " + taskName + "...").
Action(func() { cmdErr = task() }).
Style(theme.Focused.Title).
Style(util.Theme.Focused.Title).
Accessible(verbose).
Run(); err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions cmd/lk/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ func NewAuthClient(client *http.Client, baseURL string) *AuthClient {
return a
}

func initAuth(ctx context.Context, cmd *cli.Command) error {
func initAuth(ctx context.Context, cmd *cli.Command) (context.Context, error) {
authClient = *NewAuthClient(&http.Client{}, serverURL)
return nil
return nil, nil
}

func handleAuth(ctx context.Context, cmd *cli.Command) error {
if revoke {
if err := loadProjectConfig(ctx, cmd); err != nil {
if _, err := loadProjectConfig(ctx, cmd); err != nil {
return err
}
token, err := requireToken(ctx, cmd)
Expand Down Expand Up @@ -265,7 +265,7 @@ func requireToken(_ context.Context, cmd *cli.Command) (string, error) {
}

func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
if err := loadProjectConfig(ctx, cmd); err != nil {
if _, err := loadProjectConfig(ctx, cmd); err != nil {
return err
}

Expand All @@ -274,7 +274,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
if err := huh.NewInput().
Title("What is the name of this device?").
Value(&deviceName).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
Action(func() {
ak, pollErr = pollClaim(ctx, cmd)
}).
Style(theme.Focused.Title).
Style(util.Theme.Focused.Title).
Run(); err != nil {
return err
}
Expand All @@ -319,7 +319,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
Title("Make this project default?").
Value(&isDefault).
Inline(true).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
}
Expand All @@ -339,7 +339,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
}
return nil
}).
WithTheme(theme).
WithTheme(util.Theme).
Run(); err != nil {
return err
}
Expand Down
22 changes: 18 additions & 4 deletions cmd/lk/dispatch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand Down Expand Up @@ -71,14 +85,14 @@ var (
dispatchClient *lksdk.AgentDispatchClient
)

func createDispatchClient(ctx context.Context, cmd *cli.Command) error {
func createDispatchClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
pc, err := loadProjectDetails(cmd)
if err != nil {
return err
return nil, err
}

dispatchClient = lksdk.NewAgentDispatchServiceClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
return nil
return nil, nil
}

func getAgentDispatch(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -128,7 +142,7 @@ func listDispatchAndPrint(cmd *cli.Command, req *livekit.ListAgentDispatchReques
if cmd.Bool("json") {
util.PrintJSON(res)
} else {
table := CreateTable().
table := util.CreateTable().
Headers("DispatchID", "Room", "AgentName", "Metadata")
for _, item := range res.AgentDispatches {
if item == nil {
Expand Down
12 changes: 6 additions & 6 deletions cmd/lk/egress.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 LiveKit, Inc.
// Copyright 2022-2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -381,14 +381,14 @@ var (
egressClient *lksdk.EgressClient
)

func createEgressClient(ctx context.Context, c *cli.Command) error {
pc, err := loadProjectDetails(c)
func createEgressClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
pc, err := loadProjectDetails(cmd)
if err != nil {
return err
return nil, err
}

egressClient = lksdk.NewEgressClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
return nil
return nil, nil
}

func handleEgressStart(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -600,7 +600,7 @@ func listEgress(ctx context.Context, cmd *cli.Command) error {
if cmd.Bool("json") {
util.PrintJSON(items)
} else {
table := CreateTable().
table := util.CreateTable().
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
for _, item := range items {
var startedAt string
Expand Down
10 changes: 5 additions & 5 deletions cmd/lk/ingress.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 LiveKit, Inc.
// Copyright 2022-2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -161,14 +161,14 @@ var (
ingressClient *lksdk.IngressClient
)

func createIngressClient(ctx context.Context, cmd *cli.Command) error {
func createIngressClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
pc, err := loadProjectDetails(cmd)
if err != nil {
return err
return nil, err
}

ingressClient = lksdk.NewIngressClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
return nil
return nil, nil
}

func createIngress(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -224,7 +224,7 @@ func listIngress(ctx context.Context, cmd *cli.Command) error {
if cmd.Bool("verbose") || cmd.Bool("json") {
util.PrintJSON(res)
} else {
table := CreateTable().
table := util.CreateTable().
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
for _, item := range res.Items {
if item == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/lk/join.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 LiveKit, Inc.
// Copyright 2021-2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion cmd/lk/join_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 LiveKit, Inc.
// Copyright 2021-2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion cmd/lk/loadtest.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 LiveKit, Inc.
// Copyright 2021-2024 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading
Loading