cmd/tftp/tftp.go (44 lines of code) (raw):
/*
Copyright (c) Facebook, Inc. and its affiliates.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
*/
package tftp
import (
"fmt"
"strings"
"github.com/facebookincubator/fbender/cmd/core/errors"
"github.com/facebookincubator/fbender/cmd/core/input"
"github.com/facebookincubator/fbender/cmd/core/options"
"github.com/facebookincubator/fbender/cmd/core/runner"
tester "github.com/facebookincubator/fbender/tester/tftp"
"github.com/facebookincubator/fbender/utils"
"github.com/pinterest/bender/tftp"
"github.com/spf13/cobra"
)
// DefaultServerPort is a default tftp server port.
const DefaultServerPort = 69
func params(cmd *cobra.Command, o *options.Options) (*runner.Params, error) {
blocksize, err := cmd.Flags().GetInt("blocksize")
if err != nil {
//nolint:wrapcheck
return nil, err
}
r, err := input.NewRequestGenerator(o.Input, inputTransformer)
if err != nil {
//nolint:wrapcheck
return nil, err
}
t := &tester.Tester{
Target: utils.WithDefaultPort(o.Target, DefaultServerPort),
Timeout: o.Timeout,
BlockSize: blocksize,
}
return &runner.Params{Tester: t, RequestGenerator: r}, nil
}
func inputTransformer(input string) (interface{}, error) {
i := strings.Index(input, " ")
if i < 0 {
return nil, fmt.Errorf("%w, want: \"File Mode\" got %q", errors.ErrInvalidFormat, input)
}
filename, mode := input[:i], input[i+1:]
if mode != "octet" && mode != "netascii" {
return nil, fmt.Errorf("%w, want: (octet|netascii), got: %q", errors.ErrInvalidFormat, mode)
}
return &tftp.Request{
Filename: filename,
Mode: tftp.RequestMode(mode),
}, nil
}