github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/flags/flags.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     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  	"bytes"
    19  	"fmt"
    20  	"net/url"
    21  	"os"
    22  	"strings"
    23  )
    24  
    25  type Uri struct {
    26  	Key string
    27  	Val url.URL
    28  }
    29  
    30  func (u *Uri) String() string {
    31  	val := u.Val.String()
    32  	if val == "" {
    33  		return fmt.Sprintf("%s", u.Key)
    34  	}
    35  	return fmt.Sprintf("%s:%s", u.Key, val)
    36  }
    37  
    38  func (u *Uri) Set(value string) error {
    39  	s := strings.SplitN(value, ":", 2)
    40  	if s[0] == "" {
    41  		return fmt.Errorf("missing uri key in '%s'", value)
    42  	}
    43  	u.Key = s[0]
    44  	if len(s) > 1 && s[1] != "" {
    45  		e := os.ExpandEnv(s[1])
    46  		uri, err := url.Parse(e)
    47  		if err != nil {
    48  			return err
    49  		}
    50  		u.Val = *uri
    51  	}
    52  	return nil
    53  }
    54  
    55  type Uris []Uri
    56  
    57  func (us *Uris) String() string {
    58  	var b bytes.Buffer
    59  	b.WriteString("[")
    60  	for i, u := range *us {
    61  		if i > 0 {
    62  			b.WriteString(" ")
    63  		}
    64  		b.WriteString(u.String())
    65  	}
    66  	b.WriteString("]")
    67  	return b.String()
    68  }
    69  
    70  func (us *Uris) Set(value string) error {
    71  	var u Uri
    72  	if err := u.Set(value); err != nil {
    73  		return err
    74  	}
    75  	*us = append(*us, u)
    76  	return nil
    77  }
    78  
    79  func (us *Uris) Type() string {
    80  	return fmt.Sprintf("%T", us)
    81  }