github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/tools/cleangen/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  	"flag"
    19  	"fmt"
    20  	"os"
    21  	"strings"
    22  
    23  	"github.com/coreos/rkt/tools/common"
    24  	"github.com/coreos/rkt/tools/common/filelist"
    25  )
    26  
    27  type mkPair struct {
    28  	name string
    29  	data *[]string
    30  }
    31  
    32  func main() {
    33  	filename, mapTo := getValidArgs()
    34  	list := getListing(filename)
    35  	pairs := getPairs(list)
    36  	for _, pair := range pairs {
    37  		toClean := common.MapFilesToDirectories(*pair.data, mapTo)
    38  		fmt.Printf("%s += %s\n", pair.name, strings.Join(toClean, " "))
    39  	}
    40  }
    41  
    42  func getValidArgs() (string, []string) {
    43  	filename := ""
    44  	var mapTo []string
    45  	mapToWrapper := common.StringSliceWrapper{
    46  		Slice: &mapTo,
    47  	}
    48  
    49  	flag.StringVar(&filename, "filelist", "", "Name of the source (path to the directory or to the filelist")
    50  	flag.Var(&mapToWrapper, "map-to", "Map contents of traversed source to this directory, can be used multiple times")
    51  
    52  	flag.Parse()
    53  	if filename == "" {
    54  		common.Die("No --filelist parameter passed")
    55  	}
    56  	if len(mapTo) < 1 {
    57  		common.Die("No --map-to parameter passed, at least one is required")
    58  		os.Exit(1)
    59  	}
    60  
    61  	filename = common.MustAbs(filename)
    62  	for i := 0; i < len(mapTo); i++ {
    63  		mapTo[i] = common.MustAbs(mapTo[i])
    64  	}
    65  	return filename, mapTo
    66  }
    67  
    68  func getListing(filename string) *filelist.Lists {
    69  	fl, err := os.Open(filename)
    70  	if err != nil {
    71  		common.Die("Failed to open filelist %q: %v", filename, err)
    72  	}
    73  	defer fl.Close()
    74  
    75  	list := &filelist.Lists{}
    76  	if err := list.ParseFilelist(fl); err != nil {
    77  		common.Die("Error during getting listing from filelist %q: %v\n", filename, err)
    78  	}
    79  	return list
    80  }
    81  
    82  func getPairs(list *filelist.Lists) []mkPair {
    83  	return []mkPair{
    84  		{name: "CLEAN_FILES", data: &list.Files},
    85  		{name: "CLEAN_SYMLINKS", data: &list.Symlinks},
    86  		{name: "CLEAN_DIRS", data: &list.Dirs},
    87  	}
    88  }