Skip to content

Commit c936704

Browse files
author
Mrunal Patel
authored
Merge pull request #549 from rhatdan/hooks
Hooks should be passed in as rspec.Hook, not as a string.
2 parents a002fc8 + 625e232 commit c936704

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

cmd/oci-runtime-tool/generate.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
466466
if context.IsSet("hooks-poststart-add") {
467467
postStartHooks := context.StringSlice("hooks-poststart-add")
468468
for _, hook := range postStartHooks {
469-
err := g.AddPostStartHook(hook)
470-
if err != nil {
469+
tmpHook := rspec.Hook{}
470+
if err := json.Unmarshal([]byte(hook), &tmpHook); err != nil {
471+
return err
472+
}
473+
if err := g.AddPostStartHook(tmpHook); err != nil {
471474
return err
472475
}
473476
}
@@ -480,8 +483,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
480483
if context.IsSet("hooks-poststop-add") {
481484
postStopHooks := context.StringSlice("hooks-poststop-add")
482485
for _, hook := range postStopHooks {
483-
err := g.AddPostStopHook(hook)
484-
if err != nil {
486+
tmpHook := rspec.Hook{}
487+
if err := json.Unmarshal([]byte(hook), &tmpHook); err != nil {
488+
return err
489+
}
490+
if err := g.AddPostStopHook(tmpHook); err != nil {
485491
return err
486492
}
487493
}
@@ -494,8 +500,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
494500
if context.IsSet("hooks-prestart-add") {
495501
preStartHooks := context.StringSlice("hooks-prestart-add")
496502
for _, hook := range preStartHooks {
497-
err := g.AddPreStartHook(hook)
498-
if err != nil {
503+
tmpHook := rspec.Hook{}
504+
if err := json.Unmarshal([]byte(hook), &tmpHook); err != nil {
505+
return err
506+
}
507+
if err := g.AddPreStartHook(tmpHook); err != nil {
499508
return err
500509
}
501510
}

generate/generate.go

+12-27
Original file line numberDiff line numberDiff line change
@@ -912,20 +912,15 @@ func (g *Generator) ClearPreStartHooks() {
912912
}
913913

914914
// AddPreStartHook add a prestart hook into g.spec.Hooks.Prestart.
915-
func (g *Generator) AddPreStartHook(hookObject string) error {
915+
func (g *Generator) AddPreStartHook(preStartHook rspec.Hook) error {
916916
g.initSpecHooks()
917-
tmpHook := rspec.Hook{}
918-
err := json.Unmarshal([]byte(hookObject), &tmpHook)
919-
if err != nil {
920-
return err
921-
}
922917
for i, hook := range g.spec.Hooks.Prestart {
923-
if hook.Path == tmpHook.Path {
924-
g.spec.Hooks.Prestart[i] = tmpHook
918+
if hook.Path == preStartHook.Path {
919+
g.spec.Hooks.Prestart[i] = preStartHook
925920
return nil
926921
}
927922
}
928-
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, tmpHook)
923+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, preStartHook)
929924
return nil
930925
}
931926

@@ -938,20 +933,15 @@ func (g *Generator) ClearPostStopHooks() {
938933
}
939934

940935
// AddPostStopHook adds a poststop hook into g.spec.Hooks.Poststop.
941-
func (g *Generator) AddPostStopHook(hookObject string) error {
936+
func (g *Generator) AddPostStopHook(postStopHook rspec.Hook) error {
942937
g.initSpecHooks()
943-
tmpHook := rspec.Hook{}
944-
err := json.Unmarshal([]byte(hookObject), &tmpHook)
945-
if err != nil {
946-
return err
947-
}
948938
for i, hook := range g.spec.Hooks.Poststop {
949-
if hook.Path == tmpHook.Path {
950-
g.spec.Hooks.Poststop[i] = tmpHook
939+
if hook.Path == postStopHook.Path {
940+
g.spec.Hooks.Poststop[i] = postStopHook
951941
return nil
952942
}
953943
}
954-
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, tmpHook)
944+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, postStopHook)
955945
return nil
956946
}
957947

@@ -964,20 +954,15 @@ func (g *Generator) ClearPostStartHooks() {
964954
}
965955

966956
// AddPostStartHook adds a poststart hook into g.spec.Hooks.Poststart.
967-
func (g *Generator) AddPostStartHook(hookObject string) error {
957+
func (g *Generator) AddPostStartHook(postStartHook rspec.Hook) error {
968958
g.initSpecHooks()
969-
tmpHook := rspec.Hook{}
970-
err := json.Unmarshal([]byte(hookObject), &tmpHook)
971-
if err != nil {
972-
return err
973-
}
974959
for i, hook := range g.spec.Hooks.Poststart {
975-
if hook.Path == tmpHook.Path {
976-
g.spec.Hooks.Poststart[i] = tmpHook
960+
if hook.Path == postStartHook.Path {
961+
g.spec.Hooks.Poststart[i] = postStartHook
977962
return nil
978963
}
979964
}
980-
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, tmpHook)
965+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, postStartHook)
981966
return nil
982967
}
983968

0 commit comments

Comments
 (0)