Skip to content

Commit

Permalink
Merge pull request #15 from chclaus/issue-14-open-browser-on-server-s…
Browse files Browse the repository at this point in the history
…tartup

Add 'open' flag to browser command
  • Loading branch information
chclaus committed May 16, 2018
2 parents c1ca4a0 + 88ecc7d commit ebf5f40
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -126,6 +126,9 @@ Escapes and unescapes HTML
Starts a simple web server to serve static content. You can specify
hostname and port and must set a folder to serve.

You can also pass an option `-o` to open your default system browser
that points automatically to the served url.

## Configuration
You can configure a some default behaviors of dt to fit your needs.
Just place a file `.dt.yaml` into your home directory, you can configure
Expand All @@ -135,6 +138,7 @@ the following options:
server:
port: 3001
address: 127.0.0.1
openBrowser: true
uuid:
namespace: cacae610-c76a-4736-90ef-0271126b4346
version: 4
Expand Down
8 changes: 7 additions & 1 deletion cmd/server/server.go
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"github.com/chclaus/dt/cmd"
"github.com/chclaus/dt/config"
"github.com/chclaus/dt/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/http"
Expand Down Expand Up @@ -57,8 +58,11 @@ var serverCmd = &cobra.Command{
os.Exit(1)
}

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

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

serverCmd.Flags().StringP("address", "a", "0.0.0.0", "the hostname or ip address")
serverCmd.Flags().StringP("port", "p", "3000", "the listening port")
serverCmd.Flags().BoolP("open", "o", false, "open a browser window")
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port"))
viper.BindPFlag("server.address", serverCmd.Flags().Lookup("address"))
viper.BindPFlag("server.openBrowser", serverCmd.Flags().Lookup("open"))
}
2 changes: 1 addition & 1 deletion cmd/uri/decode.go
Expand Up @@ -41,7 +41,7 @@ var decodeCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
result, err := utils.DecodeUri(args[0])
result, err := utils.DecodeURI(args[0])

if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/uri/encode.go
Expand Up @@ -40,7 +40,7 @@ var encodeCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(utils.EncodeUri(args[0]))
fmt.Println(utils.EncodeURI(args[0]))
},
Example: "dt uri encode http://www.github.com",
}
Expand Down
13 changes: 11 additions & 2 deletions config/config.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
)

// Config is the root collection of command configurations
type Config struct {
Server ServerConfig
Base64 Base64Config
Expand All @@ -15,32 +16,40 @@ type Config struct {
Hash HashConfig
}

// ServerConfig allows configuration settings of the server cmd
type ServerConfig struct {
Address string
Port string
Address string
Port string
OpenBrowser bool
}

// Base64Config allows configuration settings of the base64 cmd
type Base64Config struct {
Encoding string
}

// RandomConfig allows configuration settings of the random cmd
type RandomConfig struct {
Algorithm string
Length int
}

// UUIDConfig allows configuration settings of the uuid cmd
type UUIDConfig struct {
Namespace string
Version int
}

// HashConfig allows configuration settings of the hash cmd
type HashConfig struct {
Algorithm string
Cost int
}

// Cfg the root object of the configuration
var Cfg *Config

// InitConfig initializes the configuration if provided.
func InitConfig() {
home, err := homedir.Dir()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions examples/config-sample.yaml
Expand Up @@ -9,6 +9,9 @@ server:
# Defines the hostname or IP address of the server.
address: 127.0.0.1

# Defines if a browser window should opens that points automatically to the served url
openBrowser: true


#
# Configuration of the uuid command: dt uuid
Expand Down
8 changes: 4 additions & 4 deletions utils/random.go
Expand Up @@ -36,8 +36,8 @@ const ALPH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// SPECIAL are special characters
const SPECIAL = "!#$%&()*+,-./:;><?^_"

// PARALLEL_LIMIT is the limit for concurrent execution of random char generations
const PARALLEL_LIMIT = 100
// parallelLimit is the limit for concurrent execution of random char generations
const parallelLimit = 100

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

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

Expand Down
50 changes: 50 additions & 0 deletions utils/server.go
@@ -0,0 +1,50 @@
// Copyright © 2018 Christian Claus <ch.claus@me.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package utils

import (
"errors"
"log"
"os/exec"
"runtime"
)


// Open opens the default system browser and points to the given url.
func Open(url string) {
var cmd *exec.Cmd

switch runtime.GOOS {
case "linux":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
case "darwin":
cmd = exec.Command("open", url)
default:
log.Fatal(errors.New("Unsupported operating system"))
}

err := cmd.Start()
if err != nil {
log.Fatal(err)
}
}
10 changes: 5 additions & 5 deletions utils/uri.go
Expand Up @@ -25,8 +25,8 @@ import (
"strings"
)

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

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

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

0 comments on commit ebf5f40

Please sign in to comment.