Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10669730
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Subscribers
None
View Options
diff --git a/cmd/wf/commands.go b/cmd/wf/commands.go
index ee8152d..c252e6a 100644
--- a/cmd/wf/commands.go
+++ b/cmd/wf/commands.go
@@ -1,92 +1,121 @@
package main
import (
"fmt"
+ "os"
+ "path/filepath"
"github.com/writeas/writeas-cli/api"
"github.com/writeas/writeas-cli/commands"
"github.com/writeas/writeas-cli/config"
"github.com/writeas/writeas-cli/executable"
"github.com/writeas/writeas-cli/log"
cli "gopkg.in/urfave/cli.v1"
)
func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
return func(c *cli.Context) error {
u, err := config.LoadUser(c)
if err != nil {
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
}
if u == nil {
return cli.NewExitError("You must be authenticated to "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
}
return f(c)
}
}
+// usersLoggedIn checks for logged in users for the set host flag
+// it returns the number of users and a slice of usernames
+func usersLoggedIn(c *cli.Context) (int, []string, error) {
+ path, err := config.UserHostDir(c)
+ if err != nil {
+ return 0, nil, err
+ }
+ dir, err := os.Open(path)
+ if err != nil {
+ return 0, nil, err
+ }
+ contents, err := dir.Readdir(0)
+ if err != nil {
+ return 0, nil, err
+ }
+ var names []string
+ for _, file := range contents {
+ if file.IsDir() {
+ // stat user.json
+ if _, err := os.Stat(filepath.Join(path, file.Name(), "user.json")); err == nil {
+ names = append(names, file.Name())
+ }
+ }
+ }
+ return len(names), names, nil
+}
+
func cmdAuth(c *cli.Context) error {
err := commands.CmdAuth(c)
if err != nil {
return err
}
// Get the username from the command, just like commands.CmdAuth does
username := c.Args().Get(0)
// Update config if this is user's first auth
cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if err != nil {
log.Errorln("Not saving config. Unable to load config: %s", err)
return err
}
if cfg.Default.Host == "" && cfg.Default.User == "" {
// This is user's first auth, so save defaults
cfg.Default.Host = api.HostURL(c)
cfg.Default.User = username
err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
if err != nil {
log.Errorln("Not saving config. Unable to save config: %s", err)
return err
}
fmt.Printf("Set %s on %s as default account.\n", username, c.GlobalString("host"))
}
return nil
}
func cmdLogOut(c *cli.Context) error {
err := commands.CmdLogOut(c)
if err != nil {
return err
}
// Remove this from config if it's the default account
cfg, err := config.LoadConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if err != nil {
log.Errorln("Not updating config. Unable to load: %s", err)
return err
}
username, err := config.CurrentUser(c)
if err != nil {
log.Errorln("Not updating config. Unable to load current user: %s", err)
return err
}
reqHost := api.HostURL(c)
if reqHost == "" {
// No --host given, so we're using the default host
reqHost = cfg.Default.Host
}
if cfg.Default.Host == reqHost && cfg.Default.User == username {
// We're logging out of default username + host, so remove from config file
cfg.Default.Host = ""
cfg.Default.User = ""
err = config.SaveConfig(config.UserDataDir(c.App.ExtraInfo()["configDir"]), cfg)
if err != nil {
log.Errorln("Not updating config. Unable to save config: %s", err)
return err
}
}
return nil
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, May 16, 4:30 PM (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3240211
Attached To
rWCLI writeas-cli
Event Timeline
Log In to Comment