Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Oct 18, 2021
1 parent 378a7f2 commit 2f37a40
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
24 changes: 14 additions & 10 deletions cli/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,56 @@ import (
"github.com/peterbourgon/ff/v3/ffcli"
)

type bin struct {
type Bin struct {
ffcli.Command
jsonOut bool
}

// Option for setting optional Client values
type Option func(*bin)
type Option func(*Bin)

func WithName(name string) Option {
return func(cfg *bin) {
return func(cfg *Bin) {
cfg.Name = name
}
}

func WithShortUsage(shortUsage string) Option {
return func(cfg *bin) {
return func(cfg *Bin) {
cfg.ShortUsage = shortUsage
}
}

func WithUsageFunc(usageFunc func(*ffcli.Command) string) Option {
return func(cfg *bin) {
return func(cfg *Bin) {
cfg.UsageFunc = usageFunc
}
}

func WithFlagSet(flagSet *flag.FlagSet) Option {
return func(cfg *bin) {
return func(cfg *Bin) {
cfg.FlagSet = flagSet
}
}

func WithOptions(opts ...ff.Option) Option {
return func(cfg *bin) {
return func(cfg *Bin) {
cfg.Options = append(cfg.Options, opts...)
}
}

func SupportedBins(ctx context.Context, opts ...Option) *ffcli.Command {
name := "binary"
fs := flag.NewFlagSet(name, flag.ExitOnError)
defaultCfg := &bin{
defaultCfg := &Bin{
Command: ffcli.Command{
Name: name,
ShortUsage: fmt.Sprintf("%v returns the mapping of architecture to ipxe binary name", name),
FlagSet: fs,
},
}
defaultCfg.Exec = defaultCfg.Execute
fs.BoolVar(&defaultCfg.jsonOut, "json", false, "output in json format")
defaultCfg.RegisterFlags(fs)

for _, opt := range opts {
opt(defaultCfg)
Expand All @@ -80,8 +80,12 @@ func SupportedBins(ctx context.Context, opts ...Option) *ffcli.Command {
}
}

func (b *Bin) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(&b.jsonOut, "json", false, "output in json format")
}

// Execute function for this command.
func (b *bin) Execute(ctx context.Context, _ []string) error {
func (b *Bin) Execute(ctx context.Context, _ []string) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Arch", "Binary"})

Expand Down
46 changes: 28 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

const appName = "proxy"

type config struct {
type Config struct {
ffcli.Command
LogLevel string `vname:"-loglevel" validate:"oneof=debug info"`
TftpAddr string `vname:"-tftp-addr" validate:"required,url"`
Expand All @@ -30,50 +30,56 @@ type config struct {
}

// Option for setting optional Client values
type ProxyOption func(*config)
type ProxyOption func(*Config)

func ProxyWithName(name string) ProxyOption {
return func(cfg *config) {
return func(cfg *Config) {
cfg.Name = name
}
}

func ProxyWithShortUsage(shortUsage string) ProxyOption {
return func(cfg *config) {
return func(cfg *Config) {
cfg.ShortUsage = shortUsage
}
}

func ProxyWithUsageFunc(usageFunc func(*ffcli.Command) string) ProxyOption {
return func(cfg *config) {
return func(cfg *Config) {
cfg.UsageFunc = usageFunc
}
}

func ProxyWithFlagSet(flagSet *flag.FlagSet) ProxyOption {
return func(cfg *config) {
return func(cfg *Config) {
cfg.FlagSet = flagSet
}
}

func ProxyWithOptions(opts ...ff.Option) ProxyOption {
return func(cfg *config) {
return func(cfg *Config) {
cfg.Options = append(cfg.Options, opts...)
}
}

func ProxyDHCP(ctx context.Context, opts ...ProxyOption) (*ffcli.Command, *config) {
func ProxyWithLogger(l logr.Logger) ProxyOption {
return func(cfg *Config) {
cfg.Log = l
}
}

func ProxyDHCP(ctx context.Context, opts ...ProxyOption) (*ffcli.Command, *Config) {
fs := flag.NewFlagSet(appName, flag.ExitOnError)
cfg := &config{
cfg := &Config{
Command: ffcli.Command{
Name: appName,
ShortUsage: fmt.Sprintf("%v runs the proxyDHCP server", appName),
FlagSet: fs,
Name: appName,
ShortUsage: fmt.Sprintf("%v runs the proxyDHCP server", appName),
FlagSet: fs,
},
Log: logr.Discard(),
}

//
cfg.RegisterFlags(fs)
RegisterFlags(cfg, fs)
for _, opt := range opts {
opt(cfg)
}
Expand All @@ -91,7 +97,7 @@ func ProxyDHCP(ctx context.Context, opts ...ProxyOption) (*ffcli.Command, *confi
}, cfg
}

func (c *config) RegisterFlags(fs *flag.FlagSet) {
func RegisterFlags(c *Config, fs *flag.FlagSet) {
fs.StringVar(&c.LogLevel, "loglevel", "info", "log level (optional)")
fs.StringVar(&c.Addr, "addr", "0.0.0.0:67", "IP and port to listen on for proxydhcp requests.")
fs.StringVar(&c.TftpAddr, "tftp-addr", "", "IP and URI of the TFTP server providing iPXE binaries (192.168.2.5/binaries).")
Expand All @@ -100,7 +106,7 @@ func (c *config) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(&c.CustomUserClass, "user-class", "", "A custom user-class (dhcp option 77) to use to determine when to pivot to serving the ipxe script from the ipxe-url flag.")
}

func (c *config) validateConfig() error {
func (c *Config) ValidateConfig() error {
v := validator.New()
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("vname"), ",", 2)[0]
Expand All @@ -123,12 +129,16 @@ func (c *config) validateConfig() error {
}

// Exec function for this command.
func (c *config) Exec(ctx context.Context, _ []string) error {
if err := c.validateConfig(); err != nil {
func (c *Config) Exec(ctx context.Context, args []string) error {
if err := c.ValidateConfig(); err != nil {

return err
}

return c.Run(ctx, args)
}

func (c *Config) Run(ctx context.Context, _ []string) error {
redirectionListener, err := proxy.NewListener(c.Addr)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ require (
github.com/google/go-cmp v0.5.6
github.com/libp2p/go-reuseport v0.0.2
github.com/olekukonko/tablewriter v0.0.5
github.com/peterbourgon/ff/v3 v3.1.0
github.com/peterbourgon/ff/v3 v3.1.2
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.19.1
go.universe.tf/netboot v0.0.0-20210617221821-fc2840fa7b05
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/net v0.0.0-20210825183410-e898025ed96a
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
inet.af/netaddr v0.0.0-20210903134321-85fa6c94624e
)
Expand Down
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/ff/v3 v3.1.0 h1:5JAeDK5j/zhKFjyHEZQXwXBoDijERaos10RE+xamOsY=
github.com/peterbourgon/ff/v3 v3.1.0/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
github.com/peterbourgon/ff/v3 v3.1.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM=
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -189,8 +189,9 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210825183410-e898025ed96a h1:bRuuGXV8wwSdGTB+CtJf+FjgO1APK1CoO39T4BN/XBw=
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -212,6 +213,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
Expand Down

0 comments on commit 2f37a40

Please sign in to comment.