github.com/zoumo/helm@v2.5.0+incompatible/pkg/tiller/hook_sorter.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tiller
    18  
    19  import (
    20  	"sort"
    21  
    22  	"k8s.io/helm/pkg/proto/hapi/release"
    23  )
    24  
    25  // sortByHookWeight does an in-place sort of hooks by their supplied weight.
    26  func sortByHookWeight(hooks []*release.Hook) []*release.Hook {
    27  	hs := newHookWeightSorter(hooks)
    28  	sort.Sort(hs)
    29  	return hs.hooks
    30  }
    31  
    32  type hookWeightSorter struct {
    33  	hooks []*release.Hook
    34  }
    35  
    36  func newHookWeightSorter(h []*release.Hook) *hookWeightSorter {
    37  	return &hookWeightSorter{
    38  		hooks: h,
    39  	}
    40  }
    41  
    42  func (hs *hookWeightSorter) Len() int { return len(hs.hooks) }
    43  
    44  func (hs *hookWeightSorter) Swap(i, j int) {
    45  	hs.hooks[i], hs.hooks[j] = hs.hooks[j], hs.hooks[i]
    46  }
    47  
    48  func (hs *hookWeightSorter) Less(i, j int) bool {
    49  	if hs.hooks[i].Weight == hs.hooks[j].Weight {
    50  		return hs.hooks[i].Name < hs.hooks[j].Name
    51  	}
    52  	return hs.hooks[i].Weight < hs.hooks[j].Weight
    53  }