tailscale/types/flagtype/flagtype.go
Will Norris 71029cea2d all: update copyright and license headers
This updates all source files to use a new standard header for copyright
and license declaration.  Notably, copyright no longer includes a date,
and we now use the standard SPDX-License-Identifier header.

This commit was done almost entirely mechanically with perl, and then
some minimal manual fixes.

Updates #6865

Signed-off-by: Will Norris <will@tailscale.com>
2023-01-27 15:36:29 -08:00

46 lines
931 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Package flagtype defines flag.Value types.
package flagtype
import (
"errors"
"flag"
"fmt"
"math"
"strconv"
"strings"
)
type portValue struct{ n *uint16 }
func PortValue(dst *uint16, defaultPort uint16) flag.Value {
*dst = defaultPort
return portValue{dst}
}
func (p portValue) String() string {
if p.n == nil {
return ""
}
return fmt.Sprint(*p.n)
}
func (p portValue) Set(v string) error {
if v == "" {
return errors.New("can't be the empty string")
}
if strings.Contains(v, ":") {
return errors.New("expecting just a port number, without a colon")
}
n, err := strconv.ParseUint(v, 10, 64) // use 64 instead of 16 to return nicer error message
if err != nil {
return fmt.Errorf("not a valid number")
}
if n > math.MaxUint16 {
return errors.New("out of range for port number")
}
*p.n = uint16(n)
return nil
}