Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds shell completions and custom user-agent #60

Merged
merged 4 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ install:

generate-docs: clean-docs
mkdir -p docs
GENDOCS=true go run main.go
go run main.go docs ./docs

test:
go test ./tests
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Linux](#linux)
* [Mac OS X](#mac-os-x)
* [Windows](#windows)
* [Shell Completion](#shell-completion)
* [Authentication](#authentication)
* [Reference](#reference)
* [Example Syntax](#example-syntax)
Expand Down Expand Up @@ -76,6 +77,12 @@ The path can be viewed by running:
echo $env:Path
```

### Shell Completion

Once installed, shell completion can be enabled (in Bash) with `source <(packet completion bash)` (or for some versions of Bash, `eval "$(packet completion bash)").

Check `packet completion -h` for instructions to use in other shells.

## Authentication

The Packet authentication token can be stored in the `$PACKET_TOKEN` environment variable or in JSON or YAML configuration files. The configuration file path can be overridden with the `--config` flag.
Expand Down Expand Up @@ -136,7 +143,7 @@ Use "packet [command] --help" for more information about a command.

## Reference

The full CLI documentation can be found [here](docs/packet.md) or Daby clicking the links below.
The full CLI documentation can be found [here](docs/packet.md) or by clicking the links below.

* [Device operations](docs/packet_device.md)
* [Facility operations](docs/packet_facilities.md)
Expand Down Expand Up @@ -201,6 +208,5 @@ Details on all available commands can be found by visiting the reference [pages]

For help with this package:

* Open up a GitHub issue [here](https://github.com/fog/fog-packet/issues).
* Open up a GitHub issue [here](https://github.com/packethost/packet-cli/issues).
* Contact the [Packet Community Slack](http://slack.packet.net) or on Freenode IRC in the #packethost channel.
* Search the [Packet Help Center](http://help.packet.net/).
85 changes: 85 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2020 Packet, an Equinix Company Developers <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// completionCmd represents the completion command that, when run, generates a
// bash or zsh completion script for the CLI
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: `To load completions:

Bash:

$ source <(packet completion bash)

Bash (3.2.x):

$ eval "$(packet completion bash)"

# To load completions for each session, execute once:
Linux:
$ packet completion bash > /etc/bash_completion.d/packet-cli
MacOS:
$ packet completion bash > /usr/local/etc/bash_completion.d/packet-cli

Zsh:

$ source <(packet completion zsh)

# To load completions for each session, execute once:
$ packet completion zsh > "${fpath[1]}/_packet-cli"

Fish:

$ packet completion fish | source

# To load completions for each session, execute once:
$ packet completion fish > ~/.config/fish/completions/packet-cli.fish
`,
DisableFlagsInUseLine: true,
Hidden: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}

func init() {
rootCmd.AddCommand(completionCmd)
}
2 changes: 1 addition & 1 deletion cmd/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// deviceCmd represents the device command
var deviceCmd = &cobra.Command{
Use: "device",
Aliases: []string{"server"},
Aliases: []string{"server", "servers", "devices"},
Short: "Device operations",
Long: `Device operations: create, delete, update, start/stop, reboot and get.`,
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2020 Packet, an Equinix Company Developers <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

// docsCmd represents the docs command
var docsCmd = &cobra.Command{
Use: "docs [DESTINATION]",
Short: "Generate command documentation",
Long: "To generate documentation in the ./docs directory: docs ./docs",
DisableFlagsInUseLine: true,
Hidden: true,
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dest := args[0]
return doc.GenMarkdownTree(cmd.Parent(), dest)
},
}

func init() {
rootCmd.AddCommand(docsCmd)
}
7 changes: 4 additions & 3 deletions cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// eventsCmd represents the events command
var eventCmd = &cobra.Command{
Use: "event",
Short: "Events operations",
Long: `Events operations: get`,
Use: "event",
Aliases: []string{"events"},
Short: "Events operations",
Long: `Events operations: get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/facility.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// facilityCmd represents the facility command
var facilityCmd = &cobra.Command{
Use: "facilities",
Short: "Facility operations",
Long: `Facility operations: get`,
Use: "facilities",
Aliases: []string{"facility"},
Short: "Facility operations",
Long: `Facility operations: get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/hardware_reservations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// hardwareReservationsCmd represents the hardwareReservations command
var hardwareReservationsCmd = &cobra.Command{
Use: "hardware-reservation",
Short: "Hardware reservation operations",
Long: `Hardware reservation operations: get, move`,
Use: "hardware-reservation",
Aliases: []string{"hardware-reservations"},
Short: "Hardware reservation operations",
Long: `Hardware reservation operations: get, move`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// ipCmd represents the ip command
var ipCmd = &cobra.Command{
Use: "ip",
Short: "IP operations",
Long: `IP address, reservations and assignment operations: assign, unassign, remove, available, request and get `,
Use: "ip",
Aliases: []string{"ips", "ip-addresses", "ip-address"},
Short: "IP operations",
Long: `IP address, reservations and assignment operations: assign, unassign, remove, available, request and get `,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/operating_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// operatingSystemCmd represents the operatingSystem command
var operatingSystemCmd = &cobra.Command{
Use: "operating-systems",
Short: "Operating system operations",
Long: `Operating system operations: get`,
Use: "operating-systems",
Aliases: []string{"os"},
Short: "Operating system operations",
Long: `Operating system operations: get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// organizationCmd represents the organization command
var organizationCmd = &cobra.Command{
Use: "organization",
Short: "Organization operations",
Long: `Organization operations: create, update, delete and get`,
Use: "organization",
Aliases: []string{"organizations", "org", "orgs"},
Short: "Organization operations",
Long: `Organization operations: create, update, delete and get`,
}

func init() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/payment_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (

// paymentMethodsCmd represents the paymentMethods command
var paymentMethodsCmd = &cobra.Command{
Use: "payment-methods",
Short: "Retrieves a list of payment methods for the organization",
Use: "payment-methods",
Aliases: []string{"payment-method"},
Short: "Retrieves a list of payment methods for the organization",
Long: `Example:

packet organization get payment-methods --id [organization_UUID]
Expand Down
7 changes: 4 additions & 3 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// planCmd represents the plan command
var planCmd = &cobra.Command{
Use: "plan",
Short: "Plan operations",
Long: `Plan operations: get`,
Use: "plan",
Aliases: []string{"plans"},
Short: "Plan operations",
Long: `Plan operations: get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// projectCmd represents the project command
var projectCmd = &cobra.Command{
Use: "project",
Short: "Project operations",
Long: `Project operations: create, delete, update and get`,
Use: "project",
Aliases: []string{"projects"},
Short: "Project operations",
Long: `Project operations: create, delete, update and get`,
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func packetConnect(cmd *cobra.Command, args []string) {
fmt.Println("Client error:", err)
return
}

client.UserAgent = fmt.Sprintf("packet-cli/%s %s", Version, client.UserAgent)
PacknGo = *client
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/twofa.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// 2faCmd represents the 2fa command
var twofaCmd = &cobra.Command{
Use: "2fa",
Short: "Two Factor Authentication operations",
Long: `Two Factor Authentication operations: enable, disable, receive`,
Use: "2fa",
Aliases: []string{"tfa", "mfa", "totp"},
Short: "Two Factor Authentication operations",
Long: `Two Factor Authentication operations: enable, disable, receive`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// userCmd represents the user command
var userCmd = &cobra.Command{
Use: "user",
Short: "User operations",
Long: `User operations: get`,
Use: "user",
Aliases: []string{"users"},
Short: "User operations",
Long: `User operations: get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// virtualNetworkCmd represents the virtualNetwork command
var virtualNetworkCmd = &cobra.Command{
Use: "virtual-network",
Short: "Virtual network operations",
Long: `Virtual network operations: create, delete and get`,
Use: "virtual-network",
Aliases: []string{"vlan", "vlans"},
Short: "Virtual network operations",
Long: `Virtual network operations: create, delete and get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// volumeCmd represents the volume command
var volumeCmd = &cobra.Command{
Use: "volume",
Short: "Volume operations",
Long: `Volume operations: create, delete, attach, detach and get`,
Use: "volume",
Aliases: []string{"volumes"},
Short: "Volume operations",
Long: `Volume operations: create, delete, attach, detach and get`,
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

// enableVpnCmd represents the enableVpn command
var vpnCmd = &cobra.Command{
Use: "vpn",
Short: "VPN service operations",
Long: `VPN service operations: enable, disable, get`,
Use: "vpn",
Aliases: []string{"vpns"},
Short: "VPN service operations",
Long: `VPN service operations: enable, disable, get`,
}

func init() {
Expand Down
Loading