github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/flags/urls.go (about)

     1  // Copyright 2020 PingCAP, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package flags
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/pingcap/errors"
    20  	"github.com/pingcap/ticdc/pkg/types"
    21  )
    22  
    23  // URLsValue define a slice of URLs as a type
    24  type URLsValue types.URLs
    25  
    26  // Set parses a command line set of URLs formatted like:
    27  // http://127.0.0.1:2380,http://10.1.1.2:80
    28  func (us *URLsValue) Set(s string) error {
    29  	strs := strings.Split(s, ",")
    30  	nus, err := types.NewURLs(strs)
    31  	if err != nil {
    32  		return errors.Trace(err)
    33  	}
    34  
    35  	*us = URLsValue(nus)
    36  	return nil
    37  }
    38  
    39  // HostString return a string of host:port format list separated by comma
    40  func (us *URLsValue) HostString() string {
    41  	all := make([]string, len(*us))
    42  	for i, u := range *us {
    43  		all[i] = u.Host
    44  	}
    45  	return strings.Join(all, ",")
    46  }
    47  
    48  // NewURLsValue return a URLsValue from a string of URLs list
    49  func NewURLsValue(init string) (*URLsValue, error) {
    50  	v := &URLsValue{}
    51  	err := v.Set(init)
    52  	return v, err
    53  }