Skip to content

Commit

Permalink
Implement --host, --port
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 7, 2017
1 parent 1642271 commit a71e664
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Update to the latest version:

```bash
gojekyll build # builds the site in the current directory into _site
gojekyll serve # serve the app at http://localhost:8080
gojekyll serve # serve the app at http://localhost:4000
gojekyll help
gojekyll help build
```
Expand Down
28 changes: 22 additions & 6 deletions cmd/gojekyll/helpers.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package main

import (
"strconv"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)

func boolVar(name string, ptr **bool) kingpin.Action {
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
option := false
if value != nil && *value == "true" {
option = true
if value != nil {
arg := *value == "true"
*ptr = &arg
}
return nil
}
}

func intVar(name string, ptr **int) kingpin.Action {
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
if value != nil {
n, err := strconv.Atoi(*value)
if err != nil {
panic(err)
}
arg := int(n)
*ptr = &arg
}
*ptr = &option
return nil
}
}
Expand All @@ -20,8 +36,8 @@ func stringVar(name string, ptr **string) kingpin.Action {
return func(pc *kingpin.ParseContext) error {
value := lookupKingpinValue(name, pc)
if value != nil {
copy := *value
*ptr = &copy
arg := *value
*ptr = &arg
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/gojekyll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (

var (
app = kingpin.New("gojekyll", "a (somewhat) Jekyll-compatible blog generator")
source = app.Flag("source", "Source directory").Short('s').Default(".").String()
source = app.Flag("source", "Source directory").Short('s').Default(".").ExistingDir()
_ = app.Flag("destination", "Destination directory").Short('d').Action(stringVar("destination", &configFlags.Destination)).String()
_ = app.Flag("drafts", "Render posts in the _drafts folder").Short('D').Action(boolVar("drafts", &configFlags.Drafts)).Bool()
_ = app.Flag("future", "Publishes posts with a future date").Action(boolVar("future", &configFlags.Future)).Bool()
Expand All @@ -43,6 +43,8 @@ var (

serve = app.Command("serve", "Serve your site locally").Alias("server").Alias("s")
open = serve.Flag("open-url", "Launch your site in a browser").Short('o').Bool()
_ = app.Flag("host", "Host to bind to").Short('H').Action(stringVar("host", &configFlags.Host)).String()
_ = serve.Flag("port", "Port to listen on").Short('P').Action(intVar("port", &configFlags.Port)).Int()

variables = app.Command("variables", "Display a file or URL path's variables").Alias("v").Alias("var").Alias("vars")
variablePath = variables.Arg("PATH", "Path, URL, site, or site...").String()
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
ExcerptSeparator string `yaml:"excerpt_separator"`

// Serving
Host string
Port int
AbsoluteURL string `yaml:"url"`
BaseURL string

Expand Down
6 changes: 6 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ plugins: []
# Conversion
excerpt_separator: "\n\n"
# Serving
detach: false
port: 4000
host: 127.0.0.1
baseurl: "" # does not include hostname
# Outputting
permalink: date
paginate_path: /page:num
Expand Down
13 changes: 9 additions & 4 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package config
// Flags are applied after the configuration file is loaded.
// They are pointers to represent optional types, to tell whether they have been set.
type Flags struct {
Destination *string
Unpublished *bool
Drafts *bool
Future *bool
Destination, Host *string
Drafts, Future, Unpublished *bool
Port *int
}

// ApplyFlags overwrites the configuration with values from flags.
Expand All @@ -20,6 +19,12 @@ func (c *Config) ApplyFlags(flags Flags) {
if flags.Future != nil {
c.Future = *flags.Future
}
if flags.Host != nil {
c.Host = *flags.Host
}
if flags.Port != nil {
c.Port = *flags.Port
}
if flags.Unpublished != nil {
c.Unpublished = *flags.Unpublished
}
Expand Down
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type Server struct {

// Run runs the server.
func (s *Server) Run(open bool, logger func(label, value string)) error {
cfg := s.Site.Config()
s.Site.SetAbsoluteURL("")
address := "localhost:4000"
address := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
logger("Server address:", "http://"+address+"/")
if err := s.StartLiveReloader(); err != nil {
return err
Expand Down

0 comments on commit a71e664

Please sign in to comment.