-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
119 lines (106 loc) · 2.77 KB
/
logs.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
/* Copyright © 2022-2024 Bopmatic, LLC. All Rights Reserved.
*
* See LICENSE file at the root of this package for license terms
*/
package main
import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"time"
_ "embed"
"github.com/araddon/dateparse"
bopsdk "github.com/bopmatic/sdk/golang"
)
//go:embed logsHelp.txt
var logsHelpText string
func logsMain(args []string) {
sdkOpts, err := getAuthSdkOpts()
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to get user creds; did you run bompatic config? err: %v\n",
err)
os.Exit(1)
}
type logsOpts struct {
common commonOpts
}
var opts logsOpts
f := flag.NewFlagSet("bopmatic logs", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err = f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
fmt.Fprintf(os.Stderr, "%v\n", logsHelpText)
os.Exit(1)
}
projId := opts.common.projectId
var proj *bopsdk.Project
if projId == "" {
proj, err = bopsdk.NewProject(opts.common.projectFilename)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
fmt.Fprintf(os.Stderr, "Please specify --projid or run from within a Bopmatic project directory.\n")
fmt.Fprintf(os.Stderr, "%v\n", logsHelpText)
} else {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
os.Exit(1)
}
projId = proj.Desc.Id
}
svcName := opts.common.serviceName
if svcName == "" {
if proj != nil {
if len(proj.Desc.Services) == 1 {
svcName = proj.Desc.Services[0].Name
} else {
svcList := make([]string, 0)
for _, svc := range proj.Desc.Services {
svcList = append(svcList, svc.Name)
}
fmt.Fprintf(os.Stderr, "Please specify --svcname. Project %v currently has %v services: %v\n",
projId, len(svcList), svcList)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "Please specify --svcname.")
os.Exit(1)
}
}
var startTime, endTime time.Time
const DefaultLogWindow = 48 * time.Hour
if opts.common.startTime == "" {
startTime = time.Now().UTC().Add(-DefaultLogWindow)
} else {
startTime, err = dateparse.ParseAny(opts.common.startTime)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse start time(%v): %v\n",
opts.common.startTime, err)
os.Exit(1)
}
}
if opts.common.endTime == "" {
endTime = time.Now().UTC()
} else {
endTime, err = dateparse.ParseAny(opts.common.endTime)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse end time(%v): %v\n",
opts.common.endTime, err)
os.Exit(1)
}
}
if !endTime.After(startTime) {
fmt.Fprintf(os.Stderr, "End time(%v) <= start time(%v). Please specify an end time that occurs later than start time.\n",
endTime, startTime)
os.Exit(1)
}
// @todo specify environment id
err = bopsdk.GetLogs(projId, "", svcName, startTime, endTime, sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}