-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to decouple domain from communication to the user.
- Loading branch information
Showing
20 changed files
with
212 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mantil-io/mantil/cli/build" | ||
"github.com/mantil-io/mantil/cli/controller" | ||
"github.com/mantil-io/mantil/cli/log" | ||
"github.com/mantil-io/mantil/cli/ui" | ||
"github.com/pkg/errors" | ||
"github.com/mantil-io/mantil/workspace" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/cobra/doc" | ||
) | ||
|
@@ -33,8 +36,9 @@ func Execute() error { | |
return err | ||
} | ||
|
||
showError(ec, err) | ||
// in other cases show error without usage | ||
ui.Error(err) | ||
//ui.Error(err) | ||
return err | ||
} | ||
|
||
|
@@ -50,7 +54,7 @@ func root() *cobra.Command { | |
cmd.PersistentFlags().Bool("no-color", false, "don't use colors in output") | ||
cmd.PersistentFlags().Bool("help", false, "show command help") // move help to global commands | ||
cmd.Flags().Bool("version", false, "show mantil version") // remove -v shortcut for version | ||
cmd.SetUsageTemplate(usageTemplate()) | ||
cmd.SetUsageTemplate(usageTemplate("")) | ||
|
||
add := func(factory func() *cobra.Command) { | ||
sub := factory() | ||
|
@@ -84,8 +88,13 @@ func GenDoc(dir string) error { | |
return doc.GenMarkdownTree(cmd, dir) | ||
} | ||
|
||
func usageTemplate() string { | ||
str := `\bUSAGE\c{{if .Runnable}} | ||
func usageTemplate(argumentsUsage string) string { | ||
if argumentsUsage != "" { | ||
argumentsUsage = fmt.Sprintf(` | ||
\bARGUMENTS\c%s`, argumentsUsage) | ||
} | ||
|
||
str := fmt.Sprintf(`\bUSAGE\c{{if .Runnable}} | ||
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} | ||
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}} | ||
|
@@ -96,7 +105,7 @@ func usageTemplate() string { | |
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}} | ||
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}{{if .HasAvailableLocalFlags}} | ||
%s | ||
\bFLAGS\c | ||
{{.LocalFlags.FlagUsagesWrapped 120 | trimTrailingWhitespaces}}{{end}}{{if .HasExample}} | ||
|
@@ -112,11 +121,19 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e | |
\bLEARN MORE\c | ||
Visit https://docs.mantil.io to learn more. | ||
For further support contact us at [email protected]. | ||
` | ||
`, argumentsUsage) | ||
|
||
return boldize(str) | ||
} | ||
|
||
func setUsageTemplate(cmd *cobra.Command, argumentsUsage string) { | ||
cmd.SetUsageTemplate(usageTemplate(argumentsUsage)) | ||
} | ||
|
||
func showNextSteps(str string) { | ||
fmt.Printf("==> Next steps:%s\n", str) | ||
} | ||
|
||
func boldize(str string) string { | ||
return strings.ReplaceAll(strings.ReplaceAll(str, | ||
`\b`, bold), | ||
|
@@ -127,3 +144,44 @@ const ( | |
bold = "\033[1m" | ||
clear = "\033[0m" | ||
) | ||
|
||
func showError(cmd *cobra.Command, err error) { | ||
if err == nil { | ||
return | ||
} | ||
log.Error(err) // write to log file | ||
|
||
var aee *workspace.AccountExistsError | ||
if errors.As(err, &aee) { | ||
ui.Errorf("account '%s' already exists", aee.Name) | ||
if aee.Name == workspace.DefaultAccountName { | ||
fmt.Printf(` | ||
'%s' is default account name and it is already used. | ||
Please specify another name in mantil command. | ||
`, aee.Name) | ||
} | ||
return | ||
} | ||
|
||
var rerr *workspace.ErrReservedName | ||
if errors.As(err, &rerr) { | ||
ui.Errorf("'%s' is reserved name", rerr.Name) | ||
return | ||
} | ||
var verr workspace.ValidationError | ||
if errors.As(err, &verr) { | ||
ui.Errorf(verr.UserMessage()) | ||
return | ||
} | ||
|
||
var gbe *log.GoBuildError | ||
if errors.As(err, &gbe) { | ||
for _, line := range gbe.Lines { | ||
ui.ErrorLine(line) | ||
} | ||
return | ||
} | ||
//ok, _ := cmd.InheritedFlags().GetBool("no-color") | ||
|
||
ui.Error(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.