Skip to content

Commit

Permalink
Merge branch 'tracing-post-id'
Browse files Browse the repository at this point in the history
  • Loading branch information
minodisk committed Feb 4, 2016
2 parents 6c11e79 + 53ab436 commit 19b7f65
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 304 deletions.
196 changes: 196 additions & 0 deletions cli/app.go
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)
}
}
26 changes: 26 additions & 0 deletions cli/app_test.go
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)
}
}
15 changes: 0 additions & 15 deletions command/error.go

This file was deleted.

13 changes: 4 additions & 9 deletions command/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ package command

import (
"github.com/codegangsta/cli"
"github.com/minodisk/qiitactl/api"
"github.com/minodisk/qiitactl/model"
)

func CmdGenerateFile(c *cli.Context) {
err := GenerateFile(c.String("team"), c.String("title"))
if err != nil {
printError(c, err)
}
}
func GenerateFile(c *cli.Context, client api.Client) (err error) {
teamID := c.String("team")
title := c.String("title")

func GenerateFile(teamID string, title string) (err error) {
var team *model.Team
if teamID != "" {
team.ID = teamID
}

post := model.NewPost(title, nil, team)

err = post.Save()
return
}
25 changes: 14 additions & 11 deletions command/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"testing"
"time"

"github.com/minodisk/qiitactl/command"
"github.com/minodisk/qiitactl/cli"
"github.com/minodisk/qiitactl/model"
"github.com/minodisk/qiitactl/testutil"
)

func TestGenerateFile(t *testing.T) {
defer func() {
os.RemoveAll(model.DirMine)
}()
testutil.CleanUp()
defer testutil.CleanUp()

err := command.GenerateFile("", "Example Title")
app := cli.GenerateApp(client, os.Stdout)
err := app.Run([]string{"qiitactl", "generate", "file", "-t", "Example Title"})
if err != nil {
t.Fatal(err)
}
Expand All @@ -37,13 +38,15 @@ func TestGenerateFile(t *testing.T) {
}

func TestGenerateUniqueFile(t *testing.T) {
defer func() {
os.RemoveAll(model.DirMine)
}()
testutil.CleanUp()
defer testutil.CleanUp()

var err error
err = command.GenerateFile("", "Example Title")
err = command.GenerateFile("", "Example Title")
app := cli.GenerateApp(client, os.Stdout)
err := app.Run([]string{"qiitactl", "generate", "file", "-t", "Example Title"})
err = app.Run([]string{"qiitactl", "generate", "file", "-t", "Example Title"})
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 19b7f65

Please sign in to comment.