aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-10 12:19:34 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-16 14:27:28 +0200
commitdd6f08b10b1597f44e3dc25509ac9a45336b0914 (patch)
tree3dcb835306bc3adbae7c27d94fc8a51de8d6f09a /main.go
parent0262f5bef6ff67e77b844a04733c57740fba9f00 (diff)
Add folder interface and support native pathmap folder type.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'main.go')
-rw-r--r--main.go83
1 files changed, 29 insertions, 54 deletions
diff --git a/main.go b/main.go
index 060a927..65ab7a0 100644
--- a/main.go
+++ b/main.go
@@ -8,7 +8,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
- "strings"
"syscall"
"time"
@@ -48,7 +47,7 @@ type Context struct {
SThg *st.SyncThing
SThgCmd *exec.Cmd
SThgInotCmd *exec.Cmd
- MFolder *model.Folder
+ MFolders *model.Folders
SDKs *crosssdk.SDKs
WWWServer *webserver.Server
Exit chan os.Signal
@@ -99,7 +98,7 @@ func handlerSigTerm(ctx *Context) {
ctx.Log.Infof("Stoping Web server...")
ctx.WWWServer.Stop()
}
- os.Exit(1)
+ os.Exit(0)
}
// XDS Server application main routine
@@ -112,7 +111,7 @@ func xdsApp(cliCtx *cli.Context) error {
// Load config
cfg, err := xdsconfig.Init(ctx.Cli, ctx.Log)
if err != nil {
- return cli.NewExitError(err, 2)
+ return cli.NewExitError(err, -2)
}
ctx.Config = cfg
@@ -136,26 +135,24 @@ func xdsApp(cliCtx *cli.Context) error {
ctx.Log.Out = fdL
}
- // 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
+ // Create syncthing instance when section "syncthing" is present in config.json
+ if ctx.Config.FileConf.SThgConf != nil {
ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
+ }
+ // Start local instance of Syncthing and Syncthing-notify
+ if ctx.SThg != nil {
ctx.Log.Infof("Starting Syncthing...")
ctx.SThgCmd, err = ctx.SThg.Start()
if err != nil {
- return cli.NewExitError(err, 2)
+ return cli.NewExitError(err, -4)
}
fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
ctx.Log.Infof("Starting Syncthing-inotify...")
ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
if err != nil {
- return cli.NewExitError(err, 2)
+ return cli.NewExitError(err, -4)
}
fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
@@ -174,64 +171,37 @@ func xdsApp(cliCtx *cli.Context) error {
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)
+ return cli.NewExitError(err, -4)
}
- for _, stFld := range stCfg.Folders {
- relativePath := strings.TrimPrefix(stFld.RawPath, ctx.Config.FileConf.ShareRootDir)
- if relativePath == "" {
- relativePath = stFld.RawPath
- }
- newFld := xdsconfig.NewFolderConfig(stFld.ID,
- stFld.Label,
- ctx.Config.FileConf.ShareRootDir,
- strings.TrimRight(relativePath, "/"),
- defaultSdk)
- ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld})
+ // FIXME: do we still need Builder notion ? if no cleanup
+ if ctx.Config.Builder, err = xdsconfig.NewBuilderConfig(ctx.SThg.MyID); err != nil {
+ return cli.NewExitError(err, -4)
}
+ }
- // Init model folder
- ctx.MFolder = model.NewFolder(ctx.Config, ctx.SThg)
+ // Init model folder
+ ctx.MFolders = model.FoldersNew(ctx.Config, ctx.SThg)
- default:
- err = fmt.Errorf("Unsupported builder type")
- return cli.NewExitError(err, 3)
+ // Load initial folders config from disk
+ if err := ctx.MFolders.LoadConfig(); err != nil {
+ return cli.NewExitError(err, -5)
}
// Init cross SDKs
ctx.SDKs, err = crosssdk.Init(ctx.Config, ctx.Log)
if err != nil {
- return cli.NewExitError(err, 2)
+ return cli.NewExitError(err, -6)
}
// Create and start Web Server
- ctx.WWWServer = webserver.New(ctx.Config, ctx.MFolder, ctx.SDKs, ctx.Log)
+ ctx.WWWServer = webserver.New(ctx.Config, ctx.MFolders, ctx.SDKs, ctx.Log)
if err = ctx.WWWServer.Serve(); err != nil {
ctx.Log.Println(err)
- return cli.NewExitError(err, 3)
+ return cli.NewExitError(err, -7)
}
- return cli.NewExitError("Program exited ", 4)
+ return cli.NewExitError("Program exited ", -99)
}
// main
@@ -271,6 +241,11 @@ func main() {
Usage: "filename where logs will be redirected (default stdout)\n\t",
EnvVar: "LOG_FILENAME",
},
+ cli.BoolFlag{
+ Name: "no-folderconfig, nfc",
+ Usage: fmt.Sprintf("Do not read folder config file (%s)\n\t", xdsconfig.FoldersConfigFilename),
+ EnvVar: "NO_FOLDERCONFIG",
+ },
}
// only one action: Web Server