Skip to content

Commit 325e26a

Browse files
author
song
committed
support init command for init blog from server
1 parent 2d29394 commit 325e26a

File tree

8 files changed

+46
-16
lines changed

8 files changed

+46
-16
lines changed

.gitignore

100644100755
File mode changed.

README.md

100644100755
File mode changed.

build.go

100644100755
File mode changed.

build.yml

100644100755
File mode changed.

main.go

100644100755
Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"github.com/codegangsta/cli"
88
"github.com/go-fsnotify/fsnotify"
9-
"github.com/imeoer/bamboo-api/ink"
9+
"github.com/InkProject/ink.go"
1010
"gopkg.in/yaml.v2"
1111
"io"
1212
"io/ioutil"
@@ -19,7 +19,11 @@ import (
1919
"strings"
2020
)
2121

22-
const VERSION = "Beta (2015-04-12)"
22+
const (
23+
VERSION = "Beta (2015-04-28)"
24+
DEFAULT_PATH = "blog"
25+
DOWNLOAD_URL = "http://www.inkpaper.io/release/ink_blog.zip"
26+
)
2327

2428
var watcher *fsnotify.Watcher
2529
var globalConfig *GlobalConfig
@@ -44,7 +48,7 @@ func main() {
4448
Name: "preview",
4549
Usage: "Run in server mode to preview blog",
4650
Action: func(c *cli.Context) {
47-
ParseGlobalConfig(c, true)
51+
ParseGlobalConfigByCli(c, true)
4852
Build()
4953
Watch()
5054
Server()
@@ -54,7 +58,7 @@ func main() {
5458
Name: "publish",
5559
Usage: "Generate blog to public folder and publish",
5660
Action: func(c *cli.Context) {
57-
ParseGlobalConfig(c, false)
61+
ParseGlobalConfigByCli(c, false)
5862
Build()
5963
Publish()
6064
},
@@ -68,19 +72,41 @@ func main() {
6872
},
6973
}
7074
app.Action = func(c *cli.Context) {
71-
ParseGlobalConfig(c, false)
72-
Build()
75+
ParseGlobalConfig(".", false)
76+
if globalConfig == nil {
77+
ParseGlobalConfig(DEFAULT_PATH, false)
78+
if globalConfig == nil {
79+
Init(nil)
80+
ParseGlobalConfig(DEFAULT_PATH, true)
81+
}
82+
}
83+
if globalConfig != nil {
84+
Build()
85+
Watch()
86+
Server()
87+
}
7388
}
7489
app.Run(os.Args)
7590
}
7691

77-
func ParseGlobalConfig(c *cli.Context, develop bool) {
92+
func ParseGlobalConfigByCli(c *cli.Context, develop bool) {
7893
if len(c.Args()) > 0 {
7994
rootPath = c.Args()[0]
8095
} else {
8196
rootPath = "."
8297
}
98+
ParseGlobalConfig(rootPath, develop)
99+
if globalConfig == nil {
100+
Fatal("Parse config.yml failed, please specify a valid path")
101+
}
102+
}
103+
104+
func ParseGlobalConfig(root string, develop bool) {
105+
rootPath = root
83106
globalConfig = ParseConfig(filepath.Join(rootPath, "config.yml"))
107+
if globalConfig == nil {
108+
return
109+
}
84110
globalConfig.Develop = develop
85111
if develop {
86112
globalConfig.Site.Root = ""
@@ -94,8 +120,8 @@ func Server() {
94120
port = "8888"
95121
}
96122
app := ink.New()
97-
app.Get("*", ink.Static(rootPath+"/public"))
98-
app.Head("*", ink.Static(rootPath+"/public"))
123+
app.Get("*", ink.Static(filepath.Join(rootPath, "public")))
124+
app.Head("*", ink.Static(filepath.Join(rootPath, "public")))
99125
Log("Open http://localhost:" + port + "/ to preview")
100126
app.Listen("0.0.0.0:" + port)
101127
}
@@ -263,11 +289,15 @@ func (dn *Download) Read(p []byte) (int, error) {
263289
func Init(c *cli.Context) {
264290
// Parse arguments
265291
var directory string
266-
args := c.Args()
267-
if len(args) > 0 {
268-
directory = args[0]
292+
if c == nil {
293+
directory = DEFAULT_PATH
269294
} else {
270-
Fatal("Please specify a new blog directory name")
295+
args := c.Args()
296+
if len(args) > 0 {
297+
directory = args[0]
298+
} else {
299+
Fatal("Please specify a new blog directory name")
300+
}
271301
}
272302
// Create blog directory
273303
err := os.MkdirAll(directory, 0777)
@@ -282,7 +312,7 @@ func Init(c *cli.Context) {
282312
defer zipOut.Close()
283313
// Http get request for zip
284314
fmt.Printf("Connecting server to init blog\r")
285-
resp, err := http.Get("http://www.inkpaper.io/release/ink_blog.zip")
315+
resp, err := http.Get(DOWNLOAD_URL)
286316
if err != nil {
287317
Fatal(err.Error())
288318
}

parse.go

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"github.com/russross/blackfriday"
4+
"github.com/InkProject/blackfriday"
55
"gopkg.in/yaml.v2"
66
"html/template"
77
"io/ioutil"
@@ -81,7 +81,7 @@ func ParseConfig(configPath string) *GlobalConfig {
8181
// Read data from file
8282
data, err := ioutil.ReadFile(configPath)
8383
if err != nil {
84-
Fatal(err.Error())
84+
return nil
8585
}
8686
if err = yaml.Unmarshal(data, &config); err != nil {
8787
Fatal(err.Error())

render.go

100644100755
File mode changed.

util.go

100644100755
File mode changed.

0 commit comments

Comments
 (0)