From b2e56cc774914e60cfb57dcdade1134f0e9d906c Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 20 Jul 2020 10:05:42 +0800 Subject: [PATCH] Cleanup code --- bridge/bridges.go | 1 + bridge/core/bridge.go | 1 + commands/bridge_configure.go | 86 ----------------- ...ure_example.go => bridge_configure_doc.go} | 40 ++++---- doc/gen_docs.go | 93 ++++++++++++++++++- doc/man/git-bug-bridge-configure.1 | 38 ++++---- doc/md/git-bug_bridge_configure.md | 38 ++++---- 7 files changed, 155 insertions(+), 142 deletions(-) rename commands/{bridge_configure_example.go => bridge_configure_doc.go} (59%) diff --git a/bridge/bridges.go b/bridge/bridges.go index 7bd0e76d74d06f7e1bbe83528c9755cf7c7287d3..92df68d6cb68748e2233a55f535f8e9c0640d570 100644 --- a/bridge/bridges.go +++ b/bridge/bridges.go @@ -23,6 +23,7 @@ func Targets() []string { return core.Targets() } +// ValidParams returns the valid input parameters for configuring a target bridge func ValidParams(target string) ([]string, error) { return core.ValidParams(target) } diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go index 70693ced436226c09d500203801c9add6f899663..21fd86ab39e45d5d5fe4db2d004e34855bacd9c9 100644 --- a/bridge/core/bridge.go +++ b/bridge/core/bridge.go @@ -83,6 +83,7 @@ func Targets() []string { return result } +// ValidParams returns the valid input parameters for configuring a target bridge func ValidParams(target string) ([]string, error) { validParams, ok := bridgeValidParams[target] if !ok { diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index 162518943d7a27bd7ff5e83bf8df91e0aa93bb59..276dc72f01fffe87ba3ea187a69c70719b2f8294 100644 --- a/commands/bridge_configure.go +++ b/commands/bridge_configure.go @@ -23,92 +23,6 @@ type bridgeConfigureOptions struct { tokenStdin bool } -var bridgeExampleFilePath = "commands/bridge_configure_example.go" - -func GenBridgeConfig() error { - var exampleText strings.Builder - exampleText.WriteString("`") - exampleText.WriteString(`# Interactive example -[1]: github -[2]: gitlab -[3]: jira -[4]: launchpad-preview - -target: 1 -name [default]: default - -Detected projects: -[1]: github.com/a-hilaly/git-bug -[2]: github.com/MichaelMure/git-bug - -[0]: Another project - -Select option: 1 - -[1]: user provided token -[2]: interactive token creation -Select option: 1 - -You can generate a new token by visiting https://github.com/settings/tokens. -Choose 'Generate new token' and set the necessary access scope for your repository. - -The access scope depend on the type of repository. -Public: - - 'public_repo': to be able to read public repositories -Private: - - 'repo' : to be able to read private repositories - -Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700 -Successfully configured bridge: default`) - targets := bridge.Targets() - for i, b := range targets { - _, err := fmt.Fprintf(&exampleText, "# For %s\ngit bug bridge configure \\\n", strings.Title(strings.Split(b, ".")[0])) - if err != nil { - return err - } - params, err := bridge.ValidParams(b) - if err != nil { - return fmt.Errorf("Encountered error when getting bridge parameters: %q", err) - } - for _, param := range params { - _, err = fmt.Fprintf(&exampleText, " --%s=PLACEHOLDERTEXT \\\n", param) - if err != nil { - return err - } - } - if i < len(targets)-1 { - _, err = fmt.Fprintf(&exampleText, "\n\n") - if err != nil { - return err - } - } - } - exampleText.WriteString("`") - - _ = os.Remove(bridgeExampleFilePath) - - f, err := os.Create(bridgeExampleFilePath) - if err != nil { - return err - } - defer f.Close() - - _, err = f.WriteString(`// Code generated by go generate; DO NOT EDIT. - -package commands - -var bridgeConfigureExample =`) - if err != nil { - return err - } - _, err = f.WriteString(exampleText.String()) - if err != nil { - return err - } - - return nil -} - func newBridgeConfigureCommand() *cobra.Command { env := newEnv() options := bridgeConfigureOptions{} diff --git a/commands/bridge_configure_example.go b/commands/bridge_configure_doc.go similarity index 59% rename from commands/bridge_configure_example.go rename to commands/bridge_configure_doc.go index 64cf4c783ba6aac23bf8c99f79403e690a368a58..bb71665a6a54dda7480116c8debce7d11c458b29 100644 --- a/commands/bridge_configure_example.go +++ b/commands/bridge_configure_doc.go @@ -1,4 +1,4 @@ -// Code generated by go generate; DO NOT EDIT. +// Code generated by doc/gen_docs.go; DO NOT EDIT. package commands @@ -33,35 +33,37 @@ Private: - 'repo' : to be able to read private repositories Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700 -Successfully configured bridge: default# For Github +Successfully configured bridge: default + +# For Github git bug bridge configure \ - --URL= \ - --Login= \ - --CredPrefix= \ - --TokenRaw= \ - --Owner= \ - --Project= \ + --TokenRaw=PLACEHOLDERTEXT \ + --Owner=PLACEHOLDERTEXT \ + --Project=PLACEHOLDERTEXT \ + --URL=PLACEHOLDERTEXT \ + --Login=PLACEHOLDERTEXT \ + --CredPrefix=PLACEHOLDERTEXT \ # For Gitlab git bug bridge configure \ - --BaseURL= \ - --Login= \ - --CredPrefix= \ - --TokenRaw= \ - --URL= \ + --Login=PLACEHOLDERTEXT \ + --CredPrefix=PLACEHOLDERTEXT \ + --TokenRaw=PLACEHOLDERTEXT \ + --URL=PLACEHOLDERTEXT \ + --BaseURL=PLACEHOLDERTEXT \ # For Jira git bug bridge configure \ - --Project= \ - --BaseURL= \ - --Login= \ - --CredPrefix= \ + --BaseURL=PLACEHOLDERTEXT \ + --Login=PLACEHOLDERTEXT \ + --CredPrefix=PLACEHOLDERTEXT \ + --Project=PLACEHOLDERTEXT \ # For Launchpad-Preview git bug bridge configure \ - --URL= \ - --Project= \ + --URL=PLACEHOLDERTEXT \ + --Project=PLACEHOLDERTEXT \ ` diff --git a/doc/gen_docs.go b/doc/gen_docs.go index 681a2cda46c81feae88cd9d08b7faeef9b1a0ae0..aa337598f911ddf6b6d3ab361f8f7d34be4a4083 100644 --- a/doc/gen_docs.go +++ b/doc/gen_docs.go @@ -5,18 +5,23 @@ import ( "os" "path" "path/filepath" + "strings" "time" + "github.com/go-errors/errors" "github.com/spf13/cobra/doc" + "github.com/MichaelMure/git-bug/bridge" "github.com/MichaelMure/git-bug/commands" ) +const bridgeExampleFilePath = "commands/bridge_configure_doc.go" + func main() { fmt.Println("Generating documentation ...") tasks := map[string]func() error{ - "BridgeConfig": commands.GenBridgeConfig, + "BridgeConfig": genBridgeConfig, "ManPage": genManPage, "Markdown": genMarkdown, } @@ -40,6 +45,92 @@ func main() { // wg.Wait() } +func genBridgeConfig() error { + var exampleText strings.Builder + exampleText.WriteString("`") + exampleText.WriteString(`# Interactive example +[1]: github +[2]: gitlab +[3]: jira +[4]: launchpad-preview + +target: 1 +name [default]: default + +Detected projects: +[1]: github.com/a-hilaly/git-bug +[2]: github.com/MichaelMure/git-bug + +[0]: Another project + +Select option: 1 + +[1]: user provided token +[2]: interactive token creation +Select option: 1 + +You can generate a new token by visiting https://github.com/settings/tokens. +Choose 'Generate new token' and set the necessary access scope for your repository. + +The access scope depend on the type of repository. +Public: + - 'public_repo': to be able to read public repositories +Private: + - 'repo' : to be able to read private repositories + +Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700 +Successfully configured bridge: default + +`) + targets := bridge.Targets() + for i, b := range targets { + if i != 0 { + _, err := exampleText.WriteString("\n\n") + if err != nil { + return err + } + } + _, err := fmt.Fprintf(&exampleText, "# For %s\ngit bug bridge configure \\\n", strings.Title(strings.Split(b, ".")[0])) + if err != nil { + return err + } + params, err := bridge.ValidParams(b) + if err != nil { + return errors.WrapPrefix(err, "bridge parameters", 0) + } + for _, param := range params { + _, err = fmt.Fprintf(&exampleText, " --%s=PLACEHOLDERTEXT \\\n", param) + if err != nil { + return err + } + } + } + exampleText.WriteString("`") + + _ = os.Remove(bridgeExampleFilePath) + + f, err := os.Create(bridgeExampleFilePath) + if err != nil { + return err + } + defer f.Close() + + _, err = f.WriteString(`// Code generated by doc/gen_docs.go; DO NOT EDIT. + +package commands + +var bridgeConfigureExample =`) + if err != nil { + return err + } + _, err = f.WriteString(exampleText.String()) + if err != nil { + return err + } + + return nil +} + func genManPage() error { cwd, _ := os.Getwd() dir := path.Join(cwd, "doc", "man") diff --git a/doc/man/git-bug-bridge-configure.1 b/doc/man/git-bug-bridge-configure.1 index ff996b227bd8ef9bd6254d251395a91fd5a65765..45853ad795c17111c515b82075ed1d77a9fcdd6c 100644 --- a/doc/man/git-bug-bridge-configure.1 +++ b/doc/man/git-bug-bridge-configure.1 @@ -105,37 +105,39 @@ Private: \- 'repo' : to be able to read private repositories Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700 -Successfully configured bridge: default# For Github +Successfully configured bridge: default + +# For Github git bug bridge configure \\ - \-\-%!s(int=0)= \\ - \-\-%!s(int=1)= \\ - \-\-%!s(int=2)= \\ - \-\-%!s(int=3)= \\ - \-\-%!s(int=4)= \\ - \-\-%!s(int=5)= \\ + \-\-URL= \\ + \-\-Login= \\ + \-\-CredPrefix= \\ + \-\-TokenRaw= \\ + \-\-Owner= \\ + \-\-Project= \\ # For Gitlab git bug bridge configure \\ - \-\-%!s(int=0)= \\ - \-\-%!s(int=1)= \\ - \-\-%!s(int=2)= \\ - \-\-%!s(int=3)= \\ - \-\-%!s(int=4)= \\ + \-\-BaseURL= \\ + \-\-Login= \\ + \-\-CredPrefix= \\ + \-\-TokenRaw= \\ + \-\-URL= \\ # For Jira git bug bridge configure \\ - \-\-%!s(int=0)= \\ - \-\-%!s(int=1)= \\ - \-\-%!s(int=2)= \\ - \-\-%!s(int=3)= \\ + \-\-Project= \\ + \-\-BaseURL= \\ + \-\-Login= \\ + \-\-CredPrefix= \\ # For Launchpad\-Preview git bug bridge configure \\ - \-\-%!s(int=0)= \\ - \-\-%!s(int=1)= \\ + \-\-URL= \\ + \-\-Project= \\ .fi diff --git a/doc/md/git-bug_bridge_configure.md b/doc/md/git-bug_bridge_configure.md index e64fa1b17876a5b592edb802eb379e02fde419b2..922e8dd84d5fb26776b9a240284b3cf96820792f 100644 --- a/doc/md/git-bug_bridge_configure.md +++ b/doc/md/git-bug_bridge_configure.md @@ -44,37 +44,39 @@ Private: - 'repo' : to be able to read private repositories Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700 -Successfully configured bridge: default# For Github +Successfully configured bridge: default + +# For Github git bug bridge configure \ - --%!s(int=0)= \ - --%!s(int=1)= \ - --%!s(int=2)= \ - --%!s(int=3)= \ - --%!s(int=4)= \ - --%!s(int=5)= \ + --URL= \ + --Login= \ + --CredPrefix= \ + --TokenRaw= \ + --Owner= \ + --Project= \ # For Gitlab git bug bridge configure \ - --%!s(int=0)= \ - --%!s(int=1)= \ - --%!s(int=2)= \ - --%!s(int=3)= \ - --%!s(int=4)= \ + --BaseURL= \ + --Login= \ + --CredPrefix= \ + --TokenRaw= \ + --URL= \ # For Jira git bug bridge configure \ - --%!s(int=0)= \ - --%!s(int=1)= \ - --%!s(int=2)= \ - --%!s(int=3)= \ + --Project= \ + --BaseURL= \ + --Login= \ + --CredPrefix= \ # For Launchpad-Preview git bug bridge configure \ - --%!s(int=0)= \ - --%!s(int=1)= \ + --URL= \ + --Project= \ ```