-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: Add ability to list all profiles (#29)
Creates a `list` command which will allow you to list all configured profiles. At the moment, the output is only profile name, auth mechanism and the email associated (for API keys). This can be extended in the future as more information is needed. Example output ``` PROFILE NAME AUTHENTICATION TYPE EMAIL test-api-key api_key [email protected] test-api-token api_token ``` Closes #8
- Loading branch information
1 parent
2202aca
commit f759943
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/pelletier/go-toml" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List all available profiles", | ||
Long: "", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if verbose { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
log.Fatal("unable to find home directory: ", err) | ||
} | ||
|
||
configData, err := ioutil.ReadFile(home + defaultFullConfigPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
config := tomlConfig{} | ||
err = toml.Unmarshal(configData, &config) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if len(config.Profiles) == 0 { | ||
fmt.Printf("no profiles found at %s\n", home+defaultFullConfigPath) | ||
os.Exit(0) | ||
} | ||
|
||
tableData := [][]string{} | ||
for profileName, profile := range config.Profiles { | ||
// Only display the email if we're using API tokens otherwise the value is | ||
// not used and pretty superfluous. | ||
var emailString string | ||
if profile.AuthType == "api_key" { | ||
emailString = profile.Email | ||
} | ||
|
||
tableData = append(tableData, []string{ | ||
profileName, | ||
profile.AuthType, | ||
emailString, | ||
}) | ||
} | ||
|
||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetHeader([]string{"Profile name", "Authentication type", "Email"}) | ||
table.SetAutoWrapText(false) | ||
table.SetAutoFormatHeaders(true) | ||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetCenterSeparator("") | ||
table.SetColumnSeparator("") | ||
table.SetRowSeparator("") | ||
table.SetHeaderLine(false) | ||
table.SetBorder(false) | ||
table.SetTablePadding("\t") | ||
table.SetNoWhiteSpace(true) | ||
table.AppendBulk(tableData) | ||
table.Render() | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters