code: func ensureDaemon(ctx *cli.Context) error {
  	cfg, err := config.LoadConfig()
  	if err != nil {
  		return err
  	}
  
  	proc, err := findDaemon(cfg)
  	if err != nil {
  		return err
  	}
  
  	spawned := false
  
  	if proc == nil {
  		err := spawnDaemon()
  		if err != nil {
  			return err
  		}
  
  		spawned = true
  	}
  
  	client := api.NewClient(cfg)
  
  	var v *apitypes.Version
  	increment := 5 * time.Millisecond
  	for d := increment; d < 1*time.Second; d += increment {
  		v, err = client.Version.GetDaemon(context.Background())
  		if err == nil {
  			break
  		}
  		time.Sleep(d)
  	}
  
  	if err != nil {
  		return errs.NewErrorExitError("Could not communicate with daemon.", err)
  	}
  
  	if v.Version == cfg.Version {
  		return nil
  	}
  
  	if spawned {
  		return errs.NewExitError("The daemon version is incorrect. Check for stale processes.")
  	}
  
  	ui.Warn("The daemon version is out of date and is being restarted. You will need to login again.")
  
  	_, err = stopDaemon(proc)
  	if err != nil {
  		return err
  	}
  
  	return ensureDaemon(ctx)
  }
docs: // ensureDaemon ensures that the daemon is running, and is the correct version,
  // before a command is exeucted.
  // the daemon will be started/restarted once, to try and launch the latest
  // version.