Skip to content

Commit

Permalink
Merge pull request #15 from timeglass/0.4
Browse files Browse the repository at this point in the history
Version 0.4
  • Loading branch information
advanderveer committed May 17, 2015
2 parents 3d5047c + 1f784e9 commit 19e28f6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.1
0.4.0
27 changes: 13 additions & 14 deletions command/client.go
Expand Up @@ -20,11 +20,17 @@ type Client struct {
*http.Client
}

type StatusData struct {
Time string
MostRecentVersion string
CurrentVersion string
}

func NewClient(info *model.Daemon) *Client {
return &Client{
info: info,
Client: &http.Client{
Timeout: time.Duration(100 * time.Millisecond),
Timeout: time.Duration(400 * time.Millisecond),
},
}
}
Expand Down Expand Up @@ -67,29 +73,22 @@ func (c *Client) Lap() (time.Duration, error) {
return d, nil
}

func (c *Client) Split() (time.Duration, error) {
func (c *Client) GetStatus() (*StatusData, error) {
resp, err := c.Get(fmt.Sprintf("http://%s/timer.status", c.info.Addr))
if err != nil {
return 0, ErrDaemonDown
return nil, ErrDaemonDown
} else if resp.StatusCode != 200 {
return 0, fmt.Errorf("Unexpected StatusCode from Daemon: %d", resp.StatusCode)
return nil, fmt.Errorf("Unexpected StatusCode from Daemon: %d", resp.StatusCode)
}

dec := json.NewDecoder(resp.Body)
defer resp.Body.Close()
status := struct {
Time string
}{}
status := &StatusData{}

err = dec.Decode(&status)
if err != nil {
return 0, errwrap.Wrapf("Failed to decode json response: {{err}}", err)
return status, errwrap.Wrapf("Failed to decode json response: {{err}}", err)
}

d, err := time.ParseDuration(status.Time)
if err != nil {
return 0, errwrap.Wrapf(fmt.Sprintf("Failed to parse '%s' as a time duration: {{err}}", status.Time), err)
}

return d, nil
return status, nil
}
35 changes: 32 additions & 3 deletions command/status.go
Expand Up @@ -3,6 +3,9 @@ package command
import (
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/codegangsta/cli"
"github.com/hashicorp/errwrap"
Expand Down Expand Up @@ -31,7 +34,11 @@ func (c *Status) Usage() string {
}

func (c *Status) Flags() []cli.Flag {
return []cli.Flag{}
return []cli.Flag{
cli.BoolFlag{
Name: "time-only",
Usage: "Only display the time",
}}
}

func (c *Status) Action() func(ctx *cli.Context) {
Expand All @@ -51,7 +58,7 @@ func (c *Status) Run(ctx *cli.Context) error {
}

client := NewClient(info)
t, err := client.Split()
status, err := client.GetStatus()
if err != nil {
if err == ErrDaemonDown {
return errwrap.Wrapf(fmt.Sprintf("No timer appears to be running for '%s': {{err}}", dir), err)
Expand All @@ -60,6 +67,28 @@ func (c *Status) Run(ctx *cli.Context) error {
}
}

fmt.Println(t)
t, err := time.ParseDuration(status.Time)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Failed to parse '%s' as a time duration: {{err}}", status.Time), err)
}

//simple semver check
if !ctx.Bool("time-only") {
curr, _ := strconv.Atoi(strings.Replace(status.CurrentVersion, ".", "", 2))
recent, _ := strconv.Atoi(strings.Replace(status.MostRecentVersion, ".", "", 2))
if curr != 0 && recent > curr {
fmt.Println("A new version of Timeglass is available, please upgrade from https://github.com/timeglass/glass/releases.")
}
} else if t.Seconds() == 0 {
//for script usage we return nothing when there has zero
//time elapsed, this prevents empty bracke
return nil
}

fmt.Printf(" [%s]", t)
if !ctx.Bool("time-only") {
fmt.Println()
}

return nil
}
3 changes: 3 additions & 0 deletions daemon/main.go
Expand Up @@ -33,6 +33,9 @@ func main() {
log.Fatal(err)
}

//check version without delaying start times
go svr.CheckVersion()

dir, err := os.Getwd()
if err != nil {
log.Fatal(errwrap.Wrapf("Failed to fetch current working dir: {{err}}", err))
Expand Down
53 changes: 41 additions & 12 deletions daemon/server.go
@@ -1,19 +1,26 @@
package main

import (
"bytes"
"fmt"
"io"
"log"
"net"
"net/http"
"strings"

"github.com/hashicorp/errwrap"
"github.com/labstack/echo"
)

var CheckVersionURL = "https://s3-eu-west-1.amazonaws.com/timeglass/version/VERSION"

type Server struct {
timer *Timer
httpb string
router *echo.Echo
listener net.Listener
timer *Timer
httpb string
router *echo.Echo
listener net.Listener
mostRecentVersion string

*http.Server
}
Expand Down Expand Up @@ -41,9 +48,16 @@ func (s *Server) lap(c *echo.Context) *echo.HTTPError {
}

func (s *Server) status(c *echo.Context) *echo.HTTPError {
return c.JSON(http.StatusOK, map[string]interface{}{
"Time": s.timer.Time().String(),
})
data := map[string]interface{}{
"CurrentVersion": Version,
"MostRecentVersion": s.mostRecentVersion,
"Time": s.timer.Time().String(),
}

//check version without delaying response
go s.CheckVersion()

return c.JSON(http.StatusOK, data)
}

func version(c *echo.Context) *echo.HTTPError {
Expand All @@ -58,11 +72,12 @@ func NewServer(httpb string, timer *Timer) (*Server, error) {
}

s := &Server{
timer: timer,
httpb: httpb,
router: router,
listener: l,
Server: &http.Server{Handler: router},
timer: timer,
httpb: httpb,
router: router,
listener: l,
mostRecentVersion: Version,
Server: &http.Server{Handler: router},
}

router.Get("/", version)
Expand All @@ -87,3 +102,17 @@ func (s *Server) Start() error {
s.timer.Start()
return s.Server.Serve(s.listener)
}

func (s *Server) CheckVersion() {
resp, err := http.Get(CheckVersionURL)
if err == nil {
defer resp.Body.Close()
buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, resp.Body)
if err != nil {
log.Printf("Failed to read response body for version check: %s", err)
} else {
s.mostRecentVersion = strings.TrimSpace(buff.String())
}
}
}
2 changes: 1 addition & 1 deletion vcs/git.go
Expand Up @@ -21,7 +21,7 @@ var PrepCommitTmpl = template.Must(template.New("name").Parse(`#!/bin/sh
# @see http://git-scm.com/docs/githooks#_prepare_commit_msg
case "$2" in
message|template)
printf "$(cat $1) [$(glass status)]" > "$1" ;;
printf "$(cat $1)$(glass status --time-only)" > "$1" ;;
esac
`))

Expand Down

0 comments on commit 19e28f6

Please sign in to comment.