cmd

package
v0.0.0-...-521d647 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2017 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "nedward",
	Short: "A tool for managing local instances of microservices",
	Long: `Nedward is a tool to simplify your microservices development workflow.
Build, start and manage service instances with a single command.`,
	SilenceUsage: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		if redirectLogs {
			logger = log.New(os.Stdout, fmt.Sprintf("%v >", os.Args), log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
		}
		if logFile != "" {
			prefix := "edward"
			if len(args) > 0 {
				prefix = fmt.Sprintf("%s %s", cmd.Use, args[0])
			}
			logger = log.New(&lumberjack.Logger{
				Filename:   logFile,
				MaxSize:    50,
				MaxBackups: 30,
				MaxAge:     1,
			}, fmt.Sprintf("%s > ", prefix), log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
		}

		logger.Printf("=== Nedward v%v ===\n", common.NedwardVersion)
		logger.Printf("Args: %v\n", os.Args)

		if configPath == "" {
			var err error
			configPath, err = config.GetConfigPathFromWorkingDirectory()
			if err != nil {
				return errors.WithStack(err)
			}
		}

		command := cmd.Use

		var err error
		if command != "generate" {
			nedwardClient, err = nedward.NewClientWithConfig(configPath, common.NedwardVersion, logger)
			if err != nil {
				return errors.WithStack(err)
			}
			err = os.Chdir(nedwardClient.BasePath())
			if err != nil {
				return errors.WithStack(err)
			}
		} else {
			nedwardClient, err = nedward.NewClient()
			if err != nil {
				return errors.WithStack(err)
			}
		}

		nedwardClient.ServiceChecks = func(sgs []services.ServiceOrGroup) error {
			return errors.WithStack(sudoIfNeeded(sgs))
		}
		nedwardClient.Logger = logger

		nedwardClient.NedwardExecutable = os.Args[0]

		nedwardClient.LogFile = logFile

		if redirectLogs {
			nedwardClient.Follower = output.NewNonLiveFollower()
		}

		if command != "stop" {

			for _, service := range nedwardClient.ServiceMap() {
				if _, err := os.Stat(service.GetPidPathLegacy()); !os.IsNotExist(err) {
					return errors.New("one or more services were started with an older version of Nedward. Please run `nedward stop` to stop these instances")
				}
			}
		}

		if command != "run" {
			checkUpdateChan = make(chan interface{})
			go checkUpdateAvailable(checkUpdateChan)
		}

		return nil
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		defer logger.Printf("=== Exiting ===\n")
		if checkUpdateChan != nil {
			updateAvailable, ok := (<-checkUpdateChan).(bool)
			if ok && updateAvailable {
				latestVersion := (<-checkUpdateChan).(string)
				fmt.Printf("A new version of Nedward is available (%v), update with:\n\tgo get -u github.com/nedscode/nedward\n", latestVersion)
			}
		}
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL