• 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

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

4
package relationtuple
5

6
import (
7
        "fmt"
8
        "io"
9
        "os"
10
        "strings"
11

12
        "github.com/ory/keto/ketoapi"
13

14
        "github.com/ory/x/cmdx"
15
        "github.com/spf13/cobra"
16
)
17

18
func NewParseCmd() *cobra.Command {
×
19
        cmd := &cobra.Command{
×
20
                Use:   "parse",
×
21
                Short: "Parse human readable relationships",
×
22
                Long: "Parse human readable relationships as used in the documentation.\n" +
×
23
                        "Supports various output formats. Especially useful for piping into other commands by using `--format json`.\n" +
×
24
                        "Ignores comments (lines starting with `//`) and blank lines.",
×
25
                Args: cobra.MinimumNArgs(1),
×
26
                RunE: func(cmd *cobra.Command, args []string) error {
×
27
                        var rts []*ketoapi.RelationTuple
×
28
                        for _, fn := range args {
×
29
                                rtss, err := parseFile(cmd, fn)
×
30
                                if err != nil {
×
31
                                        return err
×
32
                                }
×
33
                                rts = append(rts, rtss...)
×
34
                        }
35

36
                        if len(rts) == 1 {
×
37
                                cmdx.PrintRow(cmd, rts[0])
×
38
                                return nil
×
39
                        }
×
40
                        cmdx.PrintTable(cmd, NewAPICollection(rts))
×
41
                        return nil
×
42
                },
43
        }
44

45
        cmdx.RegisterFormatFlags(cmd.Flags())
×
46

×
47
        return cmd
×
48
}
49

50
func parseFile(cmd *cobra.Command, fn string) ([]*ketoapi.RelationTuple, error) {
1✔
51
        var f io.Reader
1✔
52
        if fn == "-" {
2✔
53
                // set human readable filename here for debug and error messages
1✔
54
                fn = "stdin"
1✔
55
                f = cmd.InOrStdin()
1✔
56
        } else {
2✔
57
                ff, err := os.Open(fn)
1✔
58
                if err != nil {
1✔
59
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not open file %s: %v\n", fn, err)
×
60
                        return nil, cmdx.FailSilently(cmd)
×
61
                }
×
62
                defer ff.Close()
1✔
63
                f = ff
1✔
64
        }
65

66
        fc, err := io.ReadAll(f)
1✔
67
        if err != nil {
1✔
68
                _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could read file %s: %v\n", fn, err)
×
69
                return nil, cmdx.FailSilently(cmd)
×
70
        }
×
71

72
        parts := strings.Split(string(fc), "\n")
1✔
73
        rts := make([]*ketoapi.RelationTuple, 0, len(parts))
1✔
74
        for i, row := range parts {
2✔
75
                row = strings.TrimSpace(row)
1✔
76
                // ignore comments and empty lines
1✔
77
                if row == "" || strings.HasPrefix(row, "//") {
2✔
78
                        continue
1✔
79
                }
80

81
                rt, err := (&ketoapi.RelationTuple{}).FromString(row)
1✔
82
                if err != nil {
1✔
83
                        _, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode %s:%d\n  %s\n\n%v\n", fn, i+1, row, err)
×
84
                        return nil, cmdx.FailSilently(cmd)
×
85
                }
×
86
                rts = append(rts, rt)
1✔
87
        }
88

89
        return rts, nil
1✔
90
}
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