• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / keto / 19929347875

04 Dec 2025 12:41PM UTC coverage: 50.769% (-0.1%) from 50.876%
19929347875

push

github

ory-bot
chore: fix golangci-lint warnings

GitOrigin-RevId: 05ddc40c2

24 of 50 new or added lines in 20 files covered. (48.0%)

5 existing lines in 2 files now uncovered.

3831 of 7546 relevant lines covered (50.77%)

0.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cmd/relationtuple/create.go
1
// Copyright © 2023 Ory Corp
2
// SPDX-License-Identifier: Apache-2.0
3

4
package relationtuple
5

6
import (
7
        "bytes"
8
        "encoding/json"
9
        "fmt"
10
        "io"
11
        "os"
12
        "path/filepath"
13

14
        "github.com/ory/keto/ketoapi"
15

16
        rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"
17

18
        "github.com/spf13/cobra"
19

20
        "github.com/ory/x/cmdx"
21
)
22

23
func NewCreateCmd() *cobra.Command {
×
24
        cmd := &cobra.Command{
×
25
                Use:   "create <relationships.json> [<relationships-dir>]",
×
26
                Short: "Create relationships from JSON files",
×
27
                Long: "Create relationships from JSON files.\n" +
×
28
                        "A directory will be traversed and all relationships will be created.\n" +
×
29
                        "Pass the special filename `-` to read from STD_IN.",
×
30
                Args: cobra.MinimumNArgs(1),
×
31
                RunE: transactRelationTuples(rts.RelationTupleDelta_ACTION_INSERT),
×
32
        }
×
33
        registerPackageFlags(cmd.Flags())
×
34

×
35
        return cmd
×
36
}
×
37

38
func readTuplesFromArg(cmd *cobra.Command, arg string) ([]*ketoapi.RelationTuple, error) {
×
39
        var f io.Reader
×
40
        if arg == "-" {
×
41
                f = cmd.InOrStdin()
×
42
        } else {
×
NEW
43
                cleanArg := filepath.Clean(arg)
×
NEW
44
                stats, err := os.Stat(cleanArg)
×
45
                if err != nil {
×
46
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error getting stats for %s: %s\n", arg, err)
×
47
                        return nil, cmdx.FailSilently(cmd)
×
48
                }
×
49

50
                if stats.IsDir() {
×
NEW
51
                        fi, err := os.ReadDir(cleanArg)
×
52
                        if err != nil {
×
53
                                return nil, err
×
54
                        }
×
55

56
                        var tuples []*ketoapi.RelationTuple
×
57
                        for _, child := range fi {
×
NEW
58
                                t, err := readTuplesFromArg(cmd, filepath.Join(cleanArg, child.Name()))
×
59
                                if err != nil {
×
60
                                        return nil, err
×
61
                                }
×
62
                                tuples = append(tuples, t...)
×
63
                        }
64
                        return tuples, nil
×
65
                }
66

NEW
67
                ff, err := os.Open(cleanArg)
×
68
                if err != nil {
×
69
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error processing arg %s: %s\n", arg, err)
×
70
                        return nil, cmdx.FailSilently(cmd)
×
71
                }
×
NEW
72
                defer func() { _ = ff.Close() }()
×
NEW
73
                f = ff
×
74
        }
75

76
        fc, err := io.ReadAll(f)
×
77
        if err != nil {
×
78
                _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could read file %s: %s\n", arg, err)
×
79
                return nil, cmdx.FailSilently(cmd)
×
80
        }
×
81

82
        decoder := json.NewDecoder(bytes.NewReader(fc))
×
83
        decoder.DisallowUnknownFields()
×
84
        // it is ok to not validate beforehand because json.Unmarshal will report errors
×
85
        if fc[0] == '[' {
×
86
                var ts []*ketoapi.RelationTuple
×
87
                if err := decoder.Decode(&ts); err != nil {
×
88
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode: %s\n", err)
×
89
                        return nil, cmdx.FailSilently(cmd)
×
90
                }
×
91
                return ts, nil
×
92
        }
93

94
        var r ketoapi.RelationTuple
×
95
        if err := decoder.Decode(&r); err != nil {
×
96
                _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode: %s\n", err)
×
97
                return nil, cmdx.FailSilently(cmd)
×
98
        }
×
99

100
        return []*ketoapi.RelationTuple{&r}, nil
×
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc