1// Copyright 2015 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 15package flags 16 17import ( 18 "flag" 19 "fmt" 20 "net/url" 21 "strings" 22 23 "go.etcd.io/etcd/pkg/v3/types" 24) 25 26// URLsValue wraps "types.URLs". 27type URLsValue types.URLs 28 29// Set parses a command line set of URLs formatted like: 30// http://127.0.0.1:2380,http://10.1.1.2:80 31// Implements "flag.Value" interface. 32func (us *URLsValue) Set(s string) error { 33 ss, err := types.NewURLs(strings.Split(s, ",")) 34 if err != nil { 35 return err 36 } 37 *us = URLsValue(ss) 38 return nil 39} 40 41// String implements "flag.Value" interface. 42func (us *URLsValue) String() string { 43 all := make([]string, len(*us)) 44 for i, u := range *us { 45 all[i] = u.String() 46 } 47 return strings.Join(all, ",") 48} 49 50// NewURLsValue implements "url.URL" slice as flag.Value interface. 51// Given value is to be separated by comma. 52func NewURLsValue(s string) *URLsValue { 53 if s == "" { 54 return &URLsValue{} 55 } 56 v := &URLsValue{} 57 if err := v.Set(s); err != nil { 58 panic(fmt.Sprintf("new URLsValue should never fail: %v", err)) 59 } 60 return v 61} 62 63// URLsFromFlag returns a slices from url got from the flag. 64func URLsFromFlag(fs *flag.FlagSet, urlsFlagName string) []url.URL { 65 return []url.URL(*fs.Lookup(urlsFlagName).Value.(*URLsValue)) 66} 67