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

ory / keto / 14309294778

07 Apr 2025 12:33PM UTC coverage: 51.121% (-26.7%) from 77.849%
14309294778

push

github

ory-bot
autogen: update license overview

3854 of 7539 relevant lines covered (51.12%)

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 {
×
43
                stats, err := os.Stat(arg)
×
44
                if err != nil {
×
45
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error getting stats for %s: %s\n", arg, err)
×
46
                        return nil, cmdx.FailSilently(cmd)
×
47
                }
×
48

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

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

66
                f, err = os.Open(arg)
×
67
                if err != nil {
×
68
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error processing arg %s: %s\n", arg, err)
×
69
                        return nil, cmdx.FailSilently(cmd)
×
70
                }
×
71
        }
72

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

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

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

97
        return []*ketoapi.RelationTuple{&r}, nil
×
98
}
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