Skip to content

Commit ebf5f40

Browse files
authored
Merge pull request #15 from chclaus/issue-14-open-browser-on-server-startup
Add 'open' flag to browser command
2 parents c1ca4a0 + 88ecc7d commit ebf5f40

File tree

9 files changed

+86
-14
lines changed

9 files changed

+86
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Escapes and unescapes HTML
126126
Starts a simple web server to serve static content. You can specify
127127
hostname and port and must set a folder to serve.
128128

129+
You can also pass an option `-o` to open your default system browser
130+
that points automatically to the served url.
131+
129132
## Configuration
130133
You can configure a some default behaviors of dt to fit your needs.
131134
Just place a file `.dt.yaml` into your home directory, you can configure
@@ -135,6 +138,7 @@ the following options:
135138
server:
136139
port: 3001
137140
address: 127.0.0.1
141+
openBrowser: true
138142
uuid:
139143
namespace: cacae610-c76a-4736-90ef-0271126b4346
140144
version: 4

cmd/server/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"github.com/chclaus/dt/cmd"
2727
"github.com/chclaus/dt/config"
28+
"github.com/chclaus/dt/utils"
2829
"github.com/spf13/cobra"
2930
"github.com/spf13/viper"
3031
"net/http"
@@ -57,8 +58,11 @@ var serverCmd = &cobra.Command{
5758
os.Exit(1)
5859
}
5960

60-
fmt.Printf("Serving %s on %s\n", path, addr)
61+
if srv.OpenBrowser {
62+
utils.Open("http://" + addr)
63+
}
6164

65+
fmt.Printf("Serving %s on %s\n", path, addr)
6266
http.ListenAndServe(addr, nil)
6367
},
6468
Example: ``,
@@ -71,6 +75,8 @@ func init() {
7175

7276
serverCmd.Flags().StringP("address", "a", "0.0.0.0", "the hostname or ip address")
7377
serverCmd.Flags().StringP("port", "p", "3000", "the listening port")
78+
serverCmd.Flags().BoolP("open", "o", false, "open a browser window")
7479
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port"))
7580
viper.BindPFlag("server.address", serverCmd.Flags().Lookup("address"))
81+
viper.BindPFlag("server.openBrowser", serverCmd.Flags().Lookup("open"))
7682
}

cmd/uri/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var decodeCmd = &cobra.Command{
4141
return nil
4242
},
4343
Run: func(cmd *cobra.Command, args []string) {
44-
result, err := utils.DecodeUri(args[0])
44+
result, err := utils.DecodeURI(args[0])
4545

4646
if err != nil {
4747
fmt.Println(err)

cmd/uri/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var encodeCmd = &cobra.Command{
4040
return nil
4141
},
4242
Run: func(cmd *cobra.Command, args []string) {
43-
fmt.Println(utils.EncodeUri(args[0]))
43+
fmt.Println(utils.EncodeURI(args[0]))
4444
},
4545
Example: "dt uri encode http://www.github.com",
4646
}

config/config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
)
99

10+
// Config is the root collection of command configurations
1011
type Config struct {
1112
Server ServerConfig
1213
Base64 Base64Config
@@ -15,32 +16,40 @@ type Config struct {
1516
Hash HashConfig
1617
}
1718

19+
// ServerConfig allows configuration settings of the server cmd
1820
type ServerConfig struct {
19-
Address string
20-
Port string
21+
Address string
22+
Port string
23+
OpenBrowser bool
2124
}
2225

26+
// Base64Config allows configuration settings of the base64 cmd
2327
type Base64Config struct {
2428
Encoding string
2529
}
2630

31+
// RandomConfig allows configuration settings of the random cmd
2732
type RandomConfig struct {
2833
Algorithm string
2934
Length int
3035
}
3136

37+
// UUIDConfig allows configuration settings of the uuid cmd
3238
type UUIDConfig struct {
3339
Namespace string
3440
Version int
3541
}
3642

43+
// HashConfig allows configuration settings of the hash cmd
3744
type HashConfig struct {
3845
Algorithm string
3946
Cost int
4047
}
4148

49+
// Cfg the root object of the configuration
4250
var Cfg *Config
4351

52+
// InitConfig initializes the configuration if provided.
4453
func InitConfig() {
4554
home, err := homedir.Dir()
4655
if err != nil {

examples/config-sample.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ server:
99
# Defines the hostname or IP address of the server.
1010
address: 127.0.0.1
1111

12+
# Defines if a browser window should opens that points automatically to the served url
13+
openBrowser: true
14+
1215

1316
#
1417
# Configuration of the uuid command: dt uuid

utils/random.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const ALPH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
3636
// SPECIAL are special characters
3737
const SPECIAL = "!#$%&()*+,-./:;><?^_"
3838

39-
// PARALLEL_LIMIT is the limit for concurrent execution of random char generations
40-
const PARALLEL_LIMIT = 100
39+
// parallelLimit is the limit for concurrent execution of random char generations
40+
const parallelLimit = 100
4141

4242
// Source defines a string of letters used as source for a random string
4343
type Source interface {
@@ -79,11 +79,11 @@ func Random(n int, a Source) string {
7979
l := len(letters)
8080
r := make(chan string)
8181
result := make([]string, n)
82-
sem := make(Semaphore, PARALLEL_LIMIT)
82+
sem := make(Semaphore, parallelLimit)
8383

8484
for i := 0; i < n; i++ {
8585
go func(numLetters int, letters string) {
86-
// Wait if there are more concurrent executions than the PARALLEL_LIMIT allows
86+
// Wait if there are more concurrent executions than the parallelLimit allows
8787
sem.Acquire(1)
8888
defer sem.Release(1)
8989

utils/server.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright © 2018 Christian Claus <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package utils
22+
23+
import (
24+
"errors"
25+
"log"
26+
"os/exec"
27+
"runtime"
28+
)
29+
30+
31+
// Open opens the default system browser and points to the given url.
32+
func Open(url string) {
33+
var cmd *exec.Cmd
34+
35+
switch runtime.GOOS {
36+
case "linux":
37+
cmd = exec.Command("xdg-open", url)
38+
case "windows":
39+
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
40+
case "darwin":
41+
cmd = exec.Command("open", url)
42+
default:
43+
log.Fatal(errors.New("Unsupported operating system"))
44+
}
45+
46+
err := cmd.Start()
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
}

utils/uri.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"strings"
2626
)
2727

28-
// EncodeUri encodes a string so it can safely placed inside an URL.
29-
func EncodeUri(uri string) string {
28+
// EncodeURI encodes a string so it can safely placed inside an URL.
29+
func EncodeURI(uri string) string {
3030
uriComponents := strings.Split(uri, " ")
3131

3232
for i, s := range uriComponents {
@@ -36,8 +36,8 @@ func EncodeUri(uri string) string {
3636
return strings.Join(uriComponents, "%20")
3737
}
3838

39-
// DecodeUri does the inverse transformation of EncodeUri. Currently it's
39+
// DecodeURI does the inverse transformation of EncodeUri. Currently it's
4040
// just a delegate for uri.QueryUnescape.
41-
func DecodeUri(decodedUri string) (string, error) {
42-
return url.QueryUnescape(decodedUri)
41+
func DecodeURI(decodedURI string) (string, error) {
42+
return url.QueryUnescape(decodedURI)
4343
}

0 commit comments

Comments
 (0)