@@ -2,10 +2,13 @@ package main
22
33import (
44 "fmt"
5+ "io/ioutil"
56 "os"
67 "path/filepath"
78 "strings"
9+ "text/tabwriter"
810
11+ "github.com/hashicorp/go-multierror"
912 "github.com/writeas/writeas-cli/api"
1013 "github.com/writeas/writeas-cli/commands"
1114 "github.com/writeas/writeas-cli/config"
@@ -196,3 +199,78 @@ func cmdLogOut(c *cli.Context) error {
196199
197200 return nil
198201}
202+
203+ func cmdAccounts (c * cli.Context ) error {
204+ // get user config dir
205+ userDir := config .UserDataDir (c .App .ExtraInfo ()["configDir" ])
206+ // load defaults
207+ cfg , err := config .LoadConfig (userDir )
208+ if err != nil {
209+ return cli .NewExitError ("Could not load default user configuration" , 1 )
210+ }
211+ defaultUser := cfg .Default .User
212+ defaultHost := cfg .Default .Host
213+ if parts := strings .Split (defaultHost , "://" ); len (parts ) > 1 {
214+ defaultHost = parts [1 ]
215+ }
216+ // get each host dir
217+ files , err := ioutil .ReadDir (userDir )
218+ if err != nil {
219+ return cli .NewExitError ("Could not read user configuration directory" , 1 )
220+ }
221+ // accounts will be a slice of slices of string. the first string in
222+ // a subslice should always be the hostname
223+ accounts := [][]string {}
224+ for _ , file := range files {
225+ if file .IsDir () {
226+ dirName := file .Name ()
227+ // get each user in host dir
228+ users , err := usersFromDir (filepath .Join (userDir , dirName ))
229+ if err != nil {
230+ log .Info (c , "Failed to get users from %s: %v" , dirName , err )
231+ continue
232+ }
233+ if len (users ) != 0 {
234+ // append the slice of users as a new slice in accounts w/ the host prepended
235+ accounts = append (accounts , append ([]string {dirName }, users ... ))
236+ }
237+ }
238+ }
239+
240+ // print out all logged in accounts
241+ tw := tabwriter .NewWriter (os .Stdout , 10 , 2 , 2 , ' ' , tabwriter .TabIndent )
242+ if len (accounts ) == 0 && (c .Bool ("v" ) || c .Bool ("verbose" ) || c .GlobalBool ("v" ) || c .GlobalBool ("verbose" )) {
243+ fmt .Fprintf (tw , "%s\t " , "No authenticated accounts found.\n " )
244+ }
245+ for _ , userList := range accounts {
246+ host := userList [0 ]
247+ for _ , username := range userList [1 :] {
248+ if host == defaultHost && username == defaultUser {
249+ fmt .Fprintf (tw , "[%s]\t %s (default)\n " , host , username )
250+ continue
251+ }
252+ fmt .Fprintf (tw , "[%s]\t %s\n " , host , username )
253+ }
254+ }
255+ return tw .Flush ()
256+ }
257+
258+ func usersFromDir (path string ) ([]string , error ) {
259+ users := make ([]string , 0 , 4 )
260+ files , err := ioutil .ReadDir (path )
261+ if err != nil {
262+ return nil , err
263+ }
264+ var errs error
265+ for _ , file := range files {
266+ if file .IsDir () {
267+ _ , err := os .Stat (filepath .Join (path , file .Name (), "user.json" ))
268+ if err != nil {
269+ err = multierror .Append (errs , err )
270+ continue
271+ }
272+ users = append (users , file .Name ())
273+ }
274+ }
275+ return users , errs
276+ }
0 commit comments