Skip to content

Commit

Permalink
Merge pull request #154 from claudiodangelis/release-0-6-3
Browse files Browse the repository at this point in the history
0.6.3
  • Loading branch information
claudiodangelis authored May 17, 2020
2 parents bdbe483 + 0d34a6f commit abad02f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 37 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ qrcp --list-all-interfaces config
```


### Configuration File

The default configuration file is stored in $HOME/qrcp.json, however, you can specify the location of the config file by passing the `--config` flag:

```sh
qrcp --config /tmp/qrcp.json MyDocument.pdf
```

### Port

By default `qrcp` listens on a random port. Pass the `--port` (or `-p`) flag to choose a specific one:
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func configCmdFunc(command *cobra.Command, args []string) error {
return config.Wizard()
return config.Wizard(configFlag, listallinterfacesFlag)
}

var configCmd = &cobra.Command{
Expand Down
2 changes: 2 additions & 0 deletions cmd/qrcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&interfaceFlag, "interface", "i", "", "network interface to use for the server")
rootCmd.PersistentFlags().StringVarP(&fqdnFlag, "fqdn", "d", "", "fully-qualified domain name to use for the resulting URLs")
rootCmd.PersistentFlags().BoolVarP(&zipFlag, "zip", "z", false, "zip content before transferring")
rootCmd.PersistentFlags().StringVarP(&configFlag, "config", "c", "", "path to the config file, defaults to $HOME/.qrcp")
// Receive command flags
receiveCmd.PersistentFlags().StringVarP(&outputFlag, "output", "o", "", "output directory for receiving files")
}
Expand All @@ -32,6 +33,7 @@ var quietFlag bool
var fqdnFlag string
var pathFlag string
var listallinterfacesFlag bool
var configFlag string

// The root command (`qrcp`) is like a shortcut of the `send` command
var rootCmd = &cobra.Command{
Expand Down
10 changes: 9 additions & 1 deletion cmd/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import (
func receiveCmdFunc(command *cobra.Command, args []string) error {
log := logger.New(quietFlag)
// Load configuration
cfg, err := config.New(interfaceFlag, portFlag, pathFlag, fqdnFlag, keepaliveFlag, listallinterfacesFlag)
configOptions := config.Options{
Interface: interfaceFlag,
Port: portFlag,
Path: pathFlag,
FQDN: fqdnFlag,
KeepAlive: keepaliveFlag,
ListAllInterfaces: listallinterfacesFlag,
}
cfg, err := config.New(configFlag, configOptions)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
if err != nil {
return err
}
cfg, err := config.New(interfaceFlag, portFlag, pathFlag, fqdnFlag, keepaliveFlag, listallinterfacesFlag)
// Load configuration
configOptions := config.Options{
Interface: interfaceFlag,
Port: portFlag,
Path: pathFlag,
FQDN: fqdnFlag,
KeepAlive: keepaliveFlag,
ListAllInterfaces: listallinterfacesFlag,
}
cfg, err := config.New(configFlag, configOptions)
if err != nil {
return err
}
Expand Down
103 changes: 69 additions & 34 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
Expand All @@ -23,29 +24,29 @@ type Config struct {
Path string `json:"path"`
}

func configFile() string {
currentUser, err := user.Current()
if err != nil {
panic(err)
}
return filepath.Join(currentUser.HomeDir, ".qrcp.json")
}
var configFile string

type configOptions struct {
interactive bool
listAllInterfaces bool
// Options of the qrcp configuration
type Options struct {
Interface string
Port int
Path string
FQDN string
KeepAlive bool
Interactive bool
ListAllInterfaces bool
}

func chooseInterface(opts configOptions) (string, error) {
interfaces, err := util.Interfaces(opts.listAllInterfaces)
func chooseInterface(opts Options) (string, error) {
interfaces, err := util.Interfaces(opts.ListAllInterfaces)
if err != nil {
return "", err
}
if len(interfaces) == 0 {
return "", errors.New("no interfaces found")
}

if len(interfaces) == 1 && opts.interactive == false {
if len(interfaces) == 1 && opts.Interactive == false {
for name := range interfaces {
fmt.Printf("only one interface found: %s, using this one\n", name)
return name, nil
Expand Down Expand Up @@ -77,10 +78,10 @@ func chooseInterface(opts configOptions) (string, error) {
}

// Load a new configuration
func Load(opts configOptions) (Config, error) {
func Load(opts Options) (Config, error) {
var cfg Config
// Read the configuration file, if it exists
if file, err := ioutil.ReadFile(configFile()); err == nil {
if file, err := ioutil.ReadFile(configFile); err == nil {
// Read the config
if err := json.Unmarshal(file, &cfg); err != nil {
return cfg, err
Expand All @@ -102,17 +103,21 @@ func Load(opts configOptions) (Config, error) {
}

// Wizard starts an interactive configuration managements
func Wizard() error {
func Wizard(path string, listAllInterfaces bool) error {
if err := setConfigFile(path); err != nil {
return err
}
var cfg Config
if file, err := ioutil.ReadFile(configFile()); err == nil {
if file, err := ioutil.ReadFile(configFile); err == nil {
// Read the config
if err := json.Unmarshal(file, &cfg); err != nil {
return err
}
}
// Ask for interface
opts := configOptions{
interactive: true,
opts := Options{
Interactive: true,
ListAllInterfaces: listAllInterfaces,
}
iface, err := chooseInterface(opts)
if err != nil {
Expand Down Expand Up @@ -195,40 +200,70 @@ func write(cfg Config) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(configFile(), j, 0644); err != nil {
if err := ioutil.WriteFile(configFile, j, 0644); err != nil {
return err
}
return nil
}

func setConfigFile(path string) error {
if path == "" {
// Use default
currentUser, err := user.Current()
if err != nil {
return err
}
configFile = filepath.Join(currentUser.HomeDir, ".qrcp.json")
return nil
}
absolutepath, err := filepath.Abs(path)
if err != nil {
return err
}
fileinfo, err := os.Stat(absolutepath)
if err != nil && !os.IsNotExist(err) {
return err
}
if fileinfo != nil && fileinfo.IsDir() {
return fmt.Errorf("%s is not a file", absolutepath)
}
configFile = absolutepath
return nil
}

// New returns a new configuration struct. It loads defaults, then overrides
// values if any.
func New(iface string, port int, path string, fqdn string, keepAlive bool, listAllInterfaces bool) (Config, error) {
// Load saved file / defults
cfg, err := Load(configOptions{listAllInterfaces: listAllInterfaces})
func New(path string, opts Options) (Config, error) {
var cfg Config
// Set configFile
if err := setConfigFile(path); err != nil {
return cfg, err
}
// Load saved file / defaults
cfg, err := Load(opts)
if err != nil {
return cfg, err
}
if iface != "" {
cfg.Interface = iface
if opts.Interface != "" {
cfg.Interface = opts.Interface
}
if fqdn != "" {
if govalidator.IsDNSName(fqdn) == false {
if opts.FQDN != "" {
if govalidator.IsDNSName(opts.FQDN) == false {
return cfg, errors.New("invalid value for fully-qualified domain name")
}
cfg.FQDN = fqdn
cfg.FQDN = opts.FQDN
}
if port != 0 {
if port > 65535 {
if opts.Port != 0 {
if opts.Port > 65535 {
return cfg, errors.New("invalid value for port")
}
cfg.Port = port
cfg.Port = opts.Port
}
if keepAlive {
if opts.KeepAlive {
cfg.KeepAlive = true
}
if path != "" {
cfg.Path = path
if opts.Path != "" {
cfg.Path = opts.Path
}
return cfg, nil
}

0 comments on commit abad02f

Please sign in to comment.