...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package main
16
17 import (
18 "context"
19 "fmt"
20 "go/build"
21 "os"
22 "os/signal"
23 "path"
24 "path/filepath"
25 )
26
27 func getenvDefault(key, defaultValue string) string {
28 if v, ok := os.LookupEnv(key); ok {
29 return v
30 }
31 return defaultValue
32 }
33
34 func concatStringsArrays(values ...[]string) []string {
35 ret := []string{}
36 for _, v := range values {
37 ret = append(ret, v...)
38 }
39 return ret
40 }
41
42 func ensureAbsolutePathFromWorkspace(path string) string {
43 if filepath.IsAbs(path) {
44 return path
45 }
46 return filepath.Join(workspaceRoot, path)
47 }
48
49 func signalContext(parentCtx context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc) {
50 ctx, cancel := context.WithCancel(parentCtx)
51 ch := make(chan os.Signal, 1)
52 go func() {
53 select {
54 case <-ch:
55 cancel()
56 case <-ctx.Done():
57 }
58 }()
59 signal.Notify(ch, signals...)
60
61 return ctx, cancel
62 }
63
64 func isLocalPattern(pattern string) bool {
65 return build.IsLocalImport(pattern) || filepath.IsAbs(pattern)
66 }
67
68 func packageID(pattern string) string {
69 pattern = path.Clean(pattern)
70 if filepath.IsAbs(pattern) {
71 if relPath, err := filepath.Rel(workspaceRoot, pattern); err == nil {
72 pattern = relPath
73 }
74 }
75
76 return fmt.Sprintf("//%s", pattern)
77 }
78
View as plain text