Skip to content

Commit

Permalink
Merge pull request #119 from packethost/env
Browse files Browse the repository at this point in the history
adds METAL_AUTH_TOKEN variable and env command
  • Loading branch information
displague authored May 20, 2021
2 parents 1797a48 + e163703 commit 1bdb63e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
7 changes: 4 additions & 3 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ var (
)

const (
consumerToken = "Equinix Metal CLI"
apiTokenEnvVar = "PACKET_TOKEN"
apiURL = "https://api.equinix.com/metal/v1/"
consumerToken = "Equinix Metal CLI"
apiTokenEnvVar = "METAL_AUTH_TOKEN"
legacyApiTokenEnvVar = "PACKET_TOKEN"
apiURL = "https://api.equinix.com/metal/v1/"
)

// NewCli struct
Expand Down
61 changes: 61 additions & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright © 2020 Equinix Metal 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 (
"fmt"

"github.com/spf13/cobra"
)

// envCmd represents a command that, when run, generates a
// set of environment variables, for use in shell environments
var envCmd = &cobra.Command{
Use: "env",
Short: "Generate environment variables",
Long: fmt.Sprintf(`Currently emitted variables:
- %s
To load environment variables:
Bash, Zsh:
$ source <(packet env)
Bash (3.2.x):
$ eval "$(packet env)"
Fish:
$ packet env | source
`, apiTokenEnvVar),
DisableFlagsInUseLine: true,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s=%s\n", apiTokenEnvVar, packetToken)
},
}

func init() {
rootCmd.AddCommand(envCmd)
}
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var rootCmd = &cobra.Command{

func apiConnect(cmd *cobra.Command, args []string) error {
if packetToken == "" {
return fmt.Errorf("Equinix Metal authentication token not provided. Please either set the 'PACKET_TOKEN' environment variable or create a JSON or YAML configuration file.")
return fmt.Errorf("Equinix Metal authentication token not provided. Please set the 'METAL_AUTH_TOKEN' or 'PACKET_TOKEN' environment variable or create a JSON or YAML configuration file.")
}
client, err := packngo.NewClientWithBaseURL(consumerToken, packetToken, nil, apiURL)
if err != nil {
Expand All @@ -67,7 +67,11 @@ func apiConnect(cmd *cobra.Command, args []string) error {
}

func apiToken() string {
return os.Getenv(apiTokenEnvVar)
token := os.Getenv(apiTokenEnvVar)
if token != "" {
return token
}
return os.Getenv(legacyApiTokenEnvVar)
}

func init() {
Expand Down

0 comments on commit 1bdb63e

Please sign in to comment.