-
Notifications
You must be signed in to change notification settings - Fork 28
/
update.go
56 lines (48 loc) · 1.57 KB
/
update.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
// SPDX-License-Identifier: Apache-2.0
//nolint:dupl // ignore similar code with update.go
package log
import (
"context"
"fmt"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
)
// UpdateLog updates an existing log in the database.
func (e *engine) UpdateLog(ctx context.Context, l *library.Log) error {
// check what the log entry is for
switch {
case l.GetServiceID() > 0:
e.logger.Tracef("updating log for service %d for build %d in the database", l.GetServiceID(), l.GetBuildID())
case l.GetStepID() > 0:
e.logger.Tracef("updating log for step %d for build %d in the database", l.GetStepID(), l.GetBuildID())
}
// cast the library type to database type
//
// https://pkg.go.dev/github.com/go-vela/types/database#LogFromLibrary
log := database.LogFromLibrary(l)
// validate the necessary fields are populated
//
// https://pkg.go.dev/github.com/go-vela/types/database#Log.Validate
err := log.Validate()
if err != nil {
return err
}
// compress log data for the resource
//
// https://pkg.go.dev/github.com/go-vela/types/database#Log.Compress
err = log.Compress(e.config.CompressionLevel)
if err != nil {
switch {
case l.GetServiceID() > 0:
return fmt.Errorf("unable to compress log for service %d for build %d: %w", l.GetServiceID(), l.GetBuildID(), err)
case l.GetStepID() > 0:
return fmt.Errorf("unable to compress log for step %d for build %d: %w", l.GetStepID(), l.GetBuildID(), err)
}
}
// send query to the database
return e.client.
Table(constants.TableLog).
Save(log).
Error
}