-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (91 loc) · 2.51 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/facebookincubator/go-belt"
"github.com/facebookincubator/go-belt/tool/logger"
xlogrus "github.com/facebookincubator/go-belt/tool/logger/implementation/logrus"
"github.com/sirupsen/logrus"
"github.com/xaionaro-go/pkg-config-wrapper/pkg/consts"
"github.com/xaionaro-go/pkg-config-wrapper/pkg/pkgconfig"
)
func ctx() context.Context {
logLevel := logger.LevelPanic
logLevelString := os.Getenv(consts.EnvVarLogLevel)
if logLevelString != "" {
err := logLevel.Set(logLevelString)
if err != nil {
panic(err)
}
}
ctx := context.Background()
ll := xlogrus.DefaultLogrusLogger()
l := xlogrus.New(ll).WithLevel(logLevel)
ctx = logger.CtxWithLogger(ctx, l)
if !func() bool {
logFilePath := os.Getenv(consts.EnvVarLogFile)
if logFilePath == "" {
return false
}
logFile, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
l.Errorf("unable to open log-file '%s': %v", logFilePath, err)
return false
}
ll.SetOutput(logFile)
return true
}() {
ll.Formatter.(*logrus.TextFormatter).ForceColors = true
ll.SetOutput(os.Stderr)
}
return ctx
}
func main() {
defer os.Stderr.Sync()
defer os.Stdout.Sync()
if len(os.Args) < 2 {
panic("not enough arguments")
}
ctx := ctx()
defer belt.Flush(ctx)
var opts pkgconfig.Options
erasePatterns := parseList(os.Getenv(consts.EnvVarEraseList))
if len(erasePatterns) > 0 {
opts = append(opts, pkgconfig.OptionErasePatterns(erasePatterns))
}
staticLibsPatterns := parseList(os.Getenv(consts.EnvVarStaticLibsList))
if len(staticLibsPatterns) > 0 {
opts = append(opts, pkgconfig.OptionForceStaticLinkPatterns(staticLibsPatterns))
}
dynamicLibsPatterns := parseList(os.Getenv(consts.EnvVarDynamicLibsList))
if len(dynamicLibsPatterns) > 0 {
opts = append(opts, pkgconfig.OptionForceDynamicLinkPatterns(dynamicLibsPatterns))
}
pkgConfig := pkgconfig.NewPkgConfig(opts...)
result, errorMsg, exitCode, err := pkgConfig.Run(ctx, os.Args[1:]...)
if _, err := fmt.Fprintf(os.Stderr, "%s", errorMsg); err != nil {
panic(err)
}
if _, err := fmt.Fprintf(os.Stdout, "%s\n", strings.Join(result, " ")); err != nil {
panic(err)
}
if err != nil {
panic(err)
}
os.Exit(exitCode)
}
func parseList(
input string,
) pkgconfig.Patterns {
var result pkgconfig.Patterns
for _, w := range strings.Split(input, ",") {
word := strings.Trim(w, " ")
if len(word) == 0 {
continue
}
result = append(result, pkgconfig.Pattern(word))
}
return result
}