-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add flag values automatically (#17)
Add mechanism to automatically add flag values based on the config struct. The default value is inferred from the provided struct. Currently, no help text is added to the flags.
- Loading branch information
Showing
6 changed files
with
232 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2020 oncilla | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package flag | ||
|
||
import ( | ||
"encoding" | ||
"net" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
var _ pflag.Value = (*TCPAddr)(nil) | ||
var _ encoding.TextMarshaler = (*TCPAddr)(nil) | ||
|
||
// TCPAddr implements pflags.Value | ||
type TCPAddr net.TCPAddr | ||
|
||
func (addr *TCPAddr) Set(input string) error { | ||
p, err := net.ResolveTCPAddr("tcp", input) | ||
if err != nil { | ||
return err | ||
} | ||
*addr = TCPAddr(*p) | ||
return nil | ||
} | ||
|
||
func (addr *TCPAddr) UnmarshalText(b []byte) error { | ||
return addr.Set(string(b)) | ||
} | ||
|
||
func (addr *TCPAddr) Type() string { | ||
return "tcp-addr" | ||
} | ||
|
||
func (addr *TCPAddr) MarshalText() ([]byte, error) { | ||
return []byte(addr.String()), nil | ||
} | ||
|
||
func (addr *TCPAddr) String() string { | ||
return (*net.TCPAddr)(addr).String() | ||
} | ||
|
||
// UDPAddr implements pflags.Value | ||
type UDPAddr net.UDPAddr | ||
|
||
func (addr *UDPAddr) Set(input string) error { | ||
p, err := net.ResolveUDPAddr("tcp", input) | ||
if err != nil { | ||
return err | ||
} | ||
*addr = UDPAddr(*p) | ||
return nil | ||
} | ||
|
||
func (addr *UDPAddr) UnmarshalText(b []byte) error { | ||
return addr.Set(string(b)) | ||
} | ||
|
||
func (addr *UDPAddr) Type() string { | ||
return "tcp-addr" | ||
} | ||
|
||
func (addr *UDPAddr) MarshalText() ([]byte, error) { | ||
return []byte(addr.String()), nil | ||
} | ||
|
||
func (addr *UDPAddr) String() string { | ||
return (*net.UDPAddr)(addr).String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters