github.com/colincross/blueprint@v0.0.0-20150626231830-9c067caf2eb5/pathtools/lists.go (about)

     1  // Copyright 2014 Google Inc. All rights reserved.
     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 pathtools
    16  
    17  import (
    18  	"path/filepath"
    19  	"strings"
    20  )
    21  
    22  // PrefixPaths returns a list of paths consisting of prefix joined with each
    23  // element of paths.  The resulting paths are "clean" in the filepath.Clean
    24  // sense.
    25  func PrefixPaths(paths []string, prefix string) []string {
    26  	result := make([]string, len(paths))
    27  	for i, path := range paths {
    28  		result[i] = filepath.Join(prefix, path)
    29  	}
    30  	return result
    31  }
    32  
    33  func ReplaceExtensions(paths []string, extension string) []string {
    34  	result := make([]string, len(paths))
    35  	for i, path := range paths {
    36  		result[i] = ReplaceExtension(path, extension)
    37  	}
    38  	return result
    39  }
    40  
    41  func ReplaceExtension(path string, extension string) string {
    42  	dot := strings.LastIndex(path, ".")
    43  	if dot == -1 {
    44  		return path
    45  	}
    46  	return path[:dot+1] + extension
    47  }