Skip to content

Commit 9da7d7a

Browse files
committed
fix: resolve all linting issues
1 parent be69286 commit 9da7d7a

File tree

4 files changed

+62
-49
lines changed

4 files changed

+62
-49
lines changed

cmd/chat.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,18 @@ var deleteChatCmd = &cobra.Command{
141141
chatID := args[0]
142142

143143
// Require explicit confirmation for destructive actions
144-
var confirmDelete bool
145-
prompt := &survey.Confirm{
146-
Message: fmt.Sprintf("Are you sure you want to delete chat conversation %s?", chatID),
147-
Default: false,
148-
}
149-
150-
if err := survey.AskOne(prompt, &confirmDelete); err != nil {
151-
return fmt.Errorf("deletion canceled: %w", err)
144+
confirmed, err := confirmDeletion("chat conversation", chatID)
145+
if err != nil {
146+
return err
152147
}
153-
154-
if !confirmDelete {
155-
fmt.Println("Deletion canceled.")
148+
if !confirmed {
156149
return nil
157150
}
158151

159152
fmt.Printf("🗑️ Deleting chat conversation with ID: %s\n", chatID)
160153

161154
// Execute deletion via API
162-
_, err := vapiClient.GetClient().Chats.Delete(ctx, chatID)
155+
_, err = vapiClient.GetClient().Chats.Delete(ctx, chatID)
163156
if err != nil {
164157
return fmt.Errorf("failed to delete chat: %w", err)
165158
}
@@ -196,6 +189,26 @@ using the Vapi SDKs or through the dashboard interface.`,
196189
},
197190
}
198191

192+
// confirmDeletion prompts the user for confirmation before destructive actions
193+
func confirmDeletion(itemType, itemID string) (bool, error) {
194+
var confirmDelete bool
195+
prompt := &survey.Confirm{
196+
Message: fmt.Sprintf("Are you sure you want to delete %s %s?", itemType, itemID),
197+
Default: false,
198+
}
199+
200+
if err := survey.AskOne(prompt, &confirmDelete); err != nil {
201+
return false, fmt.Errorf("deletion canceled: %w", err)
202+
}
203+
204+
if !confirmDelete {
205+
fmt.Println("Deletion canceled.")
206+
return false, nil
207+
}
208+
209+
return true, nil
210+
}
211+
199212
func init() {
200213
rootCmd.AddCommand(chatCmd)
201214
chatCmd.AddCommand(listChatCmd)

cmd/listen.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ func startWebhookListener(forwardURL string, port int, skipVerify bool) error {
118118
// Create HTTP server
119119
mux := http.NewServeMux()
120120
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
121-
handleWebhook(w, r, forwardURL, skipVerify, sessionID)
121+
handleWebhook(w, r, forwardURL, sessionID)
122122
})
123123

124124
server := &http.Server{
125-
Addr: ":" + strconv.Itoa(port),
126-
Handler: mux,
125+
Addr: ":" + strconv.Itoa(port),
126+
Handler: mux,
127+
ReadHeaderTimeout: 10 * time.Second,
127128
}
128129

129130
// Handle graceful shutdown
@@ -158,7 +159,7 @@ func startWebhookListener(forwardURL string, port int, skipVerify bool) error {
158159
}
159160

160161
// handleWebhook processes incoming webhook requests and forwards them
161-
func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, skipVerify bool, sessionID string) {
162+
func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL, sessionID string) {
162163
timestamp := time.Now().Format("15:04:05")
163164

164165
// Create styles
@@ -174,7 +175,11 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
174175
http.Error(w, "Failed to read request body", http.StatusBadRequest)
175176
return
176177
}
177-
defer r.Body.Close()
178+
defer func() {
179+
if err := r.Body.Close(); err != nil {
180+
fmt.Printf("[%s] %s Failed to close request body: %v\n", timestamp, errorStyle.Render("ERROR"), err)
181+
}
182+
}()
178183

179184
// Try to parse as JSON to get event type if possible
180185
var webhook map[string]interface{}
@@ -227,7 +232,11 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
227232
http.Error(w, "Failed to forward request", http.StatusBadGateway)
228233
return
229234
}
230-
defer resp.Body.Close()
235+
defer func() {
236+
if err := resp.Body.Close(); err != nil {
237+
fmt.Printf("[%s] %s Failed to close response body: %v\n", timestamp, errorStyle.Render("ERROR"), err)
238+
}
239+
}()
231240

232241
// Read response from target server
233242
respBody, err := io.ReadAll(resp.Body)
@@ -259,7 +268,9 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
259268

260269
// Send response back to Vapi
261270
w.WriteHeader(resp.StatusCode)
262-
w.Write(respBody)
271+
if _, err := w.Write(respBody); err != nil {
272+
fmt.Printf("[%s] %s Failed to write response: %v\n", timestamp, errorStyle.Render("ERROR"), err)
273+
}
263274

264275
// Print webhook details if it's a JSON payload
265276
if strings.Contains(r.Header.Get("Content-Type"), "application/json") && len(body) > 0 {
@@ -273,9 +284,12 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
273284

274285
// generateSessionID creates a unique identifier for this listening session
275286
func generateSessionID() string {
276-
bytes := make([]byte, 4)
277-
rand.Read(bytes)
278-
return hex.EncodeToString(bytes)
287+
b := make([]byte, 4)
288+
if _, err := rand.Read(b); err != nil {
289+
// Fallback to timestamp-based ID if random fails
290+
return fmt.Sprintf("%d", time.Now().Unix())
291+
}
292+
return hex.EncodeToString(b)
279293
}
280294

281295
func init() {
@@ -287,5 +301,7 @@ func init() {
287301
listenCmd.Flags().BoolVar(&skipVerify, "skip-verify", false, "Skip TLS certificate verification when forwarding")
288302

289303
// Mark required flags
290-
listenCmd.MarkFlagRequired("forward-to")
304+
if err := listenCmd.MarkFlagRequired("forward-to"); err != nil {
305+
panic(fmt.Sprintf("Failed to mark flag as required: %v", err))
306+
}
291307
}

cmd/phone.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"strings"
2525

26-
"github.com/AlecAivazis/survey/v2"
2726
"github.com/spf13/cobra"
2827

2928
"github.com/VapiAI/cli/pkg/output"
@@ -163,25 +162,18 @@ var deletePhoneCmd = &cobra.Command{
163162
phoneNumberID := args[0]
164163

165164
// Require explicit confirmation for destructive actions
166-
var confirmDelete bool
167-
prompt := &survey.Confirm{
168-
Message: fmt.Sprintf("Are you sure you want to release phone number %s?", phoneNumberID),
169-
Default: false,
170-
}
171-
172-
if err := survey.AskOne(prompt, &confirmDelete); err != nil {
173-
return fmt.Errorf("deletion canceled: %w", err)
165+
confirmed, err := confirmDeletion("phone number", phoneNumberID)
166+
if err != nil {
167+
return err
174168
}
175-
176-
if !confirmDelete {
177-
fmt.Println("Release canceled.")
169+
if !confirmed {
178170
return nil
179171
}
180172

181173
fmt.Printf("🗑️ Releasing phone number with ID: %s\n", phoneNumberID)
182174

183175
// Execute deletion via API
184-
_, err := vapiClient.GetClient().PhoneNumbers.Delete(ctx, phoneNumberID)
176+
_, err = vapiClient.GetClient().PhoneNumbers.Delete(ctx, phoneNumberID)
185177
if err != nil {
186178
return fmt.Errorf("failed to release phone number: %w", err)
187179
}

cmd/tool.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"strings"
2525

26-
"github.com/AlecAivazis/survey/v2"
2726
"github.com/spf13/cobra"
2827

2928
"github.com/VapiAI/cli/pkg/output"
@@ -183,25 +182,18 @@ var deleteToolCmd = &cobra.Command{
183182
toolID := args[0]
184183

185184
// Require explicit confirmation for destructive actions
186-
var confirmDelete bool
187-
prompt := &survey.Confirm{
188-
Message: fmt.Sprintf("Are you sure you want to delete tool %s? This will remove it from all assistants.", toolID),
189-
Default: false,
190-
}
191-
192-
if err := survey.AskOne(prompt, &confirmDelete); err != nil {
193-
return fmt.Errorf("deletion canceled: %w", err)
185+
confirmed, err := confirmDeletion("tool", fmt.Sprintf("%s (this will remove it from all assistants)", toolID))
186+
if err != nil {
187+
return err
194188
}
195-
196-
if !confirmDelete {
197-
fmt.Println("Deletion canceled.")
189+
if !confirmed {
198190
return nil
199191
}
200192

201193
fmt.Printf("🗑️ Deleting tool with ID: %s\n", toolID)
202194

203195
// Execute deletion via API
204-
_, err := vapiClient.GetClient().Tools.Delete(ctx, toolID)
196+
_, err = vapiClient.GetClient().Tools.Delete(ctx, toolID)
205197
if err != nil {
206198
return fmt.Errorf("failed to delete tool: %w", err)
207199
}

0 commit comments

Comments
 (0)