add -profile flag

pull/28/head
Yasuhiro Matsumoto 2017-04-19 16:52:26 +09:00
parent b5541c9e38
commit 3d010df5fb
1 changed files with 30 additions and 12 deletions

View File

@ -90,7 +90,7 @@ func prompt() (string, string, error) {
return email, password, nil return email, password, nil
} }
func getConfig() (string, *mastodon.Config, error) { func getConfig(c *cli.Context) (string, *mastodon.Config, error) {
dir := os.Getenv("HOME") dir := os.Getenv("HOME")
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
dir = os.Getenv("APPDATA") dir = os.Getenv("APPDATA")
@ -104,7 +104,13 @@ func getConfig() (string, *mastodon.Config, error) {
if err := os.MkdirAll(dir, 0700); err != nil { if err := os.MkdirAll(dir, 0700); err != nil {
return "", nil, err return "", nil, err
} }
file := filepath.Join(dir, "settings.json") var file string
profile := c.String("profile")
if profile != "" {
file = filepath.Join(dir, "settings"+profile+".json")
} else {
file = filepath.Join(dir, "settings.json")
}
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return "", nil, err return "", nil, err
@ -164,6 +170,13 @@ func makeApp() *cli.App {
app.Name = "mstdn" app.Name = "mstdn"
app.Usage = "mastodon client" app.Usage = "mastodon client"
app.Version = "0.0.1" app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "a",
Usage: "profile name",
Value: "",
},
}
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "toot", Name: "toot",
@ -242,23 +255,28 @@ func makeApp() *cli.App {
Action: cmdUpload, Action: cmdUpload,
}, },
} }
app.Setup()
return app return app
} }
func run() int { func run() int {
app := makeApp() app := makeApp()
file, config, err := getConfig() app.Before = func(c *cli.Context) error {
fatalIf(err) file, config, err := getConfig(c)
if err != nil {
return err
}
client := mastodon.NewClient(config) client := mastodon.NewClient(config)
if config.AccessToken == "" { if config.AccessToken == "" {
err = authenticate(client, config, file) return authenticate(client, config, file)
fatalIf(err) }
} app.Metadata = map[string]interface{}{
app.Metadata = map[string]interface{}{ "client": client,
"client": client, "config": config,
"config": config, }
return nil
} }
app.Run(os.Args) app.Run(os.Args)