-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(accounts): list available accounts (#31)
- Loading branch information
1 parent
1ea657c
commit 6d4823d
Showing
4 changed files
with
141 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,31 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// analysisCmd represents the analysis command | ||
var accountsCmd = &cobra.Command{ | ||
Use: "accounts", | ||
Short: "commands for interacting with the accounts API", | ||
Long: ``, | ||
} | ||
|
||
func init() { | ||
RootCmd().AddCommand(accountsCmd) | ||
} |
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,76 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/armory-io/kayentactl/pkg/kayenta" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var types string | ||
|
||
var shorthandMap = map[string]string{ | ||
"metrics": "METRICS_STORE", | ||
"config": "CONFIGURATION_STORE", | ||
"object": "OBJECT_STORE", | ||
} | ||
|
||
// getCmd represents the get command | ||
var accountListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
kc := kayenta.NewDefaultClient(kayenta.ClientBaseURL(kayentaURL)) | ||
credentials, err := kc.GetCredentials() | ||
if err != nil { | ||
log.Fatalf("could not get list of available accounts: %s", err.Error()) | ||
} | ||
|
||
// TODO: add type filtering | ||
|
||
var ( | ||
output []byte | ||
) | ||
switch outFormat { | ||
case "json": | ||
output, err = outputJson(credentials) | ||
case "pretty": | ||
// TODO: implement a pretty output instead of just falling back to json | ||
output, err = outputPretty(credentials) | ||
} | ||
|
||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
|
||
fmt.Fprint(os.Stdout, string(output)) | ||
}, | ||
} | ||
|
||
func outputJson(creds []kayenta.AccountCredential) ([]byte, error) { | ||
return json.MarshalIndent(creds, "", " ") | ||
} | ||
|
||
func outputPretty(creds []kayenta.AccountCredential) ([]byte, error) { | ||
return outputJson(creds) | ||
} | ||
|
||
func init() { | ||
accountsCmd.AddCommand(accountListCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// getCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
getCmd.Flags().StringVarP(&types, "types", "t", "", "filter by account type") | ||
|
||
} |
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