Skip to content

Commit

Permalink
Merge pull request #201 from kosli-dev/add-live-docs-for-queries
Browse files Browse the repository at this point in the history
Add live docs for queries
  • Loading branch information
JonJagger authored May 13, 2024
2 parents 47827f5 + f6cb1f0 commit 2319b2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/kosli/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ func KosliGenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(st
buf.WriteString("{{< /tabs >}}\n\n")
}

liveCliFullCommand, liveCliURL, liveCliExists := liveCliDocExists(name)
if liveCliExists {
buf.WriteString("## Live Example\n\n")
buf.WriteString("{{< raw-html >}}")
buf.WriteString("<pre>")
buf.WriteString("export KOSLI_ORG=cyber-dojo\n")
buf.WriteString("export KOSLI_API_TOKEN=Pj_XT2deaVA6V1qrTlthuaWsmjVt4eaHQwqnwqjRO3A # read-only\n")
buf.WriteString(fmt.Sprintf("<a href=\"%s\">%s</a>", liveCliURL, liveCliFullCommand))
buf.WriteString("</pre>")
buf.WriteString("{{< / raw-html >}}\n\n")
}

if len(cmd.Example) > 0 {
// This is an attempt to tidy up the non-live examples, so they each have their own title.
// Note: The contents of the title lines could also contain < and > characters which will
Expand Down Expand Up @@ -255,6 +267,18 @@ func liveEventDocExists(ci string, command string) bool {
return liveDocExists(url)
}

func liveCliDocExists(command string) (string, string, bool) {
fullCommand, ok := liveCliMap[command]
if ok {
plussed := strings.Replace(fullCommand, " ", "+", -1)
exists_url := fmt.Sprintf("%v/cli_exists?command=%v", baseURL, plussed)
url := fmt.Sprintf("%v/cli?command=%v", baseURL, plussed)
return fullCommand, url, liveDocExists(exists_url)
} else {
return "", "", false
}
}

func liveDocExists(url string) bool {
response, err := http.Get(url)
if err != nil {
Expand All @@ -277,3 +301,16 @@ func yamlURL(ci string, command string) string {
func eventURL(ci string, command string) string {
return fmt.Sprintf("%v/event?ci=%v&command=%v", baseURL, strings.ToLower(ci), command)
}

var liveCliMap = map[string]string{
"kosli list environments": "kosli list environments --output=json",
"kosli get environment": "kosli get environment aws-prod --output=json",
"kosli log environment": "kosli log environment aws-prod --output=json",
"kosli list snapshots": "kosli list snapshots aws-prod --output=json",
"kosli get snapshot": "kosli get snapshot aws-prod --output=json",
"kosli diff snapshots": "kosli diff snapshots aws-beta aws-prod --output=json",
"kosli list flows": "kosli list flows --output=json",
"kosli get flow": "kosli get flow dashboard-ci --output=json",
//"kosli list trails": "kosli list trails dashboard-ci --output=json",
"kosli get trail": "kosli get trail dashboard-ci 1159a6f1193150681b8484545150334e89de6c1c --output=json",
}
1 change: 1 addition & 0 deletions docs.kosli.com/layouts/shortcodes/raw-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ .Inner }}

0 comments on commit 2319b2c

Please sign in to comment.