Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements of commands service init and service gendoc #823

Merged
merged 6 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (c *baseCmd) discardOutput() {
c.cmd.SetOutput(ioutil.Discard)
}

// getFirstOrDefault returns directory if args len is gt 0 or current directory.
func getFirstOrDefault(args []string) string {
// getFirstOrCurrentPath returns directory if args len is gt 0 or current directory.
func getFirstOrCurrentPath(args []string) string {
if len(args) > 0 {
return args[0]
}
Expand Down
2 changes: 1 addition & 1 deletion commands/service_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To get more information, see the [deploy page from the documentation](https://do
}

func (c *serviceDeployCmd) preRunE(cmd *cobra.Command, args []string) error {
c.path = getFirstOrDefault(args)
c.path = getFirstOrCurrentPath(args)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion commands/service_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newServiceDevCmd(e ServiceExecutor) *serviceDevCmd {
}

func (c *serviceDevCmd) preRunE(cmd *cobra.Command, args []string) error {
c.path = getFirstOrDefault(args)
c.path = getFirstOrCurrentPath(args)
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions commands/service_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ func newServiceDocsCmd(e ServiceExecutor) *serviceDocsCmd {
Use: "gen-doc",
Short: "Generate the documentation for the service in a README.md file",
Example: `mesg-core service gen-doc
mesg-core service gen-doc ./PATH_TO_SERVICE`,
mesg-core service gen-doc ./PATH_TO_SERVICE
mesg-core service gen-doc --force`,
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
PreRunE: c.preRunE,
RunE: c.runE,
})
c.cmd.Flags().BoolVarP(&c.force, "force", "f", c.force, "No confirmation. Will replace existing README.md file")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the use of this flag in the command

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic was already implemented but the flag was not exposed

Copy link
Contributor

@ilgooz ilgooz Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having force option is good for non interactive terminals. But we can change it to --yes because we use it in general, in other commands too. It exists for answering all terminal prompts as yes by default in a command action. But this improvement can be done in an another PR.

return c
}

func (c *serviceDocsCmd) preRunE(cmd *cobra.Command, args []string) error {
c.path = getFirstOrDefault(args)
c.path = getFirstOrCurrentPath(args)
readmePath := filepath.Join(c.path, "README.md")
if _, err := os.Stat(readmePath); !c.force && err == nil {
if err := survey.AskOne(&survey.Confirm{
Expand Down
35 changes: 9 additions & 26 deletions commands/service_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
type serviceInitCmd struct {
baseCmd

name string
templateURL string
templateName string
dir string
path string

e ServiceExecutor
}
Expand All @@ -36,22 +35,21 @@ func newServiceInitCmd(e ServiceExecutor) *serviceInitCmd {

To get more information, see the page [service file from the documentation](https://docs.mesg.com/guide/service/service-file.html)`,
Example: `mesg-core service init
mesg-core service init --name NAME
mesg-core service init --current`,
Args: cobra.NoArgs,
mesg-core service init ./PATH_TO_SERVICE
mesg-core service init --template TEMPLATE_URL`,
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
PreRunE: c.preRunE,
RunE: c.runE,
})
c.cmd.Flags().StringVar(&c.dir, "dir", c.dir, "Create the service in the directory")
c.cmd.Flags().StringVarP(&c.templateURL, "template", "t", c.templateURL, "Specify the template URL to use")
return c
}

func (c *serviceInitCmd) preRunE(cmd *cobra.Command, args []string) error {
if err := c.selectOutputDirectory(); err != nil {
return err
}
c.path = getFirstOrCurrentPath(args)
return c.getTemplateURL()
}

func (c *serviceInitCmd) getTemplateURL() error {
if c.templateURL != "" {
c.templateName = c.templateURL
return nil
Expand Down Expand Up @@ -107,30 +105,15 @@ func (c *serviceInitCmd) runE(cmd *cobra.Command, args []string) error {
err = c.e.ServiceInitDownloadTemplate(&servicetemplate.Template{
Name: c.templateName,
URL: c.templateURL,
}, c.dir)
}, c.path)
})
if err != nil {
return err
}
fmt.Printf("%s Service initialized in %q\n", pretty.SuccessSign, c.dir)
fmt.Printf("%s Service initialized in %q\n", pretty.SuccessSign, c.path)
return nil
}

func (c *serviceInitCmd) selectOutputDirectory() error {
if c.dir != "" {
return nil
}
defval := c.name
if defval == "" {
defval = "."
}

return survey.AskOne(&survey.Input{
Message: "Enter the output directory",
Default: defval,
}, &c.dir, nil)
}

func templatesToOptions(templates []*servicetemplate.Template) []string {
var options []string
for _, template := range templates {
Expand Down
3 changes: 0 additions & 3 deletions commands/service_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ func TestServiceInitCmdFlags(t *testing.T) {
flags := c.cmd.Flags()
require.Equal(t, flags.ShorthandLookup("t"), flags.Lookup("template"))

flags.Set("dir", "/")
require.Equal(t, "/", c.dir)

flags.Set("template", "github.com/mesg-foundation/awesome")
require.Equal(t, "github.com/mesg-foundation/awesome", c.templateURL)
}
2 changes: 1 addition & 1 deletion commands/service_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newServicePublishCmd(e ServiceExecutor) *servicePublishCmd {
}

func (c *servicePublishCmd) preRunE(cmd *cobra.Command, args []string) error {
c.path = getFirstOrDefault(args)
c.path = getFirstOrCurrentPath(args)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion commands/service_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mesg-core service validate ./SERVICE_FOLDER`,
}

func (c *serviceValidateCmd) preRunE(cmd *cobra.Command, args []string) error {
c.path = getFirstOrDefault(args)
c.path = getFirstOrCurrentPath(args)
return nil
}

Expand Down