-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix typo in logs Signed-off-by: Utku Ozdemir <[email protected]> * Print fancy logs with emojis Signed-off-by: Utku Ozdemir <[email protected]> * Add command-line demo to README Signed-off-by: Utku Ozdemir <[email protected]> * docs: update demo and readme Signed-off-by: Utku Ozdemir <[email protected]> * Do not print progress bar when log format is not fancy Signed-off-by: Utku Ozdemir <[email protected]> * test: fix tests Signed-off-by: Utku Ozdemir <[email protected]> * test: fix tests Signed-off-by: Utku Ozdemir <[email protected]> * test: add tests for logger Signed-off-by: Utku Ozdemir <[email protected]> * test: use debug level json logging in an integration test Signed-off-by: Utku Ozdemir <[email protected]> * test: fix log test Signed-off-by: Utku Ozdemir <[email protected]> * test: add tests for progress parsing Signed-off-by: Utku Ozdemir <[email protected]>
- Loading branch information
1 parent
3f39c0c
commit 2c514ed
Showing
20 changed files
with
335 additions
and
41 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,20 +1,27 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
"github.com/utkuozdemir/pv-migrate/engine" | ||
applog "github.com/utkuozdemir/pv-migrate/internal/log" | ||
"github.com/utkuozdemir/pv-migrate/internal/rsync" | ||
"github.com/utkuozdemir/pv-migrate/internal/strategy" | ||
"github.com/utkuozdemir/pv-migrate/migration" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type cliAppContextKey string | ||
|
||
const ( | ||
authorName = "Utku Ozdemir" | ||
authorEmail = "[email protected]" | ||
CommandMigrate = "migrate" | ||
FlagLogLevel = "log-level" | ||
FlagLogFormat = "log-format" | ||
FlagSourceKubeconfig = "source-kubeconfig" | ||
FlagSourceContext = "source-context" | ||
FlagSourceNamespace = "source-namespace" | ||
|
@@ -31,9 +38,11 @@ const ( | |
FlagRsyncImage = "rsync-image" | ||
FlagSshdImage = "sshd-image" | ||
FlagSSHKeyAlgorithm = "ssh-key-algorithm" | ||
|
||
loggerContextKey cliAppContextKey = "logger" | ||
) | ||
|
||
func New(version string, commit string) *cli.App { | ||
func New(rootLogger *log.Logger, version string, commit string) *cli.App { | ||
sshKeyAlgs := strings.Join(rsync.SSHKeyAlgorithms, ",") | ||
return &cli.App{ | ||
Name: "pv-migrate", | ||
|
@@ -46,6 +55,8 @@ func New(version string, commit string) *cli.App { | |
Aliases: []string{"m"}, | ||
ArgsUsage: "[SOURCE_PVC] [DESTINATION_PVC]", | ||
Action: func(c *cli.Context) error { | ||
logger := extractLogger(c.Context) | ||
|
||
s := migration.PVC{ | ||
KubeconfigPath: c.String(FlagSourceKubeconfig), | ||
Context: c.String(FlagSourceContext), | ||
|
@@ -78,10 +89,13 @@ func New(version string, commit string) *cli.App { | |
Strategies: strategies, | ||
RsyncImage: c.String(FlagRsyncImage), | ||
SshdImage: c.String(FlagSshdImage), | ||
Logger: logger, | ||
} | ||
|
||
logger.Info(":rocket: Starting migration") | ||
if opts.DeleteExtraneousFiles { | ||
log.Info("Extraneous files will be deleted from the destination") | ||
logger.Info(":white_exclamation_mark: " + | ||
"Extraneous files will be deleted from the destination") | ||
} | ||
|
||
return engine.New().Run(&m) | ||
|
@@ -194,11 +208,50 @@ func New(version string, commit string) *cli.App { | |
}, | ||
}, | ||
}, | ||
Flags: []cli.Flag{ | ||
cli.HelpFlag, | ||
cli.VersionFlag, | ||
&cli.StringFlag{ | ||
Name: FlagLogLevel, | ||
Aliases: []string{"l"}, | ||
Usage: fmt.Sprintf("Log level. Must be one of: %s", | ||
strings.Join(applog.LogLevels, ", ")), | ||
Value: "info", | ||
}, | ||
&cli.StringFlag{ | ||
Name: FlagLogFormat, | ||
Aliases: []string{"f"}, | ||
Usage: fmt.Sprintf("Log format. Must be one of: %s", | ||
strings.Join(applog.LogFormats, ", ")), | ||
Value: applog.LogFormatFancy, | ||
}, | ||
}, | ||
Before: func(c *cli.Context) error { | ||
l := c.String(FlagLogLevel) | ||
f := c.String(FlagLogFormat) | ||
entry, err := applog.BuildLogger(rootLogger, l, f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := c.Context | ||
c.Context = context.WithValue(ctx, loggerContextKey, entry) | ||
return nil | ||
}, | ||
Authors: []*cli.Author{ | ||
{ | ||
Name: authorName, | ||
Email: authorEmail, | ||
}, | ||
}, | ||
CommandNotFound: func(c *cli.Context, s string) { | ||
logger := extractLogger(c.Context) | ||
logger.Errorf(":cross_mark: Error: no help topic for '%s'", s) | ||
os.Exit(3) | ||
}, | ||
} | ||
} | ||
|
||
func extractLogger(c context.Context) *log.Entry { | ||
return c.Value(loggerContextKey).(*log.Entry) | ||
} |
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
Oops, something went wrong.