Skip to content

Commit

Permalink
Merge pull request #120 from trickest/fix/standardize-exit-codes
Browse files Browse the repository at this point in the history
Standardize the exit codes of API errors and the files/library sub-commands
  • Loading branch information
mhmdiaa authored Jul 2, 2024
2 parents 515a096 + 1b5f45e commit 7a92add
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 7 deletions.
12 changes: 6 additions & 6 deletions client/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *Request) newRequest(url string) (*http.Request, error) {
func ProcessUnexpectedResponse(resp *Response) {
if resp == nil || resp.response == nil {
fmt.Println("Response is nil")
os.Exit(0)
os.Exit(1)
}

//fmt.Println(resp.response.Request.Method + " " + resp.response.Request.URL.Path + " " + strconv.Itoa(resp.response.StatusCode))
Expand All @@ -160,26 +160,26 @@ func ProcessUnexpectedResponse(resp *Response) {

if resp.response.StatusCode >= http.StatusInternalServerError {
fmt.Println("Sorry, something went wrong!")
os.Exit(0)
os.Exit(1)
}

if resp.response.StatusCode == http.StatusUnauthorized {
fmt.Println("Error: Unauthorized to perform this action.\nPlease, make sure that your token is correct and that you have access to this resource.")
os.Exit(0)
os.Exit(1)
}

var response map[string]interface{}
err := json.Unmarshal(resp.body, &response)
if err != nil {
fmt.Println("Sorry, something went wrong!")
os.Exit(0)
os.Exit(1)
}

if details, exists := response["details"]; exists {
fmt.Println(details)
os.Exit(0)
os.Exit(1)
} else {
fmt.Println("Sorry, something went wrong!")
os.Exit(0)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions cmd/files/filesCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var filesCreateCmd = &cobra.Command{
err := createFile(filePath)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("Uploaded %s successfully\n", filePath)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/files/filesDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package files
import (
"fmt"
"net/http"
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -20,6 +21,7 @@ var filesDeleteCmd = &cobra.Command{
err := deleteFile(fileName)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("Deleted %s successfully\n", fileName)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/files/filesGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -27,6 +28,7 @@ var filesGetCmd = &cobra.Command{
err := getFile(fileName, outputDir, partialNameMatch)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("Retrieved matches for %s successfully\n", fileName)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/files/filesList.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package files
import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/trickest/trickest-cli/types"
Expand All @@ -23,6 +24,7 @@ var filesListCmd = &cobra.Command{
files, err := getMetadata(searchQuery)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
} else {
printFiles(files, jsonOutput)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/library/libraryListModules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math"
"os"

"github.com/spf13/cobra"
"github.com/trickest/trickest-cli/cmd/list"
Expand All @@ -21,7 +22,8 @@ var libraryListModulesCmd = &cobra.Command{
if len(modules) > 0 {
printModules(modules, jsonOutput)
} else {
fmt.Println("Couldn't find any workflow in the library!")
fmt.Println("Couldn't find any module in the library!")
os.Exit(1)
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/library/libraryListTools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package library
import (
"fmt"
"math"
"os"

"github.com/spf13/cobra"
"github.com/trickest/trickest-cli/cmd/list"
Expand All @@ -19,6 +20,7 @@ var libraryListToolsCmd = &cobra.Command{
PrintTools(tools, jsonOutput)
} else {
fmt.Println("Couldn't find any tool in the library!")
os.Exit(1)
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/library/libraryListWorkflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package library
import (
"encoding/json"
"fmt"
"os"

"github.com/google/uuid"
"github.com/spf13/cobra"
Expand All @@ -22,6 +23,7 @@ var libraryListWorkflowsCmd = &cobra.Command{
printWorkflows(workflows, jsonOutput)
} else {
fmt.Println("Couldn't find any workflow in the library!")
os.Exit(1)
}
},
}
Expand Down

0 comments on commit 7a92add

Please sign in to comment.