Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"$schema": "https://aka.ms/codetour-schema",
"title": "relation-tuple parseの流れ",
"steps": [
{
"file": "cmd/relationtuple/parse.go",
"description": "relation-tuple parseのエントリーポイント",
"line": 23,
"contents": "package relationtuple\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/ory/x/cmdx\"\n\t\"github.com/spf13/cobra\"\n\n\t\"github.com/ory/keto/internal/relationtuple\"\n)\n\nfunc newParseCmd() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse: \"parse\",\n\t\tShort: \"Parse human readable relation tuples\",\n\t\tLong: \"Parse human readable relation tuples as used in the documentation.\\n\" +\n\t\t\t\"Supports various output formats. Especially useful for piping into other commands by using `--format json`.\\n\" +\n\t\t\t\"Ignores comments (starting with `//`) and blank lines.\",\n\t\tArgs: cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tvar rts []*relationtuple.InternalRelationTuple\n\t\t\tfor _, fn := range args {\n\t\t\t\trtss, err := parseFile(cmd, fn)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\trts = append(rts, rtss...)\n\t\t\t}\n\n\t\t\tif len(rts) == 1 {\n\t\t\t\tcmdx.PrintRow(cmd, rts[0])\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tcmdx.PrintTable(cmd, relationtuple.NewRelationCollection(rts))\n\t\t\treturn nil\n\t\t},\n\t}\n\n\tcmdx.RegisterFormatFlags(cmd.Flags())\n\n\treturn cmd\n}\n\nfunc parseFile(cmd *cobra.Command, fn string) ([]*relationtuple.InternalRelationTuple, error) {\n\tvar f io.Reader\n\tif fn == \"-\" {\n\t\t// set human readable filename here for debug and error messages\n\t\tfn = \"stdin\"\n\t\tf = cmd.InOrStdin()\n\t} else {\n\t\tff, err := os.Open(fn)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not open file %s: %v\\n\", fn, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\tdefer ff.Close()\n\t\tf = ff\n\t}\n\n\tfc, err := io.ReadAll(f)\n\tif err != nil {\n\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could read file %s: %v\\n\", fn, err)\n\t\treturn nil, cmdx.FailSilently(cmd)\n\t}\n\n\tparts := strings.Split(string(fc), \"\\n\")\n\trts := make([]*relationtuple.InternalRelationTuple, 0, len(parts))\n\tfor i, row := range parts {\n\t\trow = strings.TrimSpace(row)\n\t\t// ignore comments and empty lines\n\t\tif row == \"\" || strings.HasPrefix(row, \"//\") {\n\t\t\tcontinue\n\t\t}\n\n\t\trt, err := (&relationtuple.InternalRelationTuple{}).FromString(row)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not decode %s:%d\\n %s\\n\\n%v\\n\", fn, i+1, row, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\trts = append(rts, rt)\n\t}\n\n\treturn rts, nil\n}\n"
},
{
"file": "cmd/relationtuple/parse.go",
"description": "標準入力とファイルの両方から入力を受け付けてる",
"line": 61,
"selection": {
"start": {
"line": 49,
"character": 2
},
"end": {
"line": 61,
"character": 3
}
},
"contents": "package relationtuple\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/ory/x/cmdx\"\n\t\"github.com/spf13/cobra\"\n\n\t\"github.com/ory/keto/internal/relationtuple\"\n)\n\nfunc newParseCmd() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse: \"parse\",\n\t\tShort: \"Parse human readable relation tuples\",\n\t\tLong: \"Parse human readable relation tuples as used in the documentation.\\n\" +\n\t\t\t\"Supports various output formats. Especially useful for piping into other commands by using `--format json`.\\n\" +\n\t\t\t\"Ignores comments (starting with `//`) and blank lines.\",\n\t\tArgs: cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tvar rts []*relationtuple.InternalRelationTuple\n\t\t\tfor _, fn := range args {\n\t\t\t\trtss, err := parseFile(cmd, fn)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\trts = append(rts, rtss...)\n\t\t\t}\n\n\t\t\tif len(rts) == 1 {\n\t\t\t\tcmdx.PrintRow(cmd, rts[0])\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tcmdx.PrintTable(cmd, relationtuple.NewRelationCollection(rts))\n\t\t\treturn nil\n\t\t},\n\t}\n\n\tcmdx.RegisterFormatFlags(cmd.Flags())\n\n\treturn cmd\n}\n\nfunc parseFile(cmd *cobra.Command, fn string) ([]*relationtuple.InternalRelationTuple, error) {\n\tvar f io.Reader\n\tif fn == \"-\" {\n\t\t// set human readable filename here for debug and error messages\n\t\tfn = \"stdin\"\n\t\tf = cmd.InOrStdin()\n\t} else {\n\t\tff, err := os.Open(fn)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not open file %s: %v\\n\", fn, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\tdefer ff.Close()\n\t\tf = ff\n\t}\n\n\tfc, err := io.ReadAll(f)\n\tif err != nil {\n\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could read file %s: %v\\n\", fn, err)\n\t\treturn nil, cmdx.FailSilently(cmd)\n\t}\n\n\tparts := strings.Split(string(fc), \"\\n\")\n\trts := make([]*relationtuple.InternalRelationTuple, 0, len(parts))\n\tfor i, row := range parts {\n\t\trow = strings.TrimSpace(row)\n\t\t// ignore comments and empty lines\n\t\tif row == \"\" || strings.HasPrefix(row, \"//\") {\n\t\t\tcontinue\n\t\t}\n\n\t\trt, err := (&relationtuple.InternalRelationTuple{}).FromString(row)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not decode %s:%d\\n %s\\n\\n%v\\n\", fn, i+1, row, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\trts = append(rts, rt)\n\t}\n\n\treturn rts, nil\n}\n"
},
{
"file": "cmd/relationtuple/parse.go",
"description": "読み込んだ文字列をInternalRelationTupleに変換している",
"line": 78,
"contents": "package relationtuple\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/ory/x/cmdx\"\n\t\"github.com/spf13/cobra\"\n\n\t\"github.com/ory/keto/internal/relationtuple\"\n)\n\nfunc newParseCmd() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse: \"parse\",\n\t\tShort: \"Parse human readable relation tuples\",\n\t\tLong: \"Parse human readable relation tuples as used in the documentation.\\n\" +\n\t\t\t\"Supports various output formats. Especially useful for piping into other commands by using `--format json`.\\n\" +\n\t\t\t\"Ignores comments (starting with `//`) and blank lines.\",\n\t\tArgs: cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tvar rts []*relationtuple.InternalRelationTuple\n\t\t\tfor _, fn := range args {\n\t\t\t\trtss, err := parseFile(cmd, fn)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\trts = append(rts, rtss...)\n\t\t\t}\n\n\t\t\tif len(rts) == 1 {\n\t\t\t\tcmdx.PrintRow(cmd, rts[0])\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tcmdx.PrintTable(cmd, relationtuple.NewRelationCollection(rts))\n\t\t\treturn nil\n\t\t},\n\t}\n\n\tcmdx.RegisterFormatFlags(cmd.Flags())\n\n\treturn cmd\n}\n\nfunc parseFile(cmd *cobra.Command, fn string) ([]*relationtuple.InternalRelationTuple, error) {\n\tvar f io.Reader\n\tif fn == \"-\" {\n\t\t// set human readable filename here for debug and error messages\n\t\tfn = \"stdin\"\n\t\tf = cmd.InOrStdin()\n\t} else {\n\t\tff, err := os.Open(fn)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not open file %s: %v\\n\", fn, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\tdefer ff.Close()\n\t\tf = ff\n\t}\n\n\tfc, err := io.ReadAll(f)\n\tif err != nil {\n\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could read file %s: %v\\n\", fn, err)\n\t\treturn nil, cmdx.FailSilently(cmd)\n\t}\n\n\tparts := strings.Split(string(fc), \"\\n\")\n\trts := make([]*relationtuple.InternalRelationTuple, 0, len(parts))\n\tfor i, row := range parts {\n\t\trow = strings.TrimSpace(row)\n\t\t// ignore comments and empty lines\n\t\tif row == \"\" || strings.HasPrefix(row, \"//\") {\n\t\t\tcontinue\n\t\t}\n\n\t\trt, err := (&relationtuple.InternalRelationTuple{}).FromString(row)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not decode %s:%d\\n %s\\n\\n%v\\n\", fn, i+1, row, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\trts = append(rts, rt)\n\t}\n\n\treturn rts, nil\n}\n"
},
{
"file": "cmd/relationtuple/parse.go",
"description": "1行、または複数行をコンソールに出力している",
"line": 37,
"selection": {
"start": {
"line": 33,
"character": 4
},
"end": {
"line": 37,
"character": 66
}
},
"contents": "package relationtuple\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n\n\t\"github.com/ory/x/cmdx\"\n\t\"github.com/spf13/cobra\"\n\n\t\"github.com/ory/keto/internal/relationtuple\"\n)\n\nfunc newParseCmd() *cobra.Command {\n\tcmd := &cobra.Command{\n\t\tUse: \"parse\",\n\t\tShort: \"Parse human readable relation tuples\",\n\t\tLong: \"Parse human readable relation tuples as used in the documentation.\\n\" +\n\t\t\t\"Supports various output formats. Especially useful for piping into other commands by using `--format json`.\\n\" +\n\t\t\t\"Ignores comments (starting with `//`) and blank lines.\",\n\t\tArgs: cobra.MinimumNArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tvar rts []*relationtuple.InternalRelationTuple\n\t\t\tfor _, fn := range args {\n\t\t\t\trtss, err := parseFile(cmd, fn)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\trts = append(rts, rtss...)\n\t\t\t}\n\n\t\t\tif len(rts) == 1 {\n\t\t\t\tcmdx.PrintRow(cmd, rts[0])\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tcmdx.PrintTable(cmd, relationtuple.NewRelationCollection(rts))\n\t\t\treturn nil\n\t\t},\n\t}\n\n\tcmdx.RegisterFormatFlags(cmd.Flags())\n\n\treturn cmd\n}\n\nfunc parseFile(cmd *cobra.Command, fn string) ([]*relationtuple.InternalRelationTuple, error) {\n\tvar f io.Reader\n\tif fn == \"-\" {\n\t\t// set human readable filename here for debug and error messages\n\t\tfn = \"stdin\"\n\t\tf = cmd.InOrStdin()\n\t} else {\n\t\tff, err := os.Open(fn)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not open file %s: %v\\n\", fn, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\tdefer ff.Close()\n\t\tf = ff\n\t}\n\n\tfc, err := io.ReadAll(f)\n\tif err != nil {\n\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could read file %s: %v\\n\", fn, err)\n\t\treturn nil, cmdx.FailSilently(cmd)\n\t}\n\n\tparts := strings.Split(string(fc), \"\\n\")\n\trts := make([]*relationtuple.InternalRelationTuple, 0, len(parts))\n\tfor i, row := range parts {\n\t\trow = strings.TrimSpace(row)\n\t\t// ignore comments and empty lines\n\t\tif row == \"\" || strings.HasPrefix(row, \"//\") {\n\t\t\tcontinue\n\t\t}\n\n\t\trt, err := (&relationtuple.InternalRelationTuple{}).FromString(row)\n\t\tif err != nil {\n\t\t\t_, _ = fmt.Fprintf(cmd.ErrOrStderr(), \"Could not decode %s:%d\\n %s\\n\\n%v\\n\", fn, i+1, row, err)\n\t\t\treturn nil, cmdx.FailSilently(cmd)\n\t\t}\n\t\trts = append(rts, rt)\n\t}\n\n\treturn rts, nil\n}\n"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment