github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tools/depsgen/main.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  	"fmt"
    19  	"os"
    20  	"sort"
    21  	"strings"
    22  
    23  	"github.com/coreos/rkt/tools/common"
    24  )
    25  
    26  var cmds = make(map[string]func([]string) string)
    27  
    28  func main() {
    29  	depTypes := getAllDepTypes()
    30  	depTypesString := fmt.Sprintf("'%s'", strings.Join(depTypes, "', '"))
    31  	if len(os.Args) < 2 {
    32  		common.Die("Expected a deps type (possible values: %s)", depTypesString)
    33  	}
    34  	depType := os.Args[1]
    35  	cmdArgs := os.Args[2:]
    36  	if f, ok := cmds[depType]; ok {
    37  		fmt.Print(f(cmdArgs))
    38  	} else if depType == "--help" || depType == "-help" {
    39  		common.Warn("Run %s with one of the following commands: %s\nE.g. %s %s --help", appName(), depTypesString, os.Args[0], depTypes[0])
    40  	} else {
    41  		common.Die("Unknown deps type: %q, expected one of %s", depType, depTypesString)
    42  	}
    43  }
    44  
    45  // getAllDepTypes returns a sorted list of names of all dep type
    46  // commands.
    47  func getAllDepTypes() []string {
    48  	depTypes := make([]string, 0, len(cmds))
    49  	for depType := range cmds {
    50  		depTypes = append(depTypes, depType)
    51  	}
    52  	sort.Strings(depTypes)
    53  	return depTypes
    54  }