- cfg, err := xdsconfig.Init(ctx.Cli, ctx.Log)
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- ctx.Config = cfg
-
- // TODO allow to redirect stdout/sterr into logs file
- //logFilename := filepath.Join(ctx.Config.FileConf.LogsDir + "xds-server.log")
-
- // FIXME - add a builder interface and support other builder type (eg. native)
- builderType := "syncthing"
-
- switch builderType {
- case "syncthing":
-
- // Start local instance of Syncthing and Syncthing-notify
- ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
-
- ctx.Log.Infof("Starting Syncthing...")
- ctx.SThgCmd, err = ctx.SThg.Start()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- ctx.Log.Infof("Syncthing started (PID %d)", ctx.SThgCmd.Process.Pid)
-
- ctx.Log.Infof("Starting Syncthing-inotify...")
- ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- ctx.Log.Infof("Syncthing-inotify started (PID %d)", ctx.SThgInotCmd.Process.Pid)
-
- // Establish connection with local Syncthing (retry if connection fail)
- time.Sleep(2 * time.Second)
- retry := 10
- err = nil
- for retry > 0 {
- if err = ctx.SThg.Connect(); err == nil {
- break
- }
- ctx.Log.Warningf("Establishing connection to Syncthing (retry %d/10)", retry)
- time.Sleep(time.Second)
- retry--
- }
- if err != nil || retry == 0 {
- return cli.NewExitError(err, 2)
- }
-
- // Retrieve Syncthing config
- id, err := ctx.SThg.IDGet()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
-
- if ctx.Config.Builder, err = xdsconfig.NewBuilderConfig(id); err != nil {
- return cli.NewExitError(err, 2)
- }
-
- // Retrieve initial Syncthing config
-
- // FIXME: cannot retrieve default SDK, need to save on disk or somewhere
- // else all config to be able to restore it.
- defaultSdk := ""
- stCfg, err := ctx.SThg.ConfigGet()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- for _, stFld := range stCfg.Folders {
- relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.ShareRootDir)
- if relativePath == "" {
- relativePath = stFld.RawPath
- }
-
- newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.Trim(relativePath, "/"), defaultSdk)
- ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld})
- }
-
- // Init model folder
- ctx.MFolder = model.NewFolder(ctx.Config, ctx.SThg)
-
- default:
- err = fmt.Errorf("Unsupported builder type")
- return cli.NewExitError(err, 3)
- }
-
- // Init cross SDKs
- ctx.SDKs, err = crosssdk.Init(ctx.Config, ctx.Log)