github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/global.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     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 cmd
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/palantir/pkg/cli/flag"
    22  
    23  	"github.com/palantir/godel/apps/distgo/pkg/osarch"
    24  )
    25  
    26  const (
    27  	ProductsParamName = "products"
    28  	OSArchFlagName    = "os-arch"
    29  )
    30  
    31  var (
    32  	ProductsParam = flag.StringSlice{
    33  		Name:     ProductsParamName,
    34  		Usage:    "Products for which action should be performed",
    35  		Optional: true,
    36  	}
    37  	OSArchFlag = flag.StringFlag{
    38  		Name:  OSArchFlagName,
    39  		Usage: "GOOS-GOARCH for the command (comma-separate for multiple values)",
    40  	}
    41  )
    42  
    43  type OSArchFilter []osarch.OSArch
    44  
    45  // Matches returns true if the provided osArch is in the filter list or if the filter list is empty.
    46  func (f OSArchFilter) Matches(osArch osarch.OSArch) bool {
    47  	if len(f) == 0 {
    48  		return true
    49  	}
    50  	for _, curr := range f {
    51  		if curr == osArch {
    52  			return true
    53  		}
    54  	}
    55  	return false
    56  }
    57  
    58  func NewOSArchFilter(osArchs string) (OSArchFilter, error) {
    59  	if osArchs == "" {
    60  		return nil, nil
    61  	}
    62  
    63  	var filterArchs []osarch.OSArch
    64  	var invalidValues []string
    65  	// if value was provided for flag, parse
    66  	for _, osArchStr := range strings.Split(osArchs, ",") {
    67  		if osArch, err := osarch.New(osArchStr); err == nil {
    68  			filterArchs = append(filterArchs, osArch)
    69  		} else {
    70  			invalidValues = append(invalidValues, osArchStr)
    71  		}
    72  	}
    73  	if len(invalidValues) > 0 {
    74  		return nil, fmt.Errorf("invalid os-arch values: %v", invalidValues)
    75  	}
    76  
    77  	return OSArchFilter(filterArchs), nil
    78  }