Skip to content

Commit e7616fe

Browse files
committed
chore: lint & cleanup
1 parent d549f3d commit e7616fe

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

cmd/assistant.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"io"
2626
"os"
27+
"path/filepath"
2728
"strings"
2829

2930
"github.com/AlecAivazis/survey/v2"
@@ -249,7 +250,8 @@ Complex updates can also be done via the Vapi dashboard at https://dashboard.vap
249250
var payloadBytes []byte
250251

251252
if filePath != "" {
252-
b, err := os.ReadFile(filePath)
253+
filePath = filepath.Clean(filePath)
254+
b, err := os.ReadFile(filePath) // #nosec G304 - filePath is user-provided and intentional
253255
if err != nil {
254256
return fmt.Errorf("failed to read --file: %w", err)
255257
}

pkg/auth/auth.go

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -352,99 +352,105 @@ func LoginWithAccountName(accountName string) error {
352352
// First prefer matching by org ID when provided (most reliable)
353353
if authManager.orgID != "" {
354354
for existingName, existing := range cfg.Accounts {
355-
if existing.OrgID == authManager.orgID {
356-
cfg.Accounts[existingName] = config.Account{
357-
APIKey: apiKey,
358-
Organization: authManager.orgName,
359-
OrgID: authManager.orgID,
360-
Environment: cfg.GetEnvironment(),
361-
LoginTime: time.Now().Format(time.RFC3339),
362-
Label: firstNonEmpty(authManager.label, existing.Label, existing.Email),
363-
}
364-
cfg.ActiveAccount = existingName
365-
366-
if err := config.SaveConfig(cfg); err != nil {
367-
return fmt.Errorf("failed to save API key: %w", err)
368-
}
369-
370-
fmt.Printf("\n✅ Re-authenticated existing account '%s'!\n", existingName)
371-
fmt.Printf("Organization: %s\n", authManager.orgName)
372-
if authManager.label != "" {
373-
fmt.Printf("Label: %s\n", authManager.label)
374-
}
375-
if len(cfg.Accounts) > 1 {
376-
fmt.Println("💡 Use 'vapi auth switch' to switch between accounts")
377-
}
378-
return nil
355+
if existing.OrgID != authManager.orgID {
356+
continue
379357
}
358+
cfg.Accounts[existingName] = config.Account{
359+
APIKey: apiKey,
360+
Organization: authManager.orgName,
361+
OrgID: authManager.orgID,
362+
Environment: cfg.GetEnvironment(),
363+
LoginTime: time.Now().Format(time.RFC3339),
364+
Label: firstNonEmpty(authManager.label, existing.Label, existing.Email),
365+
}
366+
cfg.ActiveAccount = existingName
367+
368+
if err := config.SaveConfig(cfg); err != nil {
369+
return fmt.Errorf("failed to save API key: %w", err)
370+
}
371+
372+
fmt.Printf("\n✅ Re-authenticated existing account '%s'!\n", existingName)
373+
fmt.Printf("Organization: %s\n", authManager.orgName)
374+
if authManager.label != "" {
375+
fmt.Printf("Label: %s\n", authManager.label)
376+
}
377+
if len(cfg.Accounts) > 1 {
378+
fmt.Println("💡 Use 'vapi auth switch' to switch between accounts")
379+
}
380+
return nil
380381
}
381382
}
382383

383384
// Then try matching by organization name when provided
384385
if authManager.orgName != "" {
385386
for existingName, existing := range cfg.Accounts {
386-
if strings.EqualFold(existing.Organization, authManager.orgName) {
387-
cfg.Accounts[existingName] = config.Account{
388-
APIKey: apiKey,
389-
Organization: authManager.orgName,
390-
OrgID: authManager.orgID,
391-
Environment: cfg.GetEnvironment(),
392-
LoginTime: time.Now().Format(time.RFC3339),
393-
Label: firstNonEmpty(authManager.label, existing.Label, existing.Email),
394-
}
395-
cfg.ActiveAccount = existingName
396-
397-
if err := config.SaveConfig(cfg); err != nil {
398-
return fmt.Errorf("failed to save API key: %w", err)
399-
}
400-
401-
fmt.Printf("\n✅ Re-authenticated existing account '%s'!\n", existingName)
402-
fmt.Printf("Organization: %s\n", authManager.orgName)
403-
if authManager.label != "" {
404-
fmt.Printf("Label: %s\n", authManager.label)
405-
}
406-
if len(cfg.Accounts) > 1 {
407-
fmt.Println("💡 Use 'vapi auth switch' to switch between accounts")
408-
}
409-
return nil
410-
}
411-
}
412-
}
413-
414-
// Fallback: match by API key if the same key already exists
415-
for existingName, existing := range cfg.Accounts {
416-
if existing.APIKey == apiKey {
417-
// Update existing account's login time and organization (if newly available)
418-
orgName := authManager.orgName
419-
if orgName == "" {
420-
orgName = existing.Organization
387+
if !strings.EqualFold(existing.Organization, authManager.orgName) {
388+
continue
421389
}
422-
423390
cfg.Accounts[existingName] = config.Account{
424391
APIKey: apiKey,
425-
Organization: orgName,
392+
Organization: authManager.orgName,
426393
OrgID: authManager.orgID,
427394
Environment: cfg.GetEnvironment(),
428395
LoginTime: time.Now().Format(time.RFC3339),
429396
Label: firstNonEmpty(authManager.label, existing.Label, existing.Email),
430397
}
431-
// Set as active account
432398
cfg.ActiveAccount = existingName
433399

434400
if err := config.SaveConfig(cfg); err != nil {
435401
return fmt.Errorf("failed to save API key: %w", err)
436402
}
437403

438404
fmt.Printf("\n✅ Re-authenticated existing account '%s'!\n", existingName)
439-
if orgName != "" {
440-
fmt.Printf("Organization: %s\n", orgName)
405+
fmt.Printf("Organization: %s\n", authManager.orgName)
406+
if authManager.label != "" {
407+
fmt.Printf("Label: %s\n", authManager.label)
441408
}
442409
if len(cfg.Accounts) > 1 {
443410
fmt.Println("💡 Use 'vapi auth switch' to switch between accounts")
444411
}
445412
return nil
446413
}
447414
}
415+
416+
// Fallback: match by API key if the same key already exists
417+
for existingName, existing := range cfg.Accounts {
418+
if existing.APIKey != apiKey {
419+
continue
420+
}
421+
// Update existing account's login time and organization (if newly available)
422+
orgName := authManager.orgName
423+
if orgName == "" {
424+
orgName = existing.Organization
425+
}
426+
427+
cfg.Accounts[existingName] = config.Account{
428+
APIKey: apiKey,
429+
Organization: orgName,
430+
OrgID: authManager.orgID,
431+
Environment: cfg.GetEnvironment(),
432+
LoginTime: time.Now().Format(time.RFC3339),
433+
Label: firstNonEmpty(authManager.label, existing.Label, existing.Email),
434+
}
435+
// Set as active account
436+
cfg.ActiveAccount = existingName
437+
438+
if err := config.SaveConfig(cfg); err != nil {
439+
return fmt.Errorf("failed to save API key: %w", err)
440+
}
441+
442+
fmt.Printf("\n✅ Re-authenticated existing account '%s'!\n", existingName)
443+
if orgName != "" {
444+
fmt.Printf("Organization: %s\n", orgName)
445+
}
446+
if authManager.label != "" {
447+
fmt.Printf("Label: %s\n", authManager.label)
448+
}
449+
if len(cfg.Accounts) > 1 {
450+
fmt.Println("💡 Use 'vapi auth switch' to switch between accounts")
451+
}
452+
return nil
453+
}
448454
}
449455

450456
// Generate account name if not provided

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (v *VapiClient) GetConfig() *config.Config {
8080

8181
// DoRawJSON sends a raw JSON request to the Vapi API using the underlying client.
8282
// path should be like "/assistants/<id>". method is e.g. "PATCH".
83-
func (v *VapiClient) DoRawJSON(ctx context.Context, method string, path string, body []byte) (map[string]interface{}, error) {
83+
func (v *VapiClient) DoRawJSON(ctx context.Context, method, path string, body []byte) (map[string]interface{}, error) {
8484
baseURL := strings.TrimRight(v.config.GetAPIBaseURL(), "/")
8585
rel := "/" + strings.TrimLeft(path, "/")
8686
url := baseURL + rel

0 commit comments

Comments
 (0)