-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrelationtuples.go
More file actions
158 lines (132 loc) · 3.74 KB
/
relationtuples.go
File metadata and controls
158 lines (132 loc) · 3.74 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package relationtuples
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/ory/cli/cmd/cloudx/client"
ketoClient "github.com/ory/keto/cmd/client"
"github.com/ory/keto/cmd/relationtuple"
)
const FlagAll = "all"
var ErrDeleteMissingAllFlag = fmt.Errorf("please select the tuples to delete or use `--all` to delete all tuples")
func NewListCmd() *cobra.Command {
cmd := relationtuple.NewGetCmd()
cmd.Short = "List relation tuples"
cmd.Long = `List relation tuples matching the given partial tuple.
Returns paginated results.`
wrapForOryCLI(cmd)
return cmd
}
func NewDeleteCmd() *cobra.Command {
cmd := relationtuple.NewDeleteAllCmd()
wrapForOryCLI(cmd)
cmd.Flags().Bool(FlagAll, false, "Delete all relation tuples")
wrapForDelete(cmd)
return cmd
}
func NewCreateCmd() *cobra.Command {
cmd := relationtuple.NewCreateCmd()
wrapForOryCLI(cmd)
wrapForStdinArg(cmd)
return cmd
}
func NewParseCmd() *cobra.Command {
cmd := relationtuple.NewParseCmd()
wrapForOryCLI(cmd)
// The keto parse command uses cobra.NoArgs; allow one optional arg so
// callers can pass "-" for stdin (translated to -f - below).
cmd.Args = cobra.MaximumNArgs(1)
wrapForStdinArg(cmd)
return cmd
}
// wrapForStdinArg translates a bare "-" positional argument into the -f flag
// expected by the keto commands for reading from stdin.
func wrapForStdinArg(cmd *cobra.Command) {
originalRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 1 && args[0] == "-" {
if err := cmd.Flags().Set(relationtuple.FlagFile, "-"); err != nil {
return err
}
args = args[:0]
}
return originalRunE(cmd, args)
}
}
func forwardConnectionInfo(cmd *cobra.Command) {
originalRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
h, err := client.NewCobraCommandHelper(cmd)
if err != nil {
return err
}
project, err := h.GetSelectedProject(ctx)
if err != nil {
return err
}
tokenSource, baseURL, err := h.ProjectAuthToken(ctx)
if err != nil {
return err
}
upstream := baseURL(project.Slug + ".projects")
if upstream.Port() == "" {
upstream.Host = upstream.Host + ":443"
}
token, err := tokenSource.Token()
if err != nil {
return err
}
_ = os.Setenv(ketoClient.EnvAuthToken, token.AccessToken)
_ = os.Setenv(ketoClient.EnvReadRemote, upstream.Host)
_ = os.Setenv(ketoClient.EnvWriteRemote, upstream.Host)
return originalRunE(cmd, args)
}
}
func wrapForDelete(cmd *cobra.Command) {
originalRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
all, err := cmd.Flags().GetBool(FlagAll)
if err != nil {
return err
}
if all {
return originalRunE(cmd, args)
}
// At least one of the query flags must have been set
queryFlags := []string{
relationtuple.FlagNamespace,
relationtuple.FlagObject,
relationtuple.FlagRelation,
relationtuple.FlagSubjectID,
relationtuple.FlagSubjectSet,
}
for _, flag := range queryFlags {
if cmd.Flags().Changed(flag) {
return originalRunE(cmd, args)
}
}
return ErrDeleteMissingAllFlag
}
}
func hideKetoFlags(cmd *cobra.Command) {
for _, flag := range []string{
ketoClient.FlagReadRemote,
ketoClient.FlagWriteRemote,
ketoClient.FlagInsecureNoTransportSecurity,
ketoClient.FlagInsecureSkipHostVerification,
} {
_ = cmd.Flags().MarkHidden(flag)
}
}
// wrapForOryCLI wraps the Keto command to be used in the ORY CLI.
func wrapForOryCLI(cmd *cobra.Command) {
cmd.Use = "relationships"
cmd.Aliases = []string{"relation-tuples", "relationship", "relation-tuple"}
client.RegisterProjectFlag(cmd.Flags())
client.RegisterWorkspaceFlag(cmd.Flags())
forwardConnectionInfo(cmd)
hideKetoFlags(cmd)
}