2020-04-01 02:08:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-04-27 22:26:15 +00:00
|
|
|
"github.com/baez90/inetmock/internal/plugins"
|
2020-04-01 02:08:21 +00:00
|
|
|
"github.com/baez90/inetmock/pkg/api"
|
2020-04-27 22:26:15 +00:00
|
|
|
"github.com/baez90/inetmock/pkg/config"
|
2020-04-13 22:14:56 +00:00
|
|
|
"github.com/baez90/inetmock/pkg/logging"
|
2020-04-27 22:26:15 +00:00
|
|
|
"github.com/baez90/inetmock/pkg/path"
|
2020-04-01 02:08:21 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"go.uber.org/zap"
|
2020-04-27 22:26:15 +00:00
|
|
|
"time"
|
2020-04-01 02:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-13 22:14:56 +00:00
|
|
|
logger logging.Logger
|
2020-04-27 22:26:15 +00:00
|
|
|
rootCmd *cobra.Command
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
pluginsDirectory string
|
|
|
|
configFilePath string
|
|
|
|
logLevel string
|
|
|
|
developmentLogs bool
|
2020-04-01 02:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-04-27 22:26:15 +00:00
|
|
|
cobra.OnInitialize(onInit)
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "",
|
|
|
|
Short: "INetMock is lightweight internet mock",
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&pluginsDirectory, "plugins-directory", "", "Directory where plugins should be loaded from")
|
2020-04-01 02:08:21 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configFilePath, "config", "", "Path to config file that should be used")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "logging level to use")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&developmentLogs, "development-logs", false, "Enable development mode logs")
|
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
rootCmd.AddCommand(
|
|
|
|
serveCmd,
|
|
|
|
generateCaCmd,
|
|
|
|
pluginsCmd,
|
|
|
|
)
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
func onInit() {
|
|
|
|
logging.ConfigureLogging(
|
|
|
|
logging.ParseLevel(logLevel),
|
|
|
|
developmentLogs,
|
|
|
|
map[string]interface{}{"cwd": path.WorkingDirectory()},
|
|
|
|
)
|
|
|
|
|
|
|
|
logger, _ = logging.CreateLogger()
|
|
|
|
config.CreateConfig(rootCmd.Flags())
|
|
|
|
appConfig := config.Instance()
|
|
|
|
|
|
|
|
if err := appConfig.ReadConfig(configFilePath); err != nil {
|
|
|
|
logger.Error(
|
|
|
|
"failed to read config file",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
if err := api.InitServices(appConfig, logger); err != nil {
|
|
|
|
logger.Error(
|
|
|
|
"failed to initialize app services",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
2020-04-13 22:14:56 +00:00
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
registry := plugins.Registry()
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
cfg := config.Instance()
|
2020-04-01 02:08:21 +00:00
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
pluginLoadStartTime := time.Now()
|
|
|
|
if err := registry.LoadPlugins(cfg.PluginsDir()); err != nil {
|
|
|
|
logger.Error("Failed to load plugins",
|
|
|
|
zap.String("pluginsDirectory", cfg.PluginsDir()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
pluginLoadDuration := time.Since(pluginLoadStartTime)
|
2020-04-01 02:08:21 +00:00
|
|
|
logger.Info(
|
2020-04-27 22:26:15 +00:00
|
|
|
"loading plugins completed",
|
|
|
|
zap.Duration("pluginLoadDuration", pluginLoadDuration),
|
2020-04-01 02:08:21 +00:00
|
|
|
)
|
|
|
|
|
2020-04-27 22:26:15 +00:00
|
|
|
pluginsCmd.AddCommand(registry.PluginCommands()...)
|
|
|
|
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExecuteRootCommand() error {
|
|
|
|
return rootCmd.Execute()
|
|
|
|
}
|