-
Notifications
You must be signed in to change notification settings - Fork 28
/
update.go
41 lines (32 loc) · 1022 Bytes
/
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
// SPDX-License-Identifier: Apache-2.0
package service
import (
"context"
"github.com/sirupsen/logrus"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
)
// UpdateService updates an existing service in the database.
func (e *engine) UpdateService(ctx context.Context, s *library.Service) (*library.Service, error) {
e.logger.WithFields(logrus.Fields{
"service": s.GetNumber(),
}).Tracef("updating service %s", s.GetName())
// cast the library type to database type
//
// https://pkg.go.dev/github.com/go-vela/types/database#ServiceFromLibrary
service := database.ServiceFromLibrary(s)
// validate the necessary fields are populated
//
// https://pkg.go.dev/github.com/go-vela/types/database#Service.Validate
err := service.Validate()
if err != nil {
return nil, err
}
// send query to the database
result := e.client.
WithContext(ctx).
Table(constants.TableService).
Save(service)
return service.ToLibrary(), result.Error
}