Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #83 from puppetlabs/gh-11/main/implement_pct_help
Browse files Browse the repository at this point in the history
(GH-11) Add explain command and docs package
  • Loading branch information
michaeltlombardi authored Jan 10, 2022
2 parents a424a1e + c9d04fe commit 382da9c
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 193 deletions.
130 changes: 130 additions & 0 deletions cmd/explain/explain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package explain

import (
"fmt"

"github.com/puppetlabs/pdkgo/pkg/docs"
"github.com/puppetlabs/prm/docs/md"
"github.com/spf13/cobra"
)

var (
docsApi *docs.Docs
listTopics bool
format string
tag string
category string
topic string
// Possibly implement later to enable context aware filtering for tags/categories
// depending on which is filtered first?
// filteredDocs []docs.MarkdownDoc
)

func CreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "explain",
Short: "Present documentation about topics",
Long: "Present documentation about various topics, including...",
Args: validateArgCount,
ValidArgsFunction: flagCompletion,
PreRun: preExecute,
RunE: execute,
}

dfs := md.GetDocsFS()
docsApi = &docs.Docs{
DocsFileSystem: &dfs,
}

cmd.Flags().SortFlags = false
cmd.Flags().BoolVarP(&listTopics, "list", "l", false, "list available topics")

cmd.Flags().StringVarP(&format, "format", "f", "human", "display output in human or json format")
err := cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"table", "json"}, cobra.ShellCompDirectiveNoFileComp
})
cobra.CheckErr(err)

cmd.Flags().StringVarP(&tag, "tag", "t", "", "filter available topics by tag")
err = cmd.RegisterFlagCompletionFunc("tag", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if docsApi.ParsedDocsCache == nil {
preExecute(cmd, args)
}
return docsApi.ListTags(docsApi.ParsedDocsCache), cobra.ShellCompDirectiveNoFileComp
})
cobra.CheckErr(err)

cmd.Flags().StringVarP(&category, "category", "c", "", "filter available topics by category")
err = cmd.RegisterFlagCompletionFunc("category", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if docsApi.ParsedDocsCache == nil {
preExecute(cmd, args)
}
return docsApi.ListCategories(docsApi.ParsedDocsCache), cobra.ShellCompDirectiveNoFileComp
})
cobra.CheckErr(err)

return cmd
}

func preExecute(cmd *cobra.Command, args []string) {
docsApi.FindAndParse("content")
}

func validateArgCount(cmd *cobra.Command, args []string) error {
// show available topics if user runs `pct explain`
if len(args) == 0 && !listTopics {
listTopics = true
}

if len(args) == 1 {
if category != "" || tag != "" {
return fmt.Errorf("Specify a topic *or* search by tag/category")
}
topic = args[0]
} else if len(args) > 1 {
return fmt.Errorf("Specify only one topic to explain")
}

return nil
}

func flagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if docsApi.ParsedDocsCache == nil {
preExecute(cmd, args)
}
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return docsApi.CompleteTitle(docsApi.ParsedDocsCache, toComplete), cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
}

func execute(cmd *cobra.Command, args []string) error {
docs := docsApi.ParsedDocsCache
if listTopics {
if format == "" {
format = "table"
}
if category != "" {
docs = docsApi.FilterByCategory(category, docs)
}
if tag != "" {
docs = docsApi.FilterByTag(tag, docs)
}
// If there's only one match, should we render the matching doc?
docsApi.FormatFrontMatter(format, docs)
} else if topic != "" {
doc, err := docsApi.SelectDocument(topic, docsApi.ParsedDocsCache)
if err != nil {
return err
}
output, err := docsApi.RenderDocument(doc)
if err != nil {
return err
}
fmt.Print(output)
// If --online, open in browser and do not display
// Should we have a --scroll mode?
}
return nil
}
15 changes: 0 additions & 15 deletions docs/md/content/CHANGELOG.md

This file was deleted.

106 changes: 0 additions & 106 deletions docs/md/content/CONTRIBUTING.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/md/content/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
---
title: "Readme"
description: "An overview of the PCT program."
category: concept
tags:
- meta
draft: false
---

Expand Down
10 changes: 10 additions & 0 deletions docs/md/md.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package md

import "embed"

//go:embed content/***
var DocsFS embed.FS

func GetDocsFS() embed.FS {
return DocsFS
}
28 changes: 16 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@ module github.com/puppetlabs/prm

go 1.16

replace github.com/puppetlabs/prm/docs/md => ./docs/md

require (
github.com/Masterminds/semver v1.5.0
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/containerd/containerd v1.5.8 // indirect
github.com/Microsoft/hcsshim v0.9.1 // indirect
github.com/containerd/cgroups v1.0.2 // indirect
github.com/containerd/containerd v1.5.9 // indirect
github.com/docker/docker v20.10.12+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/go-version v1.4.0
github.com/json-iterator/go v1.1.12
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/sys/mount v0.3.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/opencontainers/image-spec v1.0.2
github.com/puppetlabs/pdkgo v0.0.0-20211208200151-2414a05b08bf
github.com/opencontainers/runc v1.0.3 // indirect
github.com/puppetlabs/pdkgo v0.0.0-20220110155330-29bce1a03e20
github.com/puppetlabs/prm/docs/md v0.0.0-00010101000000-000000000000
github.com/rs/zerolog v1.26.1
github.com/spf13/afero v1.6.0
github.com/spf13/afero v1.8.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/proto/otlp v0.11.0 // indirect
golang.org/x/net v0.0.0-20211205041911-012df41ee64c // indirect
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
Loading

0 comments on commit 382da9c

Please sign in to comment.