...

Source file src/go.etcd.io/etcd/pkg/v3/flags/unique_strings.go

Documentation: go.etcd.io/etcd/pkg/v3/flags

     1  // Copyright 2018 The etcd 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 flags
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"sort"
    21  	"strings"
    22  )
    23  
    24  // UniqueStringsValue wraps a list of unique strings.
    25  // The values are set in order.
    26  type UniqueStringsValue struct {
    27  	Values map[string]struct{}
    28  }
    29  
    30  // Set parses a command line set of strings, separated by comma.
    31  // Implements "flag.Value" interface.
    32  // The values are set in order.
    33  func (us *UniqueStringsValue) Set(s string) error {
    34  	us.Values = make(map[string]struct{})
    35  	for _, v := range strings.Split(s, ",") {
    36  		us.Values[v] = struct{}{}
    37  	}
    38  	return nil
    39  }
    40  
    41  // String implements "flag.Value" interface.
    42  func (us *UniqueStringsValue) String() string {
    43  	return strings.Join(us.stringSlice(), ",")
    44  }
    45  
    46  func (us *UniqueStringsValue) stringSlice() []string {
    47  	ss := make([]string, 0, len(us.Values))
    48  	for v := range us.Values {
    49  		ss = append(ss, v)
    50  	}
    51  	sort.Strings(ss)
    52  	return ss
    53  }
    54  
    55  // NewUniqueStringsValue implements string slice as "flag.Value" interface.
    56  // Given value is to be separated by comma.
    57  // The values are set in order.
    58  func NewUniqueStringsValue(s string) (us *UniqueStringsValue) {
    59  	us = &UniqueStringsValue{Values: make(map[string]struct{})}
    60  	if s == "" {
    61  		return us
    62  	}
    63  	if err := us.Set(s); err != nil {
    64  		panic(fmt.Sprintf("new UniqueStringsValue should never fail: %v", err))
    65  	}
    66  	return us
    67  }
    68  
    69  // UniqueStringsFromFlag returns a string slice from the flag.
    70  func UniqueStringsFromFlag(fs *flag.FlagSet, flagName string) []string {
    71  	return (*fs.Lookup(flagName).Value.(*UniqueStringsValue)).stringSlice()
    72  }
    73  
    74  // UniqueStringsMapFromFlag returns a map of strings from the flag.
    75  func UniqueStringsMapFromFlag(fs *flag.FlagSet, flagName string) map[string]struct{} {
    76  	return (*fs.Lookup(flagName).Value.(*UniqueStringsValue)).Values
    77  }
    78  

View as plain text