-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
332 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/codegangsta/cli" | ||
"github.com/minodisk/qiitactl/api" | ||
"github.com/minodisk/qiitactl/command" | ||
"github.com/minodisk/qiitactl/info" | ||
) | ||
|
||
func GenerateApp(client api.Client, w io.Writer) (app *cli.App) { | ||
app = cli.NewApp() | ||
app.Name = info.Name | ||
app.Version = info.Version | ||
app.Author = info.Author | ||
app.Usage = "Controls the Qiita posts" | ||
app.Flags = []cli.Flag{ | ||
cli.BoolFlag{ | ||
Name: "debug, d", | ||
Usage: "Panic when error occurs", | ||
}, | ||
} | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "generate", | ||
Usage: "Generate something in your local", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "file", | ||
Usage: "Generate a new markdown file for a new post", | ||
Action: partialize(command.GenerateFile, client), | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "title, t", | ||
Usage: "The title of a new post", | ||
}, | ||
cli.StringFlag{ | ||
Name: "team, T", | ||
Usage: "The name of a team, when you post to the team", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "show", | ||
Usage: "Display resources", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "post", | ||
Usage: "Print detail of a post in Qitta", | ||
Action: partializeWithWriter(command.ShowPost, client, w), | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "id, i", | ||
Usage: "The ID of the post to be printed detail", | ||
}, | ||
cli.StringFlag{ | ||
Name: "filename, f", | ||
Usage: "The filename of the post to be created", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "posts", | ||
Usage: "Print a list of posts in Qiita", | ||
Action: partializeWithWriter(command.ShowPosts, client, w), | ||
Flags: []cli.Flag{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "fetch", | ||
Usage: "Download resources from Qiita to current working directory", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "post", | ||
Usage: "Download a post as a file", | ||
Action: partialize(command.FetchPost, client), | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "id, i", | ||
Usage: "The ID of the post to be downloaded", | ||
}, | ||
cli.StringFlag{ | ||
Name: "filename, f", | ||
Usage: "The filename of the post to be created", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "posts", | ||
Usage: "Download posts as files", | ||
Action: partialize(command.FetchPosts, client), | ||
Flags: []cli.Flag{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "create", | ||
Usage: "Create resources from current working directory to Qiita", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "post", | ||
Usage: "Create a post", | ||
Action: partialize(command.CreatePost, client), | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "filename, f", | ||
Usage: "The filename of the post to be created", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "tweet, t", | ||
Usage: "Tweet the post", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "gist, g", | ||
Usage: "Create codes in the post to GitHub Gist", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "update", | ||
Usage: "Update resources from current working directory to Qiita", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "post", | ||
Usage: "Update a post", | ||
Action: partialize(command.UpdatePost, client), | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "filename, f", | ||
Usage: "The filename of the post to be updated", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "delete", | ||
Usage: "Delete resources from Qiita", | ||
Flags: []cli.Flag{}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "post", | ||
Usage: "Delete a post", | ||
Action: partialize(command.DeletePost, client), | ||
Flags: []cli.Flag{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
app.CommandNotFound = commandNotFound | ||
return | ||
} | ||
|
||
func commandNotFound(c *cli.Context, command string) { | ||
fmt.Fprintf(os.Stderr, "%s: '%s' is not a %s command. See '%s --help'.", c.App.Name, command, c.App.Name, c.App.Name) | ||
os.Exit(2) | ||
} | ||
|
||
func partialize(cmdFunc func(*cli.Context, api.Client) error, client api.Client) func(ctx *cli.Context) { | ||
return func(ctx *cli.Context) { | ||
err := cmdFunc(ctx, client) | ||
if err != nil { | ||
printError(ctx, err) | ||
} | ||
} | ||
} | ||
|
||
func partializeWithWriter(cmdFunc func(*cli.Context, api.Client, io.Writer) error, client api.Client, w io.Writer) func(ctx *cli.Context) { | ||
return func(ctx *cli.Context) { | ||
err := cmdFunc(ctx, client, w) | ||
if err != nil { | ||
printError(ctx, err) | ||
} | ||
} | ||
} | ||
|
||
func printError(ctx *cli.Context, err error) { | ||
if ctx.GlobalBool("debug") { | ||
panic(err) | ||
} else { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cli_test | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/minodisk/qiitactl/api" | ||
"github.com/minodisk/qiitactl/cli" | ||
) | ||
|
||
func TestNewApp(t *testing.T) { | ||
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
client, err := api.NewClient(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
app := cli.GenerateApp(client, os.Stdout) | ||
err = app.Run([]string{"qiitactl"}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.