snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
78
oryx/clidoc/generate.go
Normal file
78
oryx/clidoc/generate.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package clidoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Generate generates markdown documentation for a cobra command and its children.
|
||||
func Generate(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("command expects one argument which is the path to the output directory")
|
||||
}
|
||||
|
||||
return generate(cmd, args[0])
|
||||
}
|
||||
|
||||
func trimExt(s string) string {
|
||||
return strings.ReplaceAll(strings.TrimSuffix(s, filepath.Ext(s)), "_", "-")
|
||||
}
|
||||
|
||||
func generate(cmd *cobra.Command, dir string) error {
|
||||
cmd.DisableAutoGenTag = true
|
||||
for _, c := range cmd.Commands() {
|
||||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
||||
continue
|
||||
}
|
||||
if err := generate(c, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
basename := strings.Replace(cmd.CommandPath(), " ", "-", -1)
|
||||
if err := os.MkdirAll(filepath.Join(dir), 0750); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename := filepath.Join(dir, basename) + ".md"
|
||||
f, err := os.Create(filename) //#nosec:G304
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer (func() { _ = f.Close() })()
|
||||
|
||||
if _, err := io.WriteString(f, fmt.Sprintf(`---
|
||||
id: %s
|
||||
title: %s
|
||||
description: %s
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
`,
|
||||
basename,
|
||||
cmd.CommandPath(),
|
||||
cmd.CommandPath(),
|
||||
)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := GenMarkdownCustom(cmd, &b, trimExt); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.WriteString(b.String())
|
||||
return err
|
||||
}
|
||||
201
oryx/clidoc/md_docs.go
Normal file
201
oryx/clidoc/md_docs.go
Normal file
@@ -0,0 +1,201 @@
|
||||
//Copyright 2015 Red Hat Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package clidoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/ory/x/cmdx"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
|
||||
flags := cmd.NonInheritedFlags()
|
||||
flags.SetOutput(buf)
|
||||
if flags.HasAvailableFlags() {
|
||||
buf.WriteString("### Options\n\n```\n")
|
||||
flags.PrintDefaults()
|
||||
buf.WriteString("```\n\n")
|
||||
}
|
||||
|
||||
parentFlags := cmd.InheritedFlags()
|
||||
parentFlags.SetOutput(buf)
|
||||
if parentFlags.HasAvailableFlags() {
|
||||
buf.WriteString("### Options inherited from parent commands\n\n```\n")
|
||||
parentFlags.PrintDefaults()
|
||||
buf.WriteString("```\n\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenMarkdown creates markdown output.
|
||||
func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
|
||||
return GenMarkdownCustom(cmd, w, func(s string) string { return s })
|
||||
}
|
||||
|
||||
// GenMarkdownCustom creates custom markdown output.
|
||||
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
|
||||
cmd.InitDefaultHelpCmd()
|
||||
cmd.InitDefaultHelpFlag()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
name := cmd.CommandPath()
|
||||
|
||||
buf.WriteString("## " + html.EscapeString(name) + "\n\n")
|
||||
buf.WriteString(fenceIndentedBlocks(cmd.Short) + "\n\n")
|
||||
if len(cmd.Long) > 0 {
|
||||
buf.WriteString("### Synopsis\n\n")
|
||||
long, err := cmdx.TemplateCommandField(cmd, cmd.Long)
|
||||
if err != nil {
|
||||
buf.WriteString(fmt.Sprintf("<!-- Error rendering cmd.Long: %s -->\n\n", err.Error()))
|
||||
long = cmd.Long
|
||||
}
|
||||
buf.WriteString(fenceIndentedBlocks(long) + "\n\n")
|
||||
}
|
||||
|
||||
if cmd.Runnable() {
|
||||
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
|
||||
}
|
||||
|
||||
if len(cmd.Example) > 0 {
|
||||
buf.WriteString("### Examples\n\n")
|
||||
example, err := cmdx.TemplateCommandField(cmd, cmd.Example)
|
||||
if err != nil {
|
||||
buf.WriteString(fmt.Sprintf("<!-- Error rendering cmd.Example: %s -->\n\n", err.Error()))
|
||||
example = cmd.Example
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", example))
|
||||
}
|
||||
|
||||
if err := printOptions(buf, cmd, name); err != nil {
|
||||
return err
|
||||
}
|
||||
if hasSeeAlso(cmd) {
|
||||
buf.WriteString("### See also\n\n")
|
||||
if cmd.HasParent() {
|
||||
parent := cmd.Parent()
|
||||
pname := parent.CommandPath()
|
||||
link := pname + ".md"
|
||||
link = strings.Replace(link, " ", "_", -1)
|
||||
short := parent.Short
|
||||
if short != "" {
|
||||
short = fmt.Sprintf(" %s", short)
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("* [%s](%s)%s\n", pname, linkHandler(link), short))
|
||||
cmd.VisitParents(func(c *cobra.Command) {
|
||||
if c.DisableAutoGenTag {
|
||||
cmd.DisableAutoGenTag = c.DisableAutoGenTag
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
children := cmd.Commands()
|
||||
sort.Sort(byName(children))
|
||||
|
||||
for _, child := range children {
|
||||
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
|
||||
continue
|
||||
}
|
||||
cname := name + " " + child.Name()
|
||||
link := cname + ".md"
|
||||
link = strings.Replace(link, " ", "_", -1)
|
||||
short := child.Short
|
||||
if short != "" {
|
||||
short = fmt.Sprintf(" - %s", short)
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("* [%s](%s)\t%s\n", cname, linkHandler(link), short))
|
||||
}
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
|
||||
_, err := buf.WriteTo(w)
|
||||
return err
|
||||
}
|
||||
|
||||
// GenMarkdownTree will generate a markdown page for this command and all
|
||||
// descendants in the directory given. The header may be nil.
|
||||
// This function may not work correctly if your command names have `-` in them.
|
||||
// If you have `cmd` with two subcmds, `sub` and `sub-third`,
|
||||
// and `sub` has a subcommand called `third`, it is undefined which
|
||||
// help output will be in the file `cmd-sub-third.1`.
|
||||
func GenMarkdownTree(cmd *cobra.Command, dir string) error {
|
||||
identity := func(s string) string { return s }
|
||||
emptyStr := func(s string) string { return "" }
|
||||
return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)
|
||||
}
|
||||
|
||||
// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
|
||||
// with custom filePrepender and linkHandler.
|
||||
func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
|
||||
for _, c := range cmd.Commands() {
|
||||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
||||
continue
|
||||
}
|
||||
if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
|
||||
filename := filepath.Join(dir, basename)
|
||||
f, err := os.Create(filename) //#nosec:G304) //#nosec:G304
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer (func() { _ = f.Close() })()
|
||||
|
||||
if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var indentedBlock = regexp.MustCompile(`(?m)^(?: {4}|\t).*(?:\n(?: {4}|\t).*)*`)
|
||||
|
||||
func fenceIndentedBlocks(s string) string {
|
||||
return indentedBlock.ReplaceAllStringFunc(s, func(block string) string {
|
||||
// trim trailing newline to keep fence tidy
|
||||
block = strings.TrimRight(block, "\n")
|
||||
|
||||
// de-indent exactly one level for nicer fenced output
|
||||
lines := strings.Split(block, "\n")
|
||||
for i, ln := range lines {
|
||||
switch {
|
||||
case strings.HasPrefix(ln, " "):
|
||||
lines[i] = ln[4:]
|
||||
case strings.HasPrefix(ln, "\t"):
|
||||
lines[i] = ln[1:]
|
||||
}
|
||||
}
|
||||
b := strings.Join(lines, "\n")
|
||||
|
||||
// guard against already fenced content
|
||||
if strings.HasPrefix(strings.TrimSpace(b), "```") {
|
||||
return block
|
||||
}
|
||||
return "```\n" + b + "\n```"
|
||||
})
|
||||
}
|
||||
48
oryx/clidoc/testdata/hydra-client-admin.md
vendored
Normal file
48
oryx/clidoc/testdata/hydra-client-admin.md
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
---
|
||||
id: hydra-client-admin
|
||||
title: hydra client admin
|
||||
description: hydra client admin
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
## hydra client admin
|
||||
|
||||
Foo bar baz bar
|
||||
|
||||
```
|
||||
short with multiple
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Synopsis
|
||||
|
||||
Run the admin server
|
||||
|
||||
```
|
||||
<[some argument]>
|
||||
<[some argument]>
|
||||
<[some argument]>
|
||||
<[some argument]>
|
||||
<[some argument]>
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
hydra client admin <args> [flags]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for admin
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
* [hydra client](hydra-client) Run client commands
|
||||
|
||||
36
oryx/clidoc/testdata/hydra-client-public.md
vendored
Normal file
36
oryx/clidoc/testdata/hydra-client-public.md
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
---
|
||||
id: hydra-client-public
|
||||
title: hydra client public
|
||||
description: hydra client public
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
## hydra client public
|
||||
|
||||
|
||||
|
||||
### Synopsis
|
||||
|
||||
Run the public server
|
||||
|
||||
<[some argument]>
|
||||
|
||||
|
||||
```
|
||||
hydra client public <args> [flags]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for public
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
* [hydra client](hydra-client) Run client commands
|
||||
|
||||
48
oryx/clidoc/testdata/hydra-client.md
vendored
Normal file
48
oryx/clidoc/testdata/hydra-client.md
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
---
|
||||
id: hydra-client
|
||||
title: hydra client
|
||||
description: hydra client
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
## hydra client
|
||||
|
||||
Run client commands
|
||||
|
||||
### Synopsis
|
||||
|
||||
Manage OAuth2 clients
|
||||
|
||||
<[some argument]>
|
||||
|
||||
|
||||
```
|
||||
hydra client [flags]
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
hydra client --whatever
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for client
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
* [hydra](hydra)
|
||||
* [hydra client admin](hydra-client-admin) - Foo bar baz bar
|
||||
|
||||
short with multiple
|
||||
|
||||
|
||||
* [hydra client public](hydra-client-public)
|
||||
|
||||
36
oryx/clidoc/testdata/hydra-serve.md
vendored
Normal file
36
oryx/clidoc/testdata/hydra-serve.md
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
---
|
||||
id: hydra-serve
|
||||
title: hydra serve
|
||||
description: hydra serve
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
## hydra serve
|
||||
|
||||
|
||||
|
||||
### Synopsis
|
||||
|
||||
Manage the server
|
||||
|
||||
<[some argument]>
|
||||
|
||||
|
||||
```
|
||||
hydra serve [flags]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for serve
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
* [hydra](hydra)
|
||||
|
||||
38
oryx/clidoc/testdata/hydra.md
vendored
Normal file
38
oryx/clidoc/testdata/hydra.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
id: hydra
|
||||
title: hydra
|
||||
description: hydra
|
||||
---
|
||||
|
||||
<!--
|
||||
This file is auto-generated.
|
||||
|
||||
To improve this file please make your change against the appropriate "./cmd/*.go" file.
|
||||
-->
|
||||
## hydra
|
||||
|
||||
|
||||
|
||||
### Synopsis
|
||||
|
||||
A sample text
|
||||
root
|
||||
|
||||
<[some argument]>
|
||||
|
||||
|
||||
```
|
||||
hydra [flags]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for hydra
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
* [hydra client](hydra-client) - Run client commands
|
||||
* [hydra serve](hydra-serve)
|
||||
|
||||
40
oryx/clidoc/util.go
Normal file
40
oryx/clidoc/util.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2015 Red Hat Inc. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package clidoc
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Test to see if we have a reason to print See Also information in docs
|
||||
// Basically this is a test for a parent command or a subcommand which is
|
||||
// both not deprecated and not the autogenerated help command.
|
||||
func hasSeeAlso(cmd *cobra.Command) bool {
|
||||
if cmd.HasParent() {
|
||||
return true
|
||||
}
|
||||
for _, c := range cmd.Commands() {
|
||||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
||||
continue
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type byName []*cobra.Command
|
||||
|
||||
func (s byName) Len() int { return len(s) }
|
||||
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
|
||||
Reference in New Issue
Block a user