Skip to content

Commit 0c40021

Browse files
authored
refactor(cmd+pkg): upgrade urfave and reorganize some code (#491)
1 parent dbd9126 commit 0c40021

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+373
-213
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
of your accepting any such warranty or additional liability.
175175

176176

177-
Copyright 2022 LiveKit, Inc.
177+
Copyright 2021-2024 LiveKit, Inc.
178178

179179
Licensed under the Apache License, Version 2.0 (the "License");
180180
you may not use this file except in compliance with the License.

cmd/lk/app.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ var (
138138
}
139139
)
140140

141-
func requireProject(ctx context.Context, cmd *cli.Command) error {
141+
func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, error) {
142142
var err error
143143
if project, err = loadProjectDetails(cmd); err != nil {
144-
if err = loadProjectConfig(ctx, cmd); err != nil {
144+
if _, err = loadProjectConfig(ctx, cmd); err != nil {
145145
// something is wrong with config file
146-
return err
146+
return nil, err
147147
}
148148

149149
// choose from existing credentials or authenticate
@@ -157,33 +157,33 @@ func requireProject(ctx context.Context, cmd *cli.Command) error {
157157
Description("If you'd like to use a different project, run `lk cloud auth` to add credentials").
158158
Options(options...).
159159
Value(&project).
160-
WithTheme(theme).
160+
WithTheme(util.Theme).
161161
Run(); err != nil {
162-
return err
162+
return nil, err
163163
}
164164
} else {
165165
shouldAuth := true
166166
if err = huh.NewConfirm().
167167
Title("No local projects found. Authenticate one now?").
168168
Inline(true).
169169
Value(&shouldAuth).
170-
WithTheme(theme).
170+
WithTheme(util.Theme).
171171
Run(); err != nil {
172-
return err
172+
return nil, err
173173
}
174174
if shouldAuth {
175175
initAuth(ctx, cmd)
176176
if err = tryAuthIfNeeded(ctx, cmd); err != nil {
177-
return err
177+
return nil, err
178178
}
179179
return requireProject(ctx, cmd)
180180
} else {
181-
return errors.New("no project selected")
181+
return nil, errors.New("no project selected")
182182
}
183183
}
184184
}
185185

186-
return err
186+
return nil, err
187187
}
188188

189189
func listTemplates(ctx context.Context, cmd *cli.Command) error {
@@ -196,11 +196,11 @@ func listTemplates(ctx context.Context, cmd *cli.Command) error {
196196
util.PrintJSON(templates)
197197
} else {
198198
const maxDescLength = 64
199-
table := CreateTable().Headers("Template", "Description").BorderRow(true)
199+
table := util.CreateTable().Headers("Template", "Description").BorderRow(true)
200200
for _, t := range templates {
201201
desc := strings.Join(util.WrapToLines(t.Desc, maxDescLength), "\n")
202-
url := theme.Focused.Title.Render(t.URL)
203-
tags := theme.Help.ShortDesc.Render("#" + strings.Join(t.Tags, " #"))
202+
url := util.Theme.Focused.Title.Render(t.URL)
203+
tags := util.Theme.Help.ShortDesc.Render("#" + strings.Join(t.Tags, " #"))
204204
table.Row(
205205
t.Name,
206206
desc+"\n\n"+url+"\n"+tags,
@@ -251,10 +251,10 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
251251
templateSelect := huh.NewSelect[string]().
252252
Title("Select Template").
253253
Value(&templateURL).
254-
WithTheme(theme)
254+
WithTheme(util.Theme)
255255
var options []huh.Option[string]
256256
for _, t := range templateOptions {
257-
descStyle := theme.Help.ShortDesc
257+
descStyle := util.Theme.Help.ShortDesc
258258
optionText := t.Name + " " + descStyle.Render("#"+strings.Join(t.Tags, " #"))
259259
options = append(options, huh.NewOption(optionText, t.URL))
260260
}
@@ -293,13 +293,13 @@ func setupTemplate(ctx context.Context, cmd *cli.Command) error {
293293
}
294294
return nil
295295
}).
296-
WithTheme(theme))
296+
WithTheme(util.Theme))
297297
}
298298

299299
if len(preinstallPrompts) > 0 {
300300
group := huh.NewGroup(preinstallPrompts...)
301301
if err := huh.NewForm(group).
302-
WithTheme(theme).
302+
WithTheme(util.Theme).
303303
RunWithContext(ctx); err != nil {
304304
return err
305305
}
@@ -364,7 +364,7 @@ func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) err
364364
Action(func() {
365365
stdout, stderr, cmdErr = bootstrap.CloneTemplate(url, tempName)
366366
}).
367-
Style(theme.Focused.Title).
367+
Style(util.Theme.Focused.Title).
368368
Run(); err != nil {
369369
return err
370370
}
@@ -424,7 +424,7 @@ func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string, addl
424424
Title("Enter " + key + "?").
425425
Placeholder(oldValue).
426426
Value(&newValue).
427-
WithTheme(theme).
427+
WithTheme(util.Theme).
428428
Run(); err != nil || newValue == "" {
429429
return oldValue, err
430430
}
@@ -485,7 +485,7 @@ func doInstall(ctx context.Context, task bootstrap.KnownTask, rootPath string, v
485485
if err := spinner.New().
486486
Title("Installing...").
487487
Action(func() { cmdErr = install() }).
488-
Style(theme.Focused.Title).
488+
Style(util.Theme.Focused.Title).
489489
Accessible(true).
490490
Run(); err != nil {
491491
return err
@@ -512,7 +512,7 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
512512
Title("Select Task").
513513
Options(options...).
514514
Value(&taskName).
515-
WithTheme(theme).
515+
WithTheme(util.Theme).
516516
Run(); err != nil {
517517
return err
518518
}
@@ -526,7 +526,7 @@ func runTask(ctx context.Context, cmd *cli.Command) error {
526526
if err := spinner.New().
527527
Title("Running task " + taskName + "...").
528528
Action(func() { cmdErr = task() }).
529-
Style(theme.Focused.Title).
529+
Style(util.Theme.Focused.Title).
530530
Accessible(verbose).
531531
Run(); err != nil {
532532
return err

cmd/lk/cloud.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ func NewAuthClient(client *http.Client, baseURL string) *AuthClient {
220220
return a
221221
}
222222

223-
func initAuth(ctx context.Context, cmd *cli.Command) error {
223+
func initAuth(ctx context.Context, cmd *cli.Command) (context.Context, error) {
224224
authClient = *NewAuthClient(&http.Client{}, serverURL)
225-
return nil
225+
return nil, nil
226226
}
227227

228228
func handleAuth(ctx context.Context, cmd *cli.Command) error {
229229
if revoke {
230-
if err := loadProjectConfig(ctx, cmd); err != nil {
230+
if _, err := loadProjectConfig(ctx, cmd); err != nil {
231231
return err
232232
}
233233
token, err := requireToken(ctx, cmd)
@@ -265,7 +265,7 @@ func requireToken(_ context.Context, cmd *cli.Command) (string, error) {
265265
}
266266

267267
func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
268-
if err := loadProjectConfig(ctx, cmd); err != nil {
268+
if _, err := loadProjectConfig(ctx, cmd); err != nil {
269269
return err
270270
}
271271

@@ -274,7 +274,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
274274
if err := huh.NewInput().
275275
Title("What is the name of this device?").
276276
Value(&deviceName).
277-
WithTheme(theme).
277+
WithTheme(util.Theme).
278278
Run(); err != nil {
279279
return err
280280
}
@@ -303,7 +303,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
303303
Action(func() {
304304
ak, pollErr = pollClaim(ctx, cmd)
305305
}).
306-
Style(theme.Focused.Title).
306+
Style(util.Theme.Focused.Title).
307307
Run(); err != nil {
308308
return err
309309
}
@@ -319,7 +319,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
319319
Title("Make this project default?").
320320
Value(&isDefault).
321321
Inline(true).
322-
WithTheme(theme).
322+
WithTheme(util.Theme).
323323
Run(); err != nil {
324324
return err
325325
}
@@ -339,7 +339,7 @@ func tryAuthIfNeeded(ctx context.Context, cmd *cli.Command) error {
339339
}
340340
return nil
341341
}).
342-
WithTheme(theme).
342+
WithTheme(util.Theme).
343343
Run(); err != nil {
344344
return err
345345
}

cmd/lk/dispatch.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2024 LiveKit, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package main
216

317
import (
@@ -71,14 +85,14 @@ var (
7185
dispatchClient *lksdk.AgentDispatchClient
7286
)
7387

74-
func createDispatchClient(ctx context.Context, cmd *cli.Command) error {
88+
func createDispatchClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
7589
pc, err := loadProjectDetails(cmd)
7690
if err != nil {
77-
return err
91+
return nil, err
7892
}
7993

8094
dispatchClient = lksdk.NewAgentDispatchServiceClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
81-
return nil
95+
return nil, nil
8296
}
8397

8498
func getAgentDispatch(ctx context.Context, cmd *cli.Command) error {
@@ -128,7 +142,7 @@ func listDispatchAndPrint(cmd *cli.Command, req *livekit.ListAgentDispatchReques
128142
if cmd.Bool("json") {
129143
util.PrintJSON(res)
130144
} else {
131-
table := CreateTable().
145+
table := util.CreateTable().
132146
Headers("DispatchID", "Room", "AgentName", "Metadata")
133147
for _, item := range res.AgentDispatches {
134148
if item == nil {

cmd/lk/egress.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 LiveKit, Inc.
1+
// Copyright 2022-2024 LiveKit, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -381,14 +381,14 @@ var (
381381
egressClient *lksdk.EgressClient
382382
)
383383

384-
func createEgressClient(ctx context.Context, c *cli.Command) error {
385-
pc, err := loadProjectDetails(c)
384+
func createEgressClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
385+
pc, err := loadProjectDetails(cmd)
386386
if err != nil {
387-
return err
387+
return nil, err
388388
}
389389

390390
egressClient = lksdk.NewEgressClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
391-
return nil
391+
return nil, nil
392392
}
393393

394394
func handleEgressStart(ctx context.Context, cmd *cli.Command) error {
@@ -600,7 +600,7 @@ func listEgress(ctx context.Context, cmd *cli.Command) error {
600600
if cmd.Bool("json") {
601601
util.PrintJSON(items)
602602
} else {
603-
table := CreateTable().
603+
table := util.CreateTable().
604604
Headers("EgressID", "Status", "Type", "Source", "Started At", "Error")
605605
for _, item := range items {
606606
var startedAt string

cmd/lk/ingress.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 LiveKit, Inc.
1+
// Copyright 2022-2024 LiveKit, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -161,14 +161,14 @@ var (
161161
ingressClient *lksdk.IngressClient
162162
)
163163

164-
func createIngressClient(ctx context.Context, cmd *cli.Command) error {
164+
func createIngressClient(ctx context.Context, cmd *cli.Command) (context.Context, error) {
165165
pc, err := loadProjectDetails(cmd)
166166
if err != nil {
167-
return err
167+
return nil, err
168168
}
169169

170170
ingressClient = lksdk.NewIngressClient(pc.URL, pc.APIKey, pc.APISecret, withDefaultClientOpts(pc)...)
171-
return nil
171+
return nil, nil
172172
}
173173

174174
func createIngress(ctx context.Context, cmd *cli.Command) error {
@@ -224,7 +224,7 @@ func listIngress(ctx context.Context, cmd *cli.Command) error {
224224
if cmd.Bool("verbose") || cmd.Bool("json") {
225225
util.PrintJSON(res)
226226
} else {
227-
table := CreateTable().
227+
table := util.CreateTable().
228228
Headers("IngressID", "Name", "Room", "StreamKey", "URL", "Status", "Error")
229229
for _, item := range res.Items {
230230
if item == nil {

cmd/lk/join.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 LiveKit, Inc.
1+
// Copyright 2021-2024 LiveKit, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

cmd/lk/join_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 LiveKit, Inc.
1+
// Copyright 2021-2023 LiveKit, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

cmd/lk/loadtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 LiveKit, Inc.
1+
// Copyright 2021-2024 LiveKit, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)