Skip to content

Conversation

arcanericky
Copy link
Contributor

This is a proposal in the form of a PR to migrate the CLI from the standard library to the Cobra package under the Apache License 2.0. Cobra is used by Kubernetes, the GitHub CLI, GoReleaser and many other popular projects.

I can think of several benefits for this project with this migration:

  • Standard and familiar CLI interface like those of the projects mentioned above.
  • Long (--ssid) and short (-s) option formats.
  • Built in help output.
  • Automatic version option.
  • A more full featured flags library in general.

A couple of reasons why you may not want to migrate:

  • Dragging yet another non-standard library into this project.
  • Larger executable (from about 3 mb to about 3.5 mb).
  • You have a problem with the Apache License 2.0.

Command Options

To transition to the combined long and short options, I've changed some of the options. These are simply my suggestions and should be carefully reviewed to ensure it's what you want. I can make any changes here that you'd like.

Flag usage from the CLI changes a bit because of the long and short options.

The changes are:

  • -file is now --output and -o which seems to be consistent with other utilities
  • -enc is now --protocol and -p because it is the protocol being specified
  • The short form of --ssid is -i for ID so it doesn't interfere with the -s (size) short option

I'm personally not chained to any of this, so if you want the Cobra implementation but with different option names, let me know and I can change it, or you can approve and merge then change to your liking, etc.

The No Password Edge Case

When the user presents an empty key/password via the CLI (--enc ""), the user would still be prompted for password input instead of enabling no enryption protocol (NONE) which is what the user probably wants. This could probably be resolved using the old standard library but would require more than a couple lines of code. Cobra's pflag library makes this easy with the Changed method which has been implemented in this PR.

README

It has been updated with the new automatically generated Cobra help and the example command incantation updated with the new option names. For this PR, the new help output is:

$ wifiqr --help
wifiqr is a WiFi QR code generator

It is used to create a QR code containing the login details such as
the name, password, and encryption type. This QR code can be scanned
using Google Lens or other QR code reader to connect to the network.
It is Android and iOS compatible.

If the options necessary for creating the QR code are not given on
the command line, the user will be prompted for the information.

Usage:
  wifiqr [flags]

Flags:
  -h, --help              help for wifiqr
      --hidden            Hidden SSID
  -k, --key string        Wireless password (pre-shared key / PSK)
  -o, --output string     PNG file for output (default stdout)
  -p, --protocol string   Wireless network encryption protocol (WPA2, WPA, WEP, NONE). (default "WPA2")
  -s, --size int          Image width and height in pixels (default 256)
  -i, --ssid string       Wireless network name
  -v, --version           version for wifiqr

And the version output example:

$ wifiqr --version
wifiqr version 0.9.9

Why?

I'm submitting all these PRs because I use this utility every week to connect to a protected wifi network. I'm a software developer that enjoys coding in Go. Put them together and I just wanted to give back. I also have the opinion that Cobra should be used to process user input for nearly every Go application. Thanks for accepting my previous PRs.

@arcanericky arcanericky changed the title Pull Request / Proposal for Cobra Cobra Proposal / Pull Request Dec 7, 2021
@arcanericky arcanericky closed this Dec 7, 2021
@arcanericky arcanericky deleted the cobra branch December 7, 2021 15:03
@arcanericky arcanericky restored the cobra branch December 7, 2021 15:05
@arcanericky arcanericky reopened this Dec 7, 2021
@arcanericky arcanericky changed the base branch from main to develop December 7, 2021 15:06
Copy link
Owner

@reugn reugn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arcanericky I personally prefer thin libraries for simple tasks (the delta in go.sum is insane). But you're right cobra is a well-known and widely used cli command parser, so I don't have any objections since the changes look great. Merging them in. Thanks again for your interest and contributions. I plan to release the next version this weekend.

@reugn reugn changed the title Cobra Proposal / Pull Request Migrate the CLI from the standard library to Cobra Dec 7, 2021
@reugn reugn merged commit cf9630e into reugn:develop Dec 7, 2021
@arcanericky
Copy link
Contributor Author

Yes - totally agree with the observation of all extra dependencies Cobra includes. It's mostly because of that Viper package that it pulls in because Cobra itself has only 5 direct dependencies. I could tell you're conscious of the external packages you're using which is my I tried to label this as a proposal since I honestly thought you'd reject the idea of dragging in all the cruft. Since it wasn't much work to actually implement Cobra I figured I might as well present the proposal as a full PR, code and all. And that doesn't even get into the option naming changes.

At any rate - thanks for this useful project and for giving me the opportunities to contribute. I'm surprised there's not more interest in this project - but I guess most people hunt up a QR generating web site rather than pull down an app to generate the codes.

@arcanericky arcanericky deleted the cobra branch January 5, 2022 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants