Skip to content

Commit

Permalink
Integrate go-reportcard (#63)
Browse files Browse the repository at this point in the history
* Run gofmt

* Add golint

* Add badge
  • Loading branch information
rdsubhas authored Jan 8, 2017
1 parent 780774f commit 06691b7
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 113 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ go_import_path: github.com/goeuro/myke
go:
- 1.7
install:
- go get -u -v github.com/tools/godep
- go get -u -v github.com/mitchellh/gox
- go get -u -v github.com/jteeuwen/go-bindata/...
- godep restore -v
- bin/init.sh
script:
- gofmt -d -s -e . 2>&1 | tee -a fmt.out
- test ! -s fmt.out
- golint -set_exit_status .
- go test ./...
- bin/cross-compile.sh "$TRAVIS_TAG-$TRAVIS_COMMIT"
deploy:
Expand Down
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
FROM golang:1.7

WORKDIR /go/src/github.com/goeuro/myke
COPY Godeps /go/src/github.com/goeuro/myke/Godeps
RUN go get -u -v github.com/tools/godep && \
go get -u -v github.com/mitchellh/gox && \
go get -u -v github.com/jteeuwen/go-bindata/... && \
godep restore -v
COPY Godeps Godeps
COPY bin/init.sh bin/
RUN bin/init.sh

COPY . /go/src/github.com/goeuro/myke
CMD ["bin/cross-compile.sh"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# myke [![Build Status](https://travis-ci.org/goeuro/myke.svg?branch=travis-ci)](https://travis-ci.org/goeuro/myke) [![Latest Release](https://img.shields.io/github/tag/goeuro/myke.svg)](https://github.com/goeuro/myke/releases/latest)
# myke [![Build Status](https://travis-ci.org/goeuro/myke.svg?branch=travis-ci)](https://travis-ci.org/goeuro/myke) [![Latest Release](https://img.shields.io/github/tag/goeuro/myke.svg)](https://github.com/goeuro/myke/releases/latest) [![Go Report Card](https://goreportcard.com/badge/github.com/goeuro/myke)](https://goreportcard.com/report/github.com/goeuro/myke)

> myke makes it easy to write development tasks
Expand Down
7 changes: 7 additions & 0 deletions bin/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -xe
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/tools/godep
go get -u -v github.com/mitchellh/gox
go get -u -v github.com/jteeuwen/go-bindata/...
godep restore -v
5 changes: 3 additions & 2 deletions cmd/license.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cmd

import (
"gopkg.in/urfave/cli.v1"
"fmt"
"github.com/goeuro/myke/core"
"github.com/pkg/errors"
"fmt"
"gopkg.in/urfave/cli.v1"
)

// License prints all open source licenses
func License(c *cli.Context) error {
data, err := core.Asset("tmp/LICENSES")
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"gopkg.in/urfave/cli.v1"
"github.com/rdsubhas/go-elastictable"
"strings"
"gopkg.in/urfave/cli.v1"
"sort"
"strings"
)

var headers = []string{"PROJECT", "TAGS", "TASKS"}

// List lists tasks
func List(c *cli.Context) error {
w := loadWorkspace(c.String("file"))
t := elastictable.NewElasticTable(headers)
Expand Down
31 changes: 17 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package cmd

import (
"gopkg.in/urfave/cli.v1"
"github.com/goeuro/myke/core"
"github.com/apex/log"
logcli "github.com/apex/log/handlers/cli"
"github.com/goeuro/myke/core"
"gopkg.in/urfave/cli.v1"
"os"
"path/filepath"
"strings"
"os"
)

// NewApp creates a new CLI app
func NewApp() *cli.App {
app := cli.NewApp()
app.Name = "myke"
app.Version = Version()
app.Usage = "make with yml"
app.Action = Action
app.Flags = []cli.Flag {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "f, file",
Value: "myke.yml",
Usage: "`yml-file` to load",
Name: "f, file",
Value: "myke.yml",
Usage: "`yml-file` to load",
},
cli.StringFlag{
Name: "template",
Usage: "render template `tpl-file` (will not run any command)",
Name: "template",
Usage: "render template `tpl-file` (will not run any command)",
},
cli.BoolFlag{
Name: "license",
Usage: "show license",
Name: "license",
Usage: "show license",
},
cli.StringFlag{
Name: "loglevel",
Name: "loglevel",
Value: "info",
Usage: "log level, one of debug|`info`|warn|error|fatal",
},
}
return app
}

// Action is the CLI entrypoint
func Action(c *cli.Context) error {
log.SetHandler(&logcli.Handler{Writer: os.Stdout, Padding: 0})
if level, err := log.ParseLevel(c.String("loglevel")); err == nil {
Expand All @@ -51,11 +53,12 @@ func Action(c *cli.Context) error {
return License(c)
} else if c.NArg() > 0 {
return Run(c)
} else {
return List(c)
}

return List(c)
}

// Version prints myke version
func Version() string {
version, _ := core.Asset("tmp/version")
return strings.TrimSpace(string(version))
Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"gopkg.in/urfave/cli.v1"
"github.com/goeuro/myke/core"
"github.com/pkg/errors"
"gopkg.in/urfave/cli.v1"
)

// Run runs the given tasks
func Run(c *cli.Context) error {
qs := make([]string, len(c.Args()))
for i, v := range c.Args() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/template.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cmd

import (
"gopkg.in/urfave/cli.v1"
"fmt"
"github.com/goeuro/myke/core"
"github.com/pkg/errors"
"fmt"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
)

// Template renders the given template file
func Template(c *cli.Context) error {
bytes, err := ioutil.ReadFile(c.String("template"))
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions core/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 18 additions & 16 deletions core/execution.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package core

import (
"errors"
"github.com/apex/log"
"github.com/kardianos/osext"
"os"
"os/exec"
"time"
"strings"
"errors"
"time"
)

// Execution represents a task and context being invoked
type Execution struct {
Workspace *Workspace
Query *Query
Project *Project
Task *Task
}

// ExecuteQuery executes the given query in the workspace
func ExecuteQuery(w *Workspace, q Query) error {
matches := q.Search(w)
matches := q.search(w)
if len(matches) == 0 {
return errors.New("no task matched: " + q.Raw)
}
Expand All @@ -37,6 +39,7 @@ func ExecuteQuery(w *Workspace, q Query) error {
return nil
}

// Execute executes the current task
func (e *Execution) Execute() error {
start := time.Now()
displayName := e.Project.Name + "/" + e.Task.Name
Expand All @@ -54,7 +57,6 @@ func (e *Execution) Execute() error {
elapsed := time.Since(start)
if err != nil {
log.Errorf("%v/%v: Failed, Took: %v", e.Project.Name, e.Task.Name, elapsed)
return err
} else {
log.Infof("%v: Completed, Took: %v", displayName, elapsed)
}
Expand Down Expand Up @@ -82,39 +84,39 @@ func (e *Execution) executeCmd(cmd string) error {
return err
}

shell := []string{"sh", "-exc",}
shell := []string{"sh", "-exc"}
if len(e.Task.Shell) > 0 {
shell = strings.Split(strings.TrimSpace(e.Task.Shell), " ")
}
shell = append(shell, cmd)

proc := exec.Command(shell[0], shell[1:]...)
proc.Dir = e.Project.Cwd
proc.Env = e.EnvList()
proc.Env = e.envList()
proc.Stdin = os.Stdin
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
return proc.Run()
}

func (e *Execution) Env() map[string]string {
func (e *Execution) env() map[string]string {
myke, _ := osext.Executable()
extraEnv := map[string]string{
"myke": myke,
"myke": myke,
"MYKE_PROJECT": e.Project.Name,
"MYKE_TASK": e.Task.Name,
"MYKE_CWD": e.Project.Cwd,
"MYKE_TASK": e.Task.Name,
"MYKE_CWD": e.Project.Cwd,
}
env := mergeEnv(mergeEnv(e.Project.Env, extraEnv), OsEnv())
env["PATH"] = strings.Join([]string{ env["PATH"], os.Getenv("PATH") }, PathSep)
env["PATH"] = strings.Join([]string{env["PATH"], os.Getenv("PATH")}, pathSep)
return env
}

func (e *Execution) EnvList() []string {
env := e.Env()
envList := make([]string, len(env))
func (e *Execution) envList() []string {
env := e.env()
list := make([]string, len(env))
for k, v := range env {
envList = append(envList, k+"="+v)
list = append(list, k+"="+v)
}
return envList
return list
}
18 changes: 10 additions & 8 deletions core/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"io/ioutil"
"path/filepath"
"strings"
"sort"
"strings"
)

// Project represents a parsed project
type Project struct {
Src string
Cwd string
Expand All @@ -23,6 +24,7 @@ type Project struct {
Tasks map[string]Task
}

// ParseProject parses the given project
func ParseProject(path string) (Project, error) {
src, err := filepath.Abs(path)
if err != nil {
Expand All @@ -33,17 +35,17 @@ func ParseProject(path string) (Project, error) {
return ParseProject(filepath.Join(src, "myke.yml"))
}

p, err := loadProjectYaml(src)
p, err := loadProjectYAML(src)
if err != nil {
return Project{}, err
}

p.Src = src
p.Cwd = filepath.Dir(src)
p.EnvFiles = append(p.EnvFiles, strings.TrimSuffix(src, ".yml") + ".env")
p.EnvFiles = append(p.EnvFiles, strings.TrimSuffix(src, ".yml")+".env")
for _, epath := range p.EnvFiles {
p.Env = mergeEnv(p.Env, loadEnvFile(normalizeFilePath(p.Cwd, epath)))
p.Env = mergeEnv(p.Env, loadEnvFile(normalizeFilePath(p.Cwd, epath + ".local")))
p.Env = mergeEnv(p.Env, loadEnvFile(normalizeFilePath(p.Cwd, epath+".local")))
}

p.Env = mergeEnv(p.Env, OsEnv())
Expand All @@ -58,7 +60,7 @@ func ParseProject(path string) (Project, error) {
return p, nil
}

func loadProjectYaml(path string) (Project, error) {
func loadProjectYAML(path string) (Project, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return Project{}, err
Expand All @@ -70,10 +72,10 @@ func loadProjectYaml(path string) (Project, error) {
}

json := gjson.Parse(string(jsonbytes))
return loadProjectJson(json), nil
return loadProjectJSON(json), nil
}

func loadProjectJson(json gjson.Result) Project {
func loadProjectJSON(json gjson.Result) Project {
p := Project{}
if j := json.Get("project"); j.Exists() {
p.Name = j.String()
Expand Down Expand Up @@ -111,7 +113,7 @@ func loadProjectJson(json gjson.Result) Project {
p.Tasks = make(map[string]Task)
if j := json.Get("tasks"); j.Exists() {
for k, v := range j.Map() {
p.Tasks[k] = loadTaskJson(k, v)
p.Tasks[k] = loadTaskJSON(k, v)
}
}
return p
Expand Down
Loading

0 comments on commit 06691b7

Please sign in to comment.