-
Notifications
You must be signed in to change notification settings - Fork 132
/
get_freight_v1alpha1.go
57 lines (49 loc) · 1.28 KB
/
get_freight_v1alpha1.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
package api
import (
"context"
"errors"
"fmt"
"connectrpc.com/connect"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)
func (s *server) GetFreight(
ctx context.Context,
req *connect.Request[svcv1alpha1.GetFreightRequest],
) (*connect.Response[svcv1alpha1.GetFreightResponse], error) {
project := req.Msg.GetProject()
if err := validateFieldNotEmpty("project", project); err != nil {
return nil, err
}
name := req.Msg.GetName()
alias := req.Msg.GetAlias()
if (name == "" && alias == "") || (name != "" && alias != "") {
return nil, connect.NewError(
connect.CodeInvalidArgument,
errors.New("exactly one of name or alias should not be empty"),
)
}
if err := s.validateProjectExists(ctx, project); err != nil {
return nil, err
}
freight, err := s.getFreightByNameOrAliasFn(
ctx,
s.client,
project,
name,
alias,
)
if err != nil {
return nil, fmt.Errorf("get freight: %w", err)
}
if freight == nil {
if name != "" {
err = fmt.Errorf("freight %q not found in namespace %q", name, project)
} else {
err = fmt.Errorf("freight with alias %q not found in namespace %q", alias, project)
}
return nil, connect.NewError(connect.CodeNotFound, err)
}
return connect.NewResponse(&svcv1alpha1.GetFreightResponse{
Freight: freight,
}), nil
}