Skip to content

Commit

Permalink
Merge branch 'main' into dwedul/1658-exchange-module
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Sep 29, 2023
2 parents 4899347 + 28948e8 commit f32f506
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 16 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
Management of holds is internal, but there are queries for looking up holds on accounts.
Holds are also reflected in the `x/bank` module's `SpendableBalances` query.
* Add new MaxSupply param to marker module and deprecate MaxTotalSupply. [#1292](https://github.com/provenance-io/provenance/issues/1292).
* Add hidden docgen command to output documentation in different formats. [#1468](https://github.com/provenance-io/provenance/issues/1468).

### Improvements

Expand Down Expand Up @@ -85,7 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Dependencies

- Bump `google.golang.org/grpc` from 1.57.0 to 1.58.1 ([#1672](https://github.com/provenance-io/provenance/pull/1672), [#1685](https://github.com/provenance-io/provenance/pull/1685))
- Bump `google.golang.org/grpc` from 1.57.0 to 1.58.2 ([#1672](https://github.com/provenance-io/provenance/pull/1672), [#1685](https://github.com/provenance-io/provenance/pull/1685), [#1689](https://github.com/provenance-io/provenance/pull/1689))
- Bump `crazy-max/ghaction-import-gpg` from 5 to 6 ([#1677](https://github.com/provenance-io/provenance/pull/1677))
- Bump `golang.org/x/text` from 0.12.0 to 0.13.0 ([#1667](https://github.com/provenance-io/provenance/pull/1667))
- Bump `actions/checkout` from 3 to 4 ([#1668](https://github.com/provenance-io/provenance/pull/1668))
Expand All @@ -99,7 +100,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- Bump `bufbuild/buf-setup-action` from 1.25.1 to 1.26.0 ([#1645](https://github.com/provenance-io/provenance/pull/1645))
- Bump `bufbuild/buf-setup-action` from 1.25.0 to 1.25.1 ([#1642](https://github.com/provenance-io/provenance/pull/1642))
- Bump `google.golang.org/grpc` from 1.56.2 to 1.57.0 ([#1635](https://github.com/provenance-io/provenance/pull/1635))
- Bump `github.com/rs/zerolog` from 1.29.1 to 1.30.0 ([#1639](https://github.com/provenance-io/provenance/pull/1639))
- Bump `github.com/rs/zerolog` from 1.29.1 to 1.31.0 ([#1639](https://github.com/provenance-io/provenance/pull/1639), [#1691](https://github.com/provenance-io/provenance/pull/1691))
- Bump `bufbuild/buf-setup-action` from 1.24.0 to 1.25.0 ([#1632](https://github.com/provenance-io/provenance/pull/1632))
- Bump `bufbuild/buf-setup-action` from 1.23.1 to 1.24.0 ([#1631](https://github.com/provenance-io/provenance/pull/1631))
- Bump `github.com/cosmos/ibc-go/v6` from 6.1.1 to 6.2.0 ([#1629](https://github.com/provenance-io/provenance/pull/1629))
Expand Down
103 changes: 103 additions & 0 deletions cmd/provenanced/cmd/docgen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/cosmos/cosmos-sdk/version"
)

var docGenCmdStart = fmt.Sprintf("%s docgen", version.AppName)

const (
FlagMarkdown = "markdown"
FlagYaml = "yaml"
FlagRst = "rst"
FlagManpage = "manpage"
)

func GetDocGenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "docgen <target directory> (--markdown) (--yaml) (--rst) (--manpages) [flags]",
Short: "Generates cli documentation for the Provenance Blockchain.",
Long: `Generates cli documentation for the Provenance Blockchain.
Various documentation formats can be generated, including markdown, YAML, RST, and man pages.
To ensure the command's success, you must specify at least one format.
A successful command will not only generate files in the selected formats but also create the target directory if it doesn't already exist.`,
Example: fmt.Sprintf("%s '/tmp' --yaml --markdown", docGenCmdStart),
Hidden: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
markdown, err := cmd.Flags().GetBool(FlagMarkdown)
if err != nil {
return err
}
yaml, err := cmd.Flags().GetBool(FlagYaml)
if err != nil {
return err
}
rst, err := cmd.Flags().GetBool(FlagRst)
if err != nil {
return err
}
manpage, err := cmd.Flags().GetBool(FlagManpage)
if err != nil {
return err
}

if !markdown && !yaml && !rst && !manpage {
return fmt.Errorf("at least one doc type must be specified")
}

dir := args[0]
if !exists(dir) {
err = os.Mkdir(dir, 0755)
if err != nil {
return err
}
}

if markdown {
err = doc.GenMarkdownTree(cmd.Root(), dir)
if err != nil {
return err
}
}
if yaml {
err = doc.GenYamlTree(cmd.Root(), dir)
if err != nil {
return err
}
}
if rst {
err = doc.GenReSTTree(cmd.Root(), dir)
if err != nil {
return err
}
}
if manpage {
err = doc.GenManTree(cmd.Root(), nil, dir)
if err != nil {
return err
}
}

return nil
},
}

cmd.Flags().Bool(FlagMarkdown, false, "Generate documentation in the format of markdown pages.")
cmd.Flags().Bool(FlagYaml, false, "Generate documentation in the format of yaml.")
cmd.Flags().Bool(FlagRst, false, "Generate documentation in the format of rst.")
cmd.Flags().Bool(FlagManpage, false, "Generate documentation in the format of manpages.")

return cmd
}

func exists(dir string) bool {
_, err := os.Stat(dir)
return err == nil
}
186 changes: 186 additions & 0 deletions cmd/provenanced/cmd/docgen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package cmd_test

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdksim "github.com/cosmos/cosmos-sdk/simapp"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
provenancecmd "github.com/provenance-io/provenance/cmd/provenanced/cmd"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
)

func TestDocGen(t *testing.T) {
tests := []struct {
name string
target string
createTarget bool
flags []string
err string
extensions []string
}{
{
name: "failure - no flags specified",
target: "tmp",
createTarget: true,
err: "at least one doc type must be specified",
},
{
name: "failure - unsupported flag format",
target: "tmp",
flags: []string{"--bad"},
createTarget: true,
err: "unknown flag: --bad",
},
{
name: "failure - invalid target directory",
target: "/tmp/tmp2/tmp3",
flags: []string{"--yaml"},
createTarget: false,
err: "mkdir %s: no such file or directory",
},
{
name: "failure - bad yaml value",
target: "tmp",
createTarget: true,
flags: []string{"--yaml=xyz"},
err: "invalid argument \"xyz\" for \"--yaml\" flag: strconv.ParseBool: parsing \"xyz\": invalid syntax",
},
{
name: "failure - bad rst value",
target: "tmp",
createTarget: true,
flags: []string{"--rst=xyz"},
err: "invalid argument \"xyz\" for \"--rst\" flag: strconv.ParseBool: parsing \"xyz\": invalid syntax",
},
{
name: "failure - bad markdown value",
target: "tmp",
createTarget: true,
flags: []string{"--markdown=xyz"},
err: "invalid argument \"xyz\" for \"--markdown\" flag: strconv.ParseBool: parsing \"xyz\": invalid syntax",
},
{
name: "failure - bad manpage value",
target: "tmp",
createTarget: true,
flags: []string{"--manpage=xyz"},
err: "invalid argument \"xyz\" for \"--manpage\" flag: strconv.ParseBool: parsing \"xyz\": invalid syntax",
},
{
name: "success - yaml is generated",
target: "tmp",
createTarget: true,
flags: []string{"--yaml"},
extensions: []string{".yaml"},
},
{
name: "success - rst is generated",
target: "tmp",
createTarget: true,
flags: []string{"--rst"},
extensions: []string{".rst"},
},
{
name: "success - manpage is generated",
target: "tmp",
createTarget: true,
flags: []string{"--manpage"},
extensions: []string{".1"},
},
{
name: "success - markdown is generated",
target: "tmp",
createTarget: true,
flags: []string{"--markdown"},
extensions: []string{".md"},
},
{
name: "success - multiple types supported",
target: "tmp",
createTarget: true,
flags: []string{"--markdown", "--yaml"},
extensions: []string{".md", ".yaml"},
},
{
name: "success - generates a new directory",
target: "tmp2",
createTarget: false,
flags: []string{"--yaml"},
extensions: []string{".md", ".yaml"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
home := t.TempDir()

targetPath := filepath.Join(home, tc.target)
if tc.createTarget {
require.NoError(t, os.Mkdir(targetPath, 0755), "Mkdir successfully created directory")
}

logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err, "Created default tendermint config")

appCodec := sdksim.MakeTestEncodingConfig().Codec
err = genutiltest.ExecInitCmd(testMbm, home, appCodec)
require.NoError(t, err, "Executed init command")

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := provenancecmd.GetDocGenCmd()
args := append([]string{targetPath}, tc.flags...)
cmd.SetArgs(args)

if len(tc.err) > 0 {
err := cmd.ExecuteContext(ctx)
require.Error(t, err, "should throw an error")
expected := tc.err
if strings.Contains(expected, "%s") {
expected = fmt.Sprintf(expected, targetPath)
}
require.Equal(t, expected, err.Error(), "should return the correct error")
files, err := os.ReadDir(targetPath)
if err != nil {
require.Equal(t, 0, len(files), "should not generate files when failed")
}
} else {
err := cmd.ExecuteContext(ctx)
require.NoError(t, err, "should not return an error")

files, err := os.ReadDir(targetPath)
require.NoError(t, err, "ReadDir should not return an error")
require.NotZero(t, len(files), "should generate files when successful")

for _, file := range files {
ext := filepath.Ext(file.Name())

contains := false
for _, extension := range tc.extensions {
contains = contains || ext == extension
}
require.True(t, contains, "should generate files with correct extension")
}
}

if _, err := os.Stat(targetPath); err != nil {
require.NoError(t, os.RemoveAll(targetPath), "RemoveAll should be able to remove the temporary target directory")
}
})
}
}
1 change: 1 addition & 0 deletions cmd/provenanced/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
AddMetaAddressCmd(),
snapshot.Cmd(newApp),
GetPreUpgradeCmd(),
GetDocGenCmd(),
)

fixDebugPubkeyRawTypeFlag(rootCmd)
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/otiai10/copy v1.12.0
github.com/rakyll/statik v0.1.7
github.com/regen-network/cosmos-proto v0.3.1
github.com/rs/zerolog v1.30.0
github.com/rs/zerolog v1.31.0
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand All @@ -33,7 +33,7 @@ require (
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0
golang.org/x/text v0.13.0
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98
google.golang.org/grpc v1.58.1
google.golang.org/grpc v1.58.2
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v2 v2.4.0

Expand Down Expand Up @@ -68,6 +68,7 @@ require (
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.6 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down Expand Up @@ -124,7 +125,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
Expand All @@ -144,6 +145,7 @@ require (
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -161,7 +163,7 @@ require (
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.126.0 // indirect
Expand Down
Loading

0 comments on commit f32f506

Please sign in to comment.