Skip to content

Commit

Permalink
fix: improve wmill go client
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Nov 23, 2023
1 parent b4bbb79 commit cfd3da4
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions go-client/windmill.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,17 @@ func GetResource(path string) (interface{}, error) {

func SetResource(path string, value interface{}) error {
client, err := GetClient()
if err != nil {
return err
}
res, err := client.Client.UpdateResourceValueWithResponse(
context.Background(),
client.Workspace, path,
api.UpdateResourceValueJSONRequestBody{Value: &value})
res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{
UpdateIfExists: newBool(true),
}, api.CreateResource{Value: &value, Path: path})

if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}

return nil
}

Expand All @@ -93,20 +91,34 @@ func SetVariable(path string, value string) error {
f := false
res, err := client.Client.UpdateVariableWithResponse(context.Background(), client.Workspace, path, &api.UpdateVariableParams{AlreadyEncrypted: &f}, api.EditVariable{Value: &value})
if err != nil {
return err
f = true
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
f = true
}
if f == true {
res, err := client.Client.CreateVariableWithResponse(context.Background(), client.Workspace, &api.CreateVariableParams{},
api.CreateVariableJSONRequestBody{
Path: path,
Value: value,
})

if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
}
return nil
}

func GetStatePath() string {
value := os.Getenv("WM_STATE_PATH_NEW")
if len(value) == 0 {
return os.Getenv("WM_STATE_PATH")
}
return value
return os.Getenv("WM_STATE_PATH")
}
return value
}

func GetState() (interface{}, error) {
Expand All @@ -116,20 +128,7 @@ func GetState() (interface{}, error) {
func SetState(state interface{}) error {
err := SetResource(GetStatePath(), state)
if err != nil {
client, err := GetClient()
if err != nil {
return err
}
res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{
UpdateIfExists: newBool(true),
}, api.CreateResource{Value: &state})
if err != nil {
return err
}
if res.StatusCode()/100 != 2 {
return errors.New(string(res.Body))
}
return nil
return err
}
return nil
}

0 comments on commit cfd3da4

Please sign in to comment.