Skip to content

Commit

Permalink
printer: add pager option (mattermost#377)
Browse files Browse the repository at this point in the history
* printer: add pager option

* add pager flag

* Apply suggestions from code review

Co-authored-by: Miguel de la Cruz <[email protected]>

* add print warning function

* Apply suggestions from code review

Co-authored-by: Claudio Costa <[email protected]>

* reflect review comments

* remove progress indicator

Co-authored-by: Miguel de la Cruz <[email protected]>
Co-authored-by: Claudio Costa <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2021
1 parent 1235c62 commit 24c8d34
Show file tree
Hide file tree
Showing 180 changed files with 476 additions and 30 deletions.
9 changes: 3 additions & 6 deletions commands/auth_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"sync"

"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/viper"

Expand Down Expand Up @@ -84,11 +83,9 @@ func resolveConfigFilePath() string {
if viper.IsSet("config-path") {
if !suppressWarnings {
once.Do(func() {
printer.PrintError(color.YellowString("WARNING: Since mmctl v6 we have been deprecated the --config-path and started to use --config flag instead."))
printer.PrintError(color.YellowString("Please use --config flag to set config file. (note that --config-path was pointing to a directory)\n"))
printer.PrintError(color.YellowString("After moving your config file to new directory, please unset the --config-path flag or MMCTL_CONFIG_PATH environment variable.\n"))

printer.Flush()
printer.PrintWarning("Since mmctl v6 we have been deprecated the --config-path and started to use --config flag instead.\n" +
"Please use --config flag to set config file. (note that --config-path was pointing to a directory)\n\n" +
"After moving your config file to new directory, please unset the --config-path flag or MMCTL_CONFIG_PATH environment variable.\n")
})
}

Expand Down
5 changes: 3 additions & 2 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"runtime"
"strings"

"github.com/fatih/color"
"github.com/mattermost/mattermost-server/v6/model"

"github.com/pkg/errors"
Expand Down Expand Up @@ -46,6 +45,7 @@ func withClient(fn func(c client.Client, cmd *cobra.Command, args []string) erro
if err != nil {
return err
}
printer.SetServerAddres("local instance")
return fn(c, cmd, args)
}

Expand All @@ -60,10 +60,11 @@ func withClient(fn func(c client.Client, cmd *cobra.Command, args []string) erro
os.Exit(1)
}
if !viper.GetBool("suppress-warnings") {
printer.PrintError(color.YellowString("WARNING: server version " + serverVersion + " doesn't match mmctl version " + Version))
printer.PrintWarning("server version " + serverVersion + " doesn't match mmctl version " + Version)
}
}

printer.SetServerAddres(c.ApiUrl)
return fn(c, cmd, args)
}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func printPost(c client.Client, post *model.Post, usernames map[string]string, s
} else {
printer.PrintT(fmt.Sprintf("\u001b[34;1m[%s]\u001b[0m {{.Message}}", username), post)
}
printer.Flush()
}

func postListCmdF(c client.Client, cmd *cobra.Command, args []string) error {
printer.SetSingle(true)

channel := getChannelFromChannelArg(c, args[0])
if channel == nil {
return errors.New("Unable to find channel '" + args[0] + "'")
Expand Down
25 changes: 24 additions & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package commands

import (
"os"
"path/filepath"
"runtime/debug"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,11 +43,27 @@ func Run(args []string) error {
_ = viper.BindPFlag("insecure-tls-version", RootCmd.PersistentFlags().Lookup("insecure-tls-version"))
RootCmd.PersistentFlags().Bool("local", false, "allows communicating with the server through a unix socket")
_ = viper.BindPFlag("local", RootCmd.PersistentFlags().Lookup("local"))
RootCmd.PersistentFlags().Bool("short-stat", false, "short stat will provide useful statistical data")
_ = RootCmd.PersistentFlags().MarkHidden("short-stat")
RootCmd.PersistentFlags().Bool("no-stat", false, "the statistical data won't be displayed")
_ = RootCmd.PersistentFlags().MarkHidden("no-stat")
RootCmd.PersistentFlags().Bool("disable-pager", false, "disables paged output")
_ = viper.BindPFlag("disable-pager", RootCmd.PersistentFlags().Lookup("disable-pager"))
RootCmd.PersistentFlags().Bool("quiet", false, "prevent mmctl to generate output for the commands")
_ = viper.BindPFlag("quiet", RootCmd.PersistentFlags().Lookup("quiet"))

RootCmd.SetArgs(args)

defer func() {
if x := recover(); x != nil {
printer.PrintError("Uh oh! Something unexpected happened :( Would you mind reporting it?\n")
printer.PrintError(`https://github.com/mattermost/mmctl/issues/new?title=%5Bbug%5D%20panic%20on%20mmctl%20v` + Version + "&body=%3C!---%20Please%20provide%20the%20stack%20trace%20--%3E\n")
printer.PrintError(string(debug.Stack()))

os.Exit(1)
}
}()

return RootCmd.Execute()
}

Expand All @@ -56,6 +74,11 @@ var RootCmd = &cobra.Command{
DisableAutoGenTag: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
format := viper.GetString("format")
if viper.GetBool("disable-pager") {
printer.OverrideEnablePager(false)
}

printer.SetCommand(cmd)
isJSON := viper.GetBool("json")
if isJSON || format == printer.FormatJSON {
printer.SetFormat(printer.FormatJSON)
Expand All @@ -66,7 +89,7 @@ var RootCmd = &cobra.Command{
printer.SetQuiet(quiet)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
printer.Flush()
_ = printer.Flush()
},
SilenceUsage: true,
}
3 changes: 1 addition & 2 deletions commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func deleteAllUsersCmdF(c client.Client, cmd *cobra.Command, args []string) erro
return err
}

printer.Print("All users successfully deleted")
defer printer.Print("All users successfully deleted")

return nil
}
Expand Down Expand Up @@ -738,7 +738,6 @@ func listUsersCmdF(c client.Client, command *cobra.Command, args []string) error
}

tpl := `{{.Id}}: {{.Username}} ({{.Email}})`

for {
var users []*model.User
var err error
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Options
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
-h, --help help for mmctl
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_clean.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_login.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_renew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_auth_set.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_assign.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_create.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_disable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_enable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_bot_update.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_archive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_create.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_make-private.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_modify.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_move.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_rename.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_unarchive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_users_add.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_channel_users_remove.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
1 change: 1 addition & 0 deletions docs/mmctl_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Options inherited from parent commands
::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
Expand Down
Loading

0 comments on commit 24c8d34

Please sign in to comment.