-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcommon.go
More file actions
65 lines (52 loc) · 1.67 KB
/
Copy pathcommon.go
File metadata and controls
65 lines (52 loc) · 1.67 KB
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
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
// SPDX-License-Identifier: Apache-2.0
package profile
import (
"context"
"fmt"
"io"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"github.com/mindersec/minder/internal/util"
"github.com/mindersec/minder/internal/util/cli"
"github.com/mindersec/minder/internal/util/cli/table"
minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
"github.com/mindersec/minder/pkg/fileconvert"
)
// ExecOnOneProfile is a helper function to execute a function on a single profile
func ExecOnOneProfile(ctx context.Context, t table.Table, f string, dashOpen io.Reader, project string,
exec func(context.Context, string, *minderv1.Profile) (*minderv1.Profile, error),
) (*minderv1.Profile, error) {
ctx, cancel := cli.GetAppContext(ctx, viper.GetViper())
defer cancel()
reader, closer, err := util.OpenFileArg(f, dashOpen)
if err != nil {
return nil, fmt.Errorf("error opening file arg: %w", err)
}
defer closer()
p, err := parseProfile(reader, project)
if err != nil {
return nil, fmt.Errorf("error parsing profile: %w", err)
}
// create a rule
profile, err := exec(ctx, f, p)
if err != nil {
return nil, err
}
RenderProfileRulesTable(profile, t)
return profile, nil
}
func parseProfile(r io.Reader, proj string) (*minderv1.Profile, error) {
p, err := fileconvert.ReadResourceTyped[*minderv1.Profile](yaml.NewDecoder(r))
if err != nil {
return nil, fmt.Errorf("error reading profile from file: %w", err)
}
// Override the YAML specified project with the command line argument
if proj != "" {
if p.Context == nil {
p.Context = &minderv1.Context{}
}
p.Context.Project = &proj
}
return p, nil
}