1// SPDX-License-Identifier: ISC 2// Copyright (c) 2014-2020 Bitmark Inc. 3// Use of this source code is governed by an ISC 4// license that can be found in the LICENSE file. 5 6package main 7 8import ( 9 "fmt" 10 "io" 11 "sort" 12 13 "github.com/urfave/cli" 14) 15 16func runList(c *cli.Context) error { 17 18 m := c.App.Metadata["config"].(*metadata) 19 20 if c.Bool("connections") { 21 return listConnections(m.w, m.config.Connections, c.Bool("json")) 22 } 23 24 // normal list identities 25 identities := m.config.Identities 26 27 names := make([]string, len(identities)) 28 i := 0 29 for name := range identities { 30 names[i] = name 31 i += 1 32 } 33 sort.Strings(names) 34 35 if c.Bool("json") { 36 type item struct { 37 HasSecret bool `json:"hasSecretKey"` 38 Name string `json:"name"` 39 Account string `json:"account"` 40 Description string `json:"description"` 41 } 42 jsonData := make([]item, len(names)) 43 44 for i, name := range names { 45 jsonData[i].HasSecret = len(identities[name].Salt) > 0 46 jsonData[i].Name = name 47 jsonData[i].Account = identities[name].Account 48 jsonData[i].Description = identities[name].Description 49 } 50 51 printJson(m.w, jsonData) 52 53 } else { 54 for _, name := range names { 55 flag := "--" 56 if len(identities[name].Salt) > 0 { 57 flag = "SK" 58 } 59 fmt.Fprintf(m.w, "%s %-20s %s %q\n", flag, name, identities[name].Account, identities[name].Description) 60 } 61 } 62 63 return nil 64} 65 66func listConnections(handle io.Writer, connections []string, printJSON bool) error { 67 if printJSON { 68 printJson(handle, connections) 69 } else { 70 for i, conn := range connections { 71 fmt.Fprintf(handle, "%4d %s\n", i, conn) 72 } 73 } 74 75 return nil 76} 77