github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/tools/common/util.go (about)

     1  // Copyright 2015 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 common
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  )
    23  
    24  // StringSliceWrapper is an implementation of flag.Value interface. It
    25  // is basically a proxy that appends strings to an already existing
    26  // strings slice.
    27  type StringSliceWrapper struct {
    28  	Slice *[]string
    29  }
    30  
    31  func (wrapper *StringSliceWrapper) String() string {
    32  	if len(*wrapper.Slice) > 0 {
    33  		return fmt.Sprintf(`["%s"]`, strings.Join(*wrapper.Slice, `" "`))
    34  	}
    35  	return "[]"
    36  }
    37  
    38  func (wrapper *StringSliceWrapper) Set(str string) error {
    39  	*wrapper.Slice = append(*wrapper.Slice, str)
    40  	return nil
    41  }
    42  
    43  // Warn is just a shorter version of a formatted printing to
    44  // stderr. It appends a newline for you.
    45  func Warn(format string, values ...interface{}) {
    46  	fmt.Fprintf(os.Stderr, fmt.Sprintf("%s%c", format, '\n'), values...)
    47  }
    48  
    49  // Die is the same as Warn, but it quits with exit status 1 after
    50  // printing a message.
    51  func Die(format string, values ...interface{}) {
    52  	Warn(format, values...)
    53  	os.Exit(1)
    54  }
    55  
    56  // MapFilesToDirectories creates one entry for each file and directory
    57  // passed. Result is a kind of cross-product. For files f1 and f2 and
    58  // directories d1 and d2, the returned list will contain d1/f1 d2/f1
    59  // d1/f2 d2/f2.
    60  //
    61  // The "files" part might be a bit misleading - you can pass a list of
    62  // symlinks or directories there as well.
    63  func MapFilesToDirectories(files, dirs []string) []string {
    64  	mapped := make([]string, 0, len(files)*len(dirs))
    65  	for _, f := range files {
    66  		for _, d := range dirs {
    67  			mapped = append(mapped, filepath.Join(d, f))
    68  		}
    69  	}
    70  	return mapped
    71  }
    72  
    73  // MustAbs returns an absolute path. It works like filepath.Abs, but
    74  // panics if it fails.
    75  func MustAbs(dir string) string {
    76  	absDir, err := filepath.Abs(dir)
    77  	if err != nil {
    78  		panic(fmt.Sprintf("Failed to get absolute path of a directory %q: %v\n", dir, err))
    79  	}
    80  	return filepath.Clean(absDir)
    81  }