github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/hooks/0.1.0/hook.go (about)

     1  // Package hook is the 0.1.0 hook configuration structure.
     2  package hook
     3  
     4  import (
     5  	"encoding/json"
     6  	"errors"
     7  	"strings"
     8  
     9  	"github.com/containers/libpod/pkg/hooks"
    10  	current "github.com/containers/libpod/pkg/hooks/1.0.0"
    11  	rspec "github.com/opencontainers/runtime-spec/specs-go"
    12  )
    13  
    14  // Version is the hook configuration version defined in this package.
    15  const Version = "0.1.0"
    16  
    17  // Hook is the hook configuration structure.
    18  type Hook struct {
    19  	Hook      *string  `json:"hook"`
    20  	Arguments []string `json:"arguments,omitempty"`
    21  
    22  	// https://github.com/cri-o/cri-o/pull/1235
    23  	Stages []string `json:"stages"`
    24  	Stage  []string `json:"stage"`
    25  
    26  	Cmds []string `json:"cmds,omitempty"`
    27  	Cmd  []string `json:"cmd,omitempty"`
    28  
    29  	Annotations []string `json:"annotations,omitempty"`
    30  	Annotation  []string `json:"annotation,omitempty"`
    31  
    32  	HasBindMounts *bool `json:"hasbindmounts,omitempty"`
    33  }
    34  
    35  func read(content []byte) (hook *current.Hook, err error) {
    36  	var raw Hook
    37  	if err = json.Unmarshal(content, &raw); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	if raw.Hook == nil {
    42  		return nil, errors.New("missing required property: hook")
    43  	}
    44  
    45  	if raw.Stages == nil {
    46  		raw.Stages = raw.Stage
    47  	} else if raw.Stage != nil {
    48  		return nil, errors.New("cannot set both 'stage' and 'stages'")
    49  	}
    50  	if raw.Stages == nil {
    51  		return nil, errors.New("missing required property: stages")
    52  	}
    53  
    54  	if raw.Cmds == nil {
    55  		raw.Cmds = raw.Cmd
    56  	} else if raw.Cmd != nil {
    57  		return nil, errors.New("cannot set both 'cmd' and 'cmds'")
    58  	}
    59  
    60  	if raw.Annotations == nil {
    61  		raw.Annotations = raw.Annotation
    62  	} else if raw.Annotation != nil {
    63  		return nil, errors.New("cannot set both 'annotation' and 'annotations'")
    64  	}
    65  
    66  	hook = &current.Hook{
    67  		Version: current.Version,
    68  		Hook: rspec.Hook{
    69  			Path: *raw.Hook,
    70  		},
    71  		When: current.When{
    72  			Commands:      raw.Cmds,
    73  			HasBindMounts: raw.HasBindMounts,
    74  			Or:            true,
    75  		},
    76  		Stages: raw.Stages,
    77  	}
    78  	if raw.Arguments != nil {
    79  		hook.Hook.Args = append([]string{*raw.Hook}, raw.Arguments...)
    80  	}
    81  	if raw.Annotations != nil {
    82  		hook.When.Annotations = map[string]string{
    83  			".*": strings.Join(raw.Annotations, "|"),
    84  		}
    85  	}
    86  
    87  	return hook, nil
    88  }
    89  
    90  func init() {
    91  	hooks.Readers[""] = read
    92  	hooks.Readers[Version] = read
    93  }