Skip to content

Commit

Permalink
Fixing missing error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Feb 28, 2024
1 parent 6d8ed9a commit 501e359
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func MakeFromEnv(debug bool) Client {
func (client *Client) Apply(resource *resource.Resource) (string, error) {
url := client.baseUrl + "/" + resource.Kind
resp, err := client.client.R().SetBody(resource.Json).Put(url)
if err != nil {
return "", err
}
if resp.IsError() {
return "", fmt.Errorf("Error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body()))
}
Expand Down
41 changes: 23 additions & 18 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,22 @@ import (
"os"
)

var filePath *string
var filePath *[]string

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "upsert a resource on Conduktor",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
var resources []resource.Resource
var err error

directory, err := isDirectory(*filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if directory {
resources, err = resource.FromFolder(*filePath)
} else {
resources, err = resource.FromFile(*filePath)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
var resources []resource.Resource = make([]resource.Resource, 0)
for _, path := range *filePath {
r, err := resourceForPath(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
resources = append(resources, r...)
}
client := client.MakeFromEnv(*debug)
for _, resource := range resources {
Expand All @@ -46,12 +38,25 @@ var applyCmd = &cobra.Command{
},
}

func resourceForPath(path string) ([]resource.Resource, error) {
directory, err := isDirectory(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if directory {
return resource.FromFolder(path)
} else {
return resource.FromFile(path)
}
}

func init() {
rootCmd.AddCommand(applyCmd)

// Here you will define your flags and configuration settings.
filePath = applyCmd.
PersistentFlags().StringP("file", "f", "", "Specify the file to apply")
PersistentFlags().StringArrayP("file", "f", make([]string, 0, 0), "Specify the file to apply")

applyCmd.MarkPersistentFlagRequired("file")
}
Expand Down

0 comments on commit 501e359

Please sign in to comment.