cmd

package
v1.8.14 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2018 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "edward",
	Short: "A tool for managing local instances of microservices",
	Long: `Edward 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 {

		var dirConfig, err = home.NewConfiguration(edwardHome)
		if err != nil {
			return errors.WithStack(err)
		}

		prefix := "edward"
		if len(args) > 0 {
			prefix = fmt.Sprintf("%s %s", cmd.Use, args[0])
		}

		if redirectLogs {
			log.SetOutput(os.Stdout)
			log.SetPrefix(fmt.Sprintf("%v >", prefix))
			log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
		} else {
			log.SetOutput(&lumberjack.Logger{
				Filename:   path.Join(dirConfig.EdwardLogDir, "edward.log"),
				MaxSize:    50,
				MaxBackups: 30,
				MaxAge:     1,
			})
			log.SetPrefix(fmt.Sprintf("%s > ", prefix))
			log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
		}

		log.SetOutput(&lumberjack.Logger{
			Filename:   path.Join(dirConfig.EdwardLogDir, "edward.log"),
			MaxSize:    50,
			MaxBackups: 30,
			MaxAge:     1,
		})
		log.SetPrefix(fmt.Sprintf("%s > ", prefix))
		log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)

		log.Printf("=== Edward v%v ===\n", common.EdwardVersion)
		log.Printf("Args: %v\n", os.Args)

		defer func() {
			if r := recover(); r != nil {
				log.Println("Recovered from panic:", r)
			}
			log.Printf("=== Exiting ===\n")
		}()

		if configPath == "" {
			var err error
			tmpDirCfg := &home.EdwardConfiguration{}
			err = tmpDirCfg.Initialize()
			if err != nil {
				return errors.WithStack(err)
			}
			configPath, err = config.GetConfigPathFromWorkingDirectory(tmpDirCfg.Dir)
			if err != nil {
				return errors.WithStack(err)
			}
		}

		command := cmd.Use

		if command != "generate" {
			edwardClient, err = edward.NewClientWithConfig(configPath, common.EdwardVersion)
			if err != nil {
				return errors.WithStack(err)
			}
			err = os.Chdir(edwardClient.BasePath())
			if err != nil {
				return errors.WithStack(err)
			}
		} else {
			edwardClient, err = edward.NewClient()
			if err != nil {
				return errors.WithStack(err)
			}
		}

		edwardClient.DirConfig = dirConfig
		edwardClient.Backends, err = buildBackendOverrides()
		if err != nil {
			return errors.WithStack(err)
		}

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

		edwardClient.EdwardExecutable = os.Args[0]

		edwardClient.LogFile = logFile

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

		if command != "stop" {

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

		if command != "run" {
			checkUpdateChan = make(chan interface{})
			go checkUpdateAvailable(edwardClient.DirConfig.Dir, checkUpdateChan)
		}

		return nil
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		if checkUpdateChan != nil {
			updateAvailable, ok := (<-checkUpdateChan).(bool)
			if ok && updateAvailable {
				latestVersion := (<-checkUpdateChan).(string)
				fmt.Printf("A new version of Edward is available (%v), update with:\n\tgo get -u github.com/yext/edward\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