Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Add open in browser flag
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jun 12, 2020
1 parent 4b80501 commit a08be96
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ server:

.PHONY: run
run:
CGO_CFLAGS="-g -O2 -Wno-return-local-addr" go run -gccgoflags "-L /lib64 -l pthread" cmd/streamhut/main.go
CGO_CFLAGS="-g -O2 -Wno-return-local-addr" go run -gccgoflags "-L /lib64 -l pthread" cmd/streamhut/main.go $(args)
5 changes: 4 additions & 1 deletion cmd/streamhut/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ func main() {

defaultHTTPPort := uint(8080)
defaultTCPPort := uint(1337)
defaultDelay := 2
defaultDelay := 0
defaultTimeout := 5

var help bool
var connectPort uint
var connectHost string
var delay int
var timeout int
var open bool

rootCmd := &cobra.Command{
Use: "streamhut",
Expand All @@ -64,6 +65,7 @@ For more info, visit: https://github.com/streamhut/streamhut`,
return sclient.Stream(&client.StreamConfig{
Delay: time.Duration(delay) * time.Second,
Timeout: time.Duration(timeout) * time.Second,
Open: open,
})
},
}
Expand All @@ -73,6 +75,7 @@ For more info, visit: https://github.com/streamhut/streamhut`,
rootCmd.Flags().StringVarP(&connectHost, "host", "h", "127.0.0.1", "Host to connect to")
rootCmd.Flags().IntVarP(&delay, "delay", "d", defaultDelay, "Delay in seconds before starting stream")
rootCmd.Flags().IntVarP(&timeout, "timeout", "t", defaultTimeout, "Max timeout allowed before exiting if no data is recieved after starting")
rootCmd.Flags().BoolVarP(&open, "open", "o", false, "Open the stream url in your browser")

var httpPort uint
var tcpPort uint
Expand Down
48 changes: 48 additions & 0 deletions common/open/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package open

import (
"fmt"
"os/exec"
"strings"
)

var openCmd string
var possibleCmds = []string{
"xdg-open", // generic linux
"gvfs-open", // gnome linux
"gnome-open", // gnome linux
"kde-open", // kde linux
"exo-open", // xfce linux
"enlightenment_open", // enlightenment linux
"open", // mac
"start", // windows
"cygstart", // windows
}

func init() {
for _, cmd := range possibleCmds {
out, err := exec.Command("/bin/bash", "-c", fmt.Sprintf("%s %s %s", "command", "-v", cmd)).Output()
if err != nil {
continue
}

bin := strings.TrimSpace(string(out))
if bin != "" {
openCmd = bin
break
}
}
}

// URL open url
func URL(s string) error {
if openCmd != "" {
exec.Command(openCmd, s).Output()
}
return nil
}

// CommandExists returns true if an 'open' command exists
func CommandExists() bool {
return openCmd != ""
}
36 changes: 32 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"net"
"net/url"
"os"
"regexp"
"strings"
"time"

"github.com/gorilla/websocket"
"github.com/streamhut/streamhut/common/byteutil"
"github.com/streamhut/streamhut/common/open"
)

// Client ...
Expand Down Expand Up @@ -46,6 +49,7 @@ type ListenConfig struct {
type StreamConfig struct {
Delay time.Duration
Timeout time.Duration
Open bool
}

// Listen ...
Expand Down Expand Up @@ -77,6 +81,14 @@ func (c *Client) Stream(config *StreamConfig) error {
url := fmt.Sprintf("%s:%v", c.host, c.port)
fmt.Printf("streamhut: connecting to %s\n", url)

timeout := config.Timeout
delay := config.Delay
openURL := config.Open

if delay >= timeout {
timeout = delay + (5 * time.Second)
}

conn, err := net.Dial("tcp", url)
if err != nil {
return err
Expand All @@ -89,13 +101,14 @@ func (c *Client) Stream(config *StreamConfig) error {

receivedData := false
errChan := make(chan error)
time.AfterFunc(config.Timeout, func() {
time.AfterFunc(timeout, func() {
if !receivedData {
errChan <- errors.New("timedout")
}
})

go func() {
// send to streamhut server
for {
if !ready {
continue
Expand All @@ -114,17 +127,32 @@ func (c *Client) Stream(config *StreamConfig) error {
}()

go func() {
// handle responses from streamhut
for {
line := make([]byte, 1024)
_, err := reader.Read(line)
switch err {
case nil:
fmt.Print(string(line))
lineStr := string(line)
if strings.Contains(lineStr, "streaming to") {
regex := regexp.MustCompile(`(https?.*)`)
matches := regex.FindAllString(lineStr, -1)
if len(matches) > 0 {
url := matches[0]
if openURL {
open.URL(url)
}
}
}

fmt.Print(lineStr)
if !ready {
time.Sleep(config.Delay)
time.Sleep(delay)
fmt.Println()
}
ready = true
if !ready {
ready = true
}
case io.EOF:
fmt.Println("EOF")
errChan <- nil
Expand Down

0 comments on commit a08be96

Please sign in to comment.