Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Commit

Permalink
Reverse middlewares ordering.
Browse files Browse the repository at this point in the history
This ensure that the api client is configured before any call to the
api.

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera committed Jan 27, 2018
1 parent a213b58 commit 5a78f1e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (

func addCommands() {
middlewares := []middleware.Middleware{
middleware.DebugMiddleware,
middleware.LoggingMiddleware,
middleware.AuthMiddleware,
middleware.ClientMiddleware,
middleware.AuthMiddleware,
middleware.LoggingMiddleware,
middleware.DebugMiddleware,
}

loginMiddlewares := []middleware.Middleware{
middleware.DebugMiddleware,
middleware.LoggingMiddleware,
middleware.NoAuthMiddleware,
middleware.ClientMiddleware,
middleware.NoAuthMiddleware,
middleware.LoggingMiddleware,
middleware.DebugMiddleware,
}

siteMiddlewares := append(middlewares, middleware.SiteConfigMiddleware)
siteMiddlewares := append([]middleware.Middleware{middleware.SiteConfigMiddleware}, middlewares...)

rootCmd.AddCommand(deploy.Setup(siteMiddlewares))
rootCmd.AddCommand(assets.Setup(siteMiddlewares))
Expand Down
7 changes: 7 additions & 0 deletions commands/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func DebugMiddleware(cmd CommandFunc) CommandFunc {
logrus.SetLevel(logrus.DebugLevel)
logrus.WithFields(logrus.Fields{"command": c.Use, "arguments": args}).Debug("PreRun")

logrus.Debug("configure debug middleware")

dump, err := c.Root().Flags().GetBool("debug")
if err != nil {
return err
Expand Down Expand Up @@ -106,6 +108,7 @@ func DebugMiddleware(cmd CommandFunc) CommandFunc {

func LoggingMiddleware(cmd CommandFunc) CommandFunc {
return func(ctx context.Context, c *cobra.Command, args []string) error {
logrus.Debug("configure logging middleware")
entry := logrus.NewEntry(logrus.StandardLogger())
entry.Debugf("setup logger middleware: %v", entry.Level)
ctx = apiContext.WithLogger(ctx, entry)
Expand All @@ -116,6 +119,7 @@ func LoggingMiddleware(cmd CommandFunc) CommandFunc {

func AuthMiddleware(cmd CommandFunc) CommandFunc {
return func(ctx context.Context, c *cobra.Command, args []string) error {
logrus.Debug("configure auth middleware")
creds := auth.ClientCredentials()
logrus.WithField("credentials", creds).Debug("setup credentials")

Expand All @@ -127,6 +131,7 @@ func AuthMiddleware(cmd CommandFunc) CommandFunc {

func NoAuthMiddleware(cmd CommandFunc) CommandFunc {
return func(ctx context.Context, c *cobra.Command, args []string) error {
logrus.Debug("configure no auth middleware")
creds := auth.NoCredentials()
logrus.WithField("credentials", creds).Debug("setup credentials")

Expand All @@ -138,6 +143,7 @@ func NoAuthMiddleware(cmd CommandFunc) CommandFunc {

func ClientMiddleware(cmd CommandFunc) CommandFunc {
return func(ctx context.Context, c *cobra.Command, args []string) error {
logrus.Debug("configure client middleware")
var transport *apiClient.Runtime

if endpoint := c.Flag("endpoint"); endpoint != nil {
Expand Down Expand Up @@ -180,6 +186,7 @@ func ClientMiddleware(cmd CommandFunc) CommandFunc {

func SiteConfigMiddleware(cmd CommandFunc) CommandFunc {
return func(ctx context.Context, c *cobra.Command, args []string) error {
logrus.Debug("configure site middleware")
var siteId string
if siteIdFlag := c.Flag("site-id"); siteIdFlag != nil {
siteId = siteIdFlag.Value.String()
Expand Down
2 changes: 1 addition & 1 deletion commands/sites/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func setupUpdateCommand(middlewares []middleware.Middleware) *cobra.Command {
ccmd.Flags().StringVarP(&cmd.password, "password", "p", "", "site's access password")
ccmd.Flags().BoolVarP(&cmd.forceTLS, "force-tls", "t", false, "force TLS connections")

siteMiddlewares := append(middlewares, middleware.SiteConfigMiddleware)
siteMiddlewares := append([]middleware.Middleware{middleware.SiteConfigMiddleware}, middlewares...)

return middleware.SetupCommand(ccmd, cmd.updateSite, siteMiddlewares)
}
Expand Down

0 comments on commit 5a78f1e

Please sign in to comment.