From 4a5663f42444240334c54a53f795e14d1cb6bb0a Mon Sep 17 00:00:00 2001 From: Mohsen Mirzakhani Date: Fri, 11 Oct 2024 20:45:25 +0200 Subject: [PATCH] fix import from postman --- internal/importer/importer.go | 6 ++++++ ui/explorer/explorer.go | 8 ++++++++ ui/pages/environments/controller.go | 7 +++++-- ui/pages/protofiles/controller.go | 9 +++++---- ui/pages/requests/controller.go | 29 +++++++++++++++++++---------- ui/widgets/file_selector.go | 10 +++++----- 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/internal/importer/importer.go b/internal/importer/importer.go index bcc9121..63aa022 100644 --- a/internal/importer/importer.go +++ b/internal/importer/importer.go @@ -2,6 +2,7 @@ package importer import ( "encoding/json" + "errors" "fmt" "os" "strings" @@ -115,6 +116,11 @@ func ImportPostmanCollection(data []byte) error { var collection PostmanCollection if err := json.Unmarshal(data, &collection); err != nil { + // it might be v2 of the collection. inform the user to convert it to v2.1 + if strings.Contains(string(data), "https://schema.getpostman.com/json/collection/v2.0.0/collection.json") { + return errors.New("it seems like you are using v2 of the collection. Please export it as v2.1 and try again") + } + return fmt.Errorf("error parsing JSON: %w", err) } diff --git a/ui/explorer/explorer.go b/ui/explorer/explorer.go index 2917504..5c51b4a 100644 --- a/ui/explorer/explorer.go +++ b/ui/explorer/explorer.go @@ -1,6 +1,7 @@ package explorer import ( + "errors" "fmt" "io" "os" @@ -22,6 +23,8 @@ type Result struct { Data []byte Error error FilePath string + + Declined bool } func NewExplorer(w *app.Window) *Explorer { @@ -39,6 +42,11 @@ func (e *Explorer) ChoseFile(onResult func(r Result), extensions ...string) { file, err := e.expl.ChooseFile(extensions...) if err != nil { + if errors.Is(err, explorer.ErrUserDecline) { + onResult(Result{Error: err, Declined: true}) + return + } + err = fmt.Errorf("failed opening file: %w", err) onResult(Result{Error: err}) return diff --git a/ui/pages/environments/controller.go b/ui/pages/environments/controller.go index d8c9c6c..c527674 100644 --- a/ui/pages/environments/controller.go +++ b/ui/pages/environments/controller.go @@ -1,7 +1,6 @@ package environments import ( - "errors" "fmt" "github.com/chapar-rest/chapar/internal/domain" @@ -64,7 +63,11 @@ func (c *Controller) onNewEnvironment() { func (c *Controller) onImportEnvironment() { c.explorer.ChoseFile(func(result explorer.Result) { - if !errors.Is(result.Error, explorer.ErrUserDecline) { + if result.Declined { + return + } + + if result.Error != nil { c.view.showError(fmt.Errorf("failed to get file %w", result.Error)) return } diff --git a/ui/pages/protofiles/controller.go b/ui/pages/protofiles/controller.go index 11cf4e1..a95e326 100644 --- a/ui/pages/protofiles/controller.go +++ b/ui/pages/protofiles/controller.go @@ -1,7 +1,6 @@ package protofiles import ( - "errors" "path/filepath" "google.golang.org/protobuf/reflect/protoreflect" @@ -79,10 +78,12 @@ func (c *Controller) addPath(path string) { func (c *Controller) onAdd() { c.explorer.ChoseFile(func(result explorer.Result) { + if result.Declined { + return + } + if result.Error != nil { - if !errors.Is(result.Error, explorer.ErrUserDecline) { - c.showError("Failed to open file", result.Error.Error()) - } + c.showError("Failed to open file", result.Error.Error()) return } diff --git a/ui/pages/requests/controller.go b/ui/pages/requests/controller.go index 934dd6f..979a461 100644 --- a/ui/pages/requests/controller.go +++ b/ui/pages/requests/controller.go @@ -1,7 +1,6 @@ package requests import ( - "errors" "fmt" "io" "net/http" @@ -89,12 +88,15 @@ func (c *Controller) LoadData() error { func (c *Controller) onSelectBinaryFile(id string) { c.explorer.ChoseFile(func(result explorer.Result) { + if result.Declined { + return + } + if result.Error != nil { - if !errors.Is(result.Error, explorer.ErrUserDecline) { - c.view.showError(fmt.Errorf("failed to get file, %w", result.Error)) - return - } + c.view.showError(fmt.Errorf("failed to get file, %w", result.Error)) + return } + if result.FilePath == "" { return } @@ -104,12 +106,15 @@ func (c *Controller) onSelectBinaryFile(id string) { func (c *Controller) onFormDataFileSelect(requestId, fieldId string) { c.explorer.ChoseFile(func(result explorer.Result) { + if result.Declined { + return + } + if result.Error != nil { - if !errors.Is(result.Error, explorer.ErrUserDecline) { - c.view.showError(fmt.Errorf("failed to get file, %w", result.Error)) - return - } + c.view.showError(fmt.Errorf("failed to get file, %w", result.Error)) + return } + if result.FilePath == "" { return } @@ -690,7 +695,11 @@ func (c *Controller) onNewRequest(requestType string) { func (c *Controller) onImport() { c.explorer.ChoseFile(func(result explorer.Result) { - if !errors.Is(result.Error, explorer.ErrUserDecline) { + if result.Declined { + return + } + + if result.Error != nil { c.view.showError(fmt.Errorf("failed to get file, %w", result.Error)) return } diff --git a/ui/widgets/file_selector.go b/ui/widgets/file_selector.go index 829f025..6a9871c 100644 --- a/ui/widgets/file_selector.go +++ b/ui/widgets/file_selector.go @@ -1,7 +1,6 @@ package widgets import ( - "errors" "fmt" "gioui.org/layout" @@ -50,13 +49,14 @@ func (b *FileSelector) handleExplorerSelect() { } b.explorer.ChoseFile(func(result explorer.Result) { + if result.Declined { + return + } if result.Error != nil { - if !errors.Is(result.Error, explorer.ErrUserDecline) { - fmt.Printf("failed to get file, %s\n", result.Error) - return - } + fmt.Printf("failed to get file, %s\n", result.Error) return } + if result.FilePath == "" { return }