github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tools/depsgen/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 main 16 17 import ( 18 "flag" 19 "os" 20 "path/filepath" 21 "strings" 22 23 "github.com/coreos/rkt/tools/common" 24 ) 25 26 // toMap creates a map from passed strings. This function expects an 27 // even number of strings, otherwise it will bail out. Odd (first, 28 // third and so on) strings are keys, even (second, fourth and so on) 29 // strings are values. 30 func toMap(kv ...string) map[string]string { 31 if len(kv)%2 != 0 { 32 common.Die("Expected even number of strings in toMap") 33 } 34 r := make(map[string]string, len(kv)) 35 lastKey := "" 36 for i, kv := range kv { 37 if i%2 == 0 { 38 lastKey = kv 39 } else { 40 r[lastKey] = kv 41 } 42 } 43 return r 44 } 45 46 // appName returns application name, like depsgen 47 func appName() string { 48 return filepath.Base(os.Args[0]) 49 } 50 51 // replacePlaceholders replaces placeholders with values in kv in 52 // initial str. Placeholders are in form of !!!FOO!!!, but those 53 // passed here should be without exclamation marks. 54 func replacePlaceholders(str string, kv ...string) string { 55 for ph, value := range toMap(kv...) { 56 str = strings.Replace(str, "!!!"+ph+"!!!", value, -1) 57 } 58 return str 59 } 60 61 // standardFlags returns a new flag set with target flag already set up 62 func standardFlags(cmd string) (*flag.FlagSet, *string) { 63 f := flag.NewFlagSet(appName()+" "+cmd, flag.ExitOnError) 64 target := f.String("target", "", "Make target (example: $(FOO_BINARY))") 65 return f, target 66 }