Skip to content

Commit 14f62e0

Browse files
committed
add CLI
1 parent be6aa82 commit 14f62e0

File tree

5 files changed

+137
-15
lines changed

5 files changed

+137
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
# prodoc
3+
4+
[![Build Status](https://img.shields.io/travis/coreos/prodoc.svg?style=flat-square)][cistat] [![Godoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][prodoc-godoc]
5+
26
prodoc generates Protocol Buffer documentation.
37

48
Note that parser only understands the minimum syntax

main.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,71 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// prodoc generates Protocol Buffer documentation.
16+
//
17+
// Usage:
18+
// prodoc [flags]
19+
//
20+
// Flags:
21+
// -o, --language-options value language options in field descriptions (default [Go,C++,Java,Python])
22+
// -p, --target-path string file path to save the documentation
23+
// -t, --title string title of documentation
24+
//
1525
package main
1626

17-
func main() {
27+
import (
28+
"fmt"
29+
"log"
30+
"os"
31+
32+
"github.com/coreos/prodoc/parse"
33+
"github.com/spf13/cobra"
34+
)
35+
36+
var (
37+
rootCommand = &cobra.Command{
38+
Use: "prodoc",
39+
Short: "prodoc generates Protocol Buffer documentation.",
40+
RunE: CommandFunc,
41+
}
42+
43+
title string
44+
targetDir string
45+
targetPath string
46+
languageOptions []string
47+
)
1848

49+
func init() {
50+
cobra.EnablePrefixMatching = true
51+
}
52+
53+
func init() {
54+
rootCommand.PersistentFlags().StringVarP(&title, "title", "t", "", "title of documentation")
55+
rootCommand.PersistentFlags().StringVarP(&targetPath, "target-path", "p", "", "file path to save the documentation")
56+
rootCommand.PersistentFlags().StringSliceVarP(&languageOptions, "language-options", "o", []string{"Go", "C++", "Java", "Python"}, "language options in field descriptions")
57+
}
58+
59+
func CommandFunc(cmd *cobra.Command, args []string) error {
60+
if len(args) != 1 {
61+
return fmt.Errorf("need 1 argument of target directory, got %q", args)
62+
}
63+
targetDir := args[0]
64+
log.Println("opening", targetDir)
65+
proto, err := parse.ReadDir(targetDir)
66+
if err != nil {
67+
return err
68+
}
69+
err = proto.Markdown(title, targetPath, languageOptions...)
70+
if err != nil {
71+
return err
72+
}
73+
log.Printf("saved at %s", targetPath)
74+
return nil
75+
}
76+
77+
func main() {
78+
if err := rootCommand.Execute(); err != nil {
79+
fmt.Fprintln(os.Stdout, err)
80+
os.Exit(1)
81+
}
1982
}

parse/command.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package parse
16+
17+
import (
18+
"log"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
var (
24+
Command = &cobra.Command{
25+
Use: "parse",
26+
Short: "parses Protocol Buffer files to generate documentation.",
27+
}
28+
)
29+
30+
var (
31+
title string
32+
targetDir string
33+
targetPath string
34+
languageOptions []string
35+
)
36+
37+
func init() {
38+
cobra.EnablePrefixMatching = true
39+
}
40+
41+
func init() {
42+
Command.PersistentFlags().StringVarP(&title, "title", "t", "", "title of documentation")
43+
Command.PersistentFlags().StringVarP(&targetDir, "target-directory", "d", "", "target directory with .proto files")
44+
Command.PersistentFlags().StringVarP(&targetPath, "target-path", "p", "", "file path to save the documentation")
45+
Command.PersistentFlags().StringSliceVarP(&languageOptions, "language-options", "o", []string{"Go", "C++", "Java", "Python"}, "language options in field descriptions")
46+
}
47+
48+
func CommandFunc(cmd *cobra.Command, args []string) error {
49+
log.Println("opening", targetDir)
50+
proto, err := ReadDir(targetDir)
51+
if err != nil {
52+
return err
53+
}
54+
return proto.Markdown(title, targetPath, languageOptions...)
55+
}

parse/markdown.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (p *Proto) Markdown(title, fpath string, lopts ...string) error {
5252
ts = elem.UserDefinedProtoType
5353
}
5454
if elem.Repeated {
55-
ts = "[]" + ts
55+
ts = "(slice of) " + ts
5656
}
5757
line := fmt.Sprintf("| %s | %s | %s |", elem.Name, elem.Description, ts)
5858
for _, lopt := range lopts {
@@ -62,7 +62,7 @@ func (p *Proto) Markdown(title, fpath string, lopts ...string) error {
6262
}
6363
formatSt := " %s |"
6464
if elem.Repeated {
65-
formatSt = " []%s |"
65+
formatSt = " (slice of) %s |"
6666
}
6767
switch lopt {
6868
case "C++":

parse/testdata/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| Field | Description | Type | Go | Java | Python | C++ |
2828
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
2929
| header | | ResponseHeader | | | | |
30-
| alarms | | []AlarmMember | | | | |
30+
| alarms | | slice of AlarmMember | | | | |
3131

3232

3333
<br>
@@ -473,8 +473,8 @@ An InternalRaftRequest is the union of all requests which can be sent via raft.
473473
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
474474
| ID | | uint64 | uint64 | long | int/long | uint64 |
475475
| name | If the member is not started, name will be an empty string. | string | string | String | str/unicode | string |
476-
| peerURLs | | []string | []string | []String | []str/unicode | []string |
477-
| clientURLs | If the member is not started, client_URLs will be an zero length string array. | []string | []string | []String | []str/unicode | []string |
476+
| peerURLs | | slice of string | []string | []String | []str/unicode | []string |
477+
| clientURLs | If the member is not started, client_URLs will be an zero length string array. | slice of string | []string | []String | []str/unicode | []string |
478478

479479

480480
<br>
@@ -483,7 +483,7 @@ An InternalRaftRequest is the union of all requests which can be sent via raft.
483483

484484
| Field | Description | Type | Go | Java | Python | C++ |
485485
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
486-
| peerURLs | | []string | []string | []String | []str/unicode | []string |
486+
| peerURLs | | slice of string | []string | []String | []str/unicode | []string |
487487

488488

489489
<br>
@@ -511,7 +511,7 @@ An InternalRaftRequest is the union of all requests which can be sent via raft.
511511
| Field | Description | Type | Go | Java | Python | C++ |
512512
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
513513
| header | | ResponseHeader | | | | |
514-
| members | | []Member | | | | |
514+
| members | | slice of Member | | | | |
515515

516516

517517
<br>
@@ -539,7 +539,7 @@ An InternalRaftRequest is the union of all requests which can be sent via raft.
539539
| Field | Description | Type | Go | Java | Python | C++ |
540540
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
541541
| ID | | uint64 | uint64 | long | int/long | uint64 |
542-
| peerURLs | | []string | []string | []String | []str/unicode | []string |
542+
| peerURLs | | slice of string | []string | []String | []str/unicode | []string |
543543

544544

545545
<br>
@@ -603,7 +603,7 @@ An InternalRaftRequest is the union of all requests which can be sent via raft.
603603
| Field | Description | Type | Go | Java | Python | C++ |
604604
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
605605
| header | | ResponseHeader | | | | |
606-
| kvs | | []storagepb.KeyValue | | | | |
606+
| kvs | | slice of storagepb.KeyValue | | | | |
607607
| more | more indicates if there are more keys to return in the requested range. | bool | bool | boolean | boolean | bool |
608608

609609

@@ -717,9 +717,9 @@ If the comparisons succeed, then the success requests will be processed in order
717717

718718
| Field | Description | Type | Go | Java | Python | C++ |
719719
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
720-
| compare | | []Compare | | | | |
721-
| success | | []RequestUnion | | | | |
722-
| failure | | []RequestUnion | | | | |
720+
| compare | | slice of Compare | | | | |
721+
| success | | slice of RequestUnion | | | | |
722+
| failure | | slice of RequestUnion | | | | |
723723

724724

725725
<br>
@@ -730,7 +730,7 @@ If the comparisons succeed, then the success requests will be processed in order
730730
| ----- | ----------- | ---- | --- | ---- | ------ | --- |
731731
| header | | ResponseHeader | | | | |
732732
| succeeded | | bool | bool | boolean | boolean | bool |
733-
| responses | | []ResponseUnion | | | | |
733+
| responses | | slice of ResponseUnion | | | | |
734734

735735

736736
<br>
@@ -776,7 +776,7 @@ If the comparisons succeed, then the success requests will be processed in order
776776
| created | If the response is for a create watch request, created is set to true. Client should record the watch_id and prepare for receiving events for that watching from the same stream. All events sent to the created watching will attach with the same watch_id. | bool | bool | boolean | boolean | bool |
777777
| canceled | If the response is for a cancel watch request, cancel is set to true. No further events will be sent to the canceled watching. | bool | bool | boolean | boolean | bool |
778778
| compact_revision | CompactRevision is set to the minimum index if a watching tries to watch at a compacted index. This happens when creating a watching at a compacted revision or the watching cannot catch up with the progress of the KV. Client should treat the watching as canceled and should not try to create any watching with same start_revision again. | int64 | int64 | long | int/long | int64 |
779-
| events | | []storagepb.Event | | | | |
779+
| events | | slice of storagepb.Event | | | | |
780780

781781

782782
<br>

0 commit comments

Comments
 (0)