Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee239bb

Browse files
authored
Merge pull request #44 from writeas/T672-accounts
add accounts command Resolves T672
2 parents de2ec24 + 5b1ae2c commit ee239bb

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

cmd/wf/commands.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package main
22

33
import (
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+
}

cmd/wf/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ func main() {
206206
Usage: "Make the operation more talkative",
207207
},
208208
},
209+
}, {
210+
Name: "accounts",
211+
Usage: "List all currently logged in accounts",
212+
Action: cmdAccounts,
213+
Flags: []cli.Flag{
214+
cli.BoolFlag{
215+
Name: "verbose, v",
216+
Usage: "Make the operation more talkative",
217+
},
218+
},
209219
}, {
210220
Name: "auth",
211221
Usage: "Authenticate with a WriteFreely instance",

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require (
55
github.com/atotto/clipboard v0.1.1
66
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
77
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
8+
github.com/hashicorp/go-multierror v1.0.0
89
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
910
github.com/jtolds/gls v4.2.1+incompatible // indirect
1011
github.com/microcosm-cc/bluemonday v1.0.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM
1212
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1313
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
1414
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
15+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
16+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
17+
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
18+
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
1519
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c h1:kQWxfPIHVLbgLzphqk3QUflDy9QdksZR4ygR807bpy0=
1620
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
1721
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=

0 commit comments

Comments
 (0)