github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/store/utils.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package store
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"sort"
    21  	"strings"
    22  
    23  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types"
    24  )
    25  
    26  // blockTransform creates a path slice from the given string to use as a
    27  // directory prefix. The string must be in hash format:
    28  //    "sha256-abcdefgh"... -> []{"sha256", "ab"}
    29  // Right now it just copies the default of git which is a two byte prefix. We
    30  // will likely want to add re-sharding later.
    31  func blockTransform(s string) []string {
    32  	// TODO(philips): use spec/types.Hash after export typ field
    33  	parts := strings.SplitN(s, "-", 2)
    34  	if len(parts) != 2 {
    35  		panic(fmt.Errorf("blockTransform should never receive non-hash, got %v", s))
    36  	}
    37  	return []string{
    38  		parts[0],
    39  		parts[1][0:2],
    40  	}
    41  }
    42  
    43  func parseAlways(s string) *url.URL {
    44  	u, _ := url.Parse(s)
    45  	return u
    46  }
    47  
    48  func getLabelPriority(name types.ACIdentifier) int {
    49  	labelsPriority := map[types.ACIdentifier]int{
    50  		"version": 0,
    51  		"os":      1,
    52  		"arch":    2,
    53  	}
    54  	if i, ok := labelsPriority[name]; ok {
    55  		return i
    56  	}
    57  	return len(labelsPriority) + 1
    58  }
    59  
    60  // labelsSlice implements sort.Interface for types.Labels
    61  type labelsSlice types.Labels
    62  
    63  func (p labelsSlice) Len() int { return len(p) }
    64  func (p labelsSlice) Less(i, j int) bool {
    65  	return getLabelPriority(p[i].Name) < getLabelPriority(p[j].Name)
    66  }
    67  func (p labelsSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    68  
    69  func labelsToString(inLabels types.Labels) string {
    70  	// take a copy to avoid changing the original slice
    71  	labels := append(types.Labels(nil), inLabels...)
    72  	sort.Sort(labelsSlice(labels))
    73  
    74  	var out []string
    75  	for _, l := range labels {
    76  		out = append(out, fmt.Sprintf("%q:%q", l.Name, l.Value))
    77  	}
    78  	return "[" + strings.Join(out, ", ") + "]"
    79  }