forked from pace/bricks
/
service.go
129 lines (107 loc) · 2.91 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
// Created at 2018/08/10 by Vincent Landgraf
package service
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
// Clone the service into pace path
func Clone(name string) {
// get dir for the service
dir, err := GoServicePath(name)
if err != nil {
log.Fatal(err)
}
SimpleExec("git", "clone", fmt.Sprintf(GitLabTemplate, name), dir)
}
// Path prints the path of the service identified by name to STDOUT
func Path(name string) {
// get dir for the service
dir, err := GoServicePath(name)
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
}
// Edit the service with given name in favorite editor, defined
// by env PACE_EDITOR or EDITOR
func Edit(name string) {
editor, ok := os.LookupEnv("PACE_EDITOR")
if !ok {
editor, ok = os.LookupEnv("EDITOR")
if !ok {
log.Fatal("No $PACE_EDITOR or $EDITOR defined!")
}
}
// get dir for the service
dir, err := GoServicePath(name)
if err != nil {
log.Fatal(err)
}
SimpleExec(editor, dir)
}
// RunOptions fallback cmdName and additional arguments for the run cmd
type RunOptions struct {
CmdName string // alternative name for the command of the service
Args []string // rest of arguments
}
// Run the service daemon for the given name or use the optional
// cmdname instead
func Run(name string, options RunOptions) {
// get dir for the service
dir, err := GoServicePath(name)
if err != nil {
log.Fatal(err)
}
// identify the go files to run
var args []string
if options.CmdName == "" {
args, err = filepath.Glob(filepath.Join(dir, fmt.Sprintf("cmd/%sd/*.go", name)))
} else {
args, err = filepath.Glob(filepath.Join(dir, fmt.Sprintf("cmd/%s/*.go", options.CmdName)))
}
if err != nil {
log.Fatal(err)
}
// start go run
args = append([]string{"run"}, args...)
args = append(args, options.Args...)
SimpleExec("go", args...)
}
// TestOptions options to respect when starting a test
type TestOptions struct {
GoConvey bool
}
// Test execute the gorich or goconvey test runners
func Test(name string, options TestOptions) {
if options.GoConvey {
AutoInstall("goconvey", "github.com/smartystreets/goconvey")
} else {
AutoInstall("richgo", "github.com/kyoh86/richgo")
}
// get dir for the service
pkg := filepath.Join(GoServicePackagePath(name), "...")
if options.GoConvey {
// get dir for the service
dir, err := GoServicePath(name)
if err != nil {
log.Fatal(err)
}
SimpleExecInPath(dir, GoBinCommand("goconvey"))
} else {
SimpleExec(GoBinCommand("richgo"), "test", "-race", "-cover", pkg)
}
}
// Lint executes golint or installes if not already installed
func Lint(name string) {
AutoInstall("golint", "golang.org/x/lint/golint")
var buf bytes.Buffer
GoBinCommandText(&buf, "go", "list", filepath.Join(GoServicePackagePath(name), "..."))
paths := strings.Split(buf.String(), "\n")
// start go run
SimpleExec(GoBinCommand("golint"), paths...)
}