2020-09-18 12:56:12 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-04 14:37:37 +01:00
|
|
|
"context"
|
2020-09-18 12:56:12 +02:00
|
|
|
"os"
|
2021-03-10 12:26:14 +01:00
|
|
|
|
2021-03-09 16:03:47 +00:00
|
|
|
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
|
2020-09-18 15:26:25 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/log"
|
2021-10-05 12:39:31 +02:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/version"
|
2020-09-18 12:59:04 +02:00
|
|
|
"github.com/owncloud/ocis/webdav/pkg/config"
|
2021-03-11 15:50:01 +01:00
|
|
|
"github.com/thejerf/suture/v4"
|
2021-09-14 16:05:03 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-18 12:56:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Execute is the entry point for the ocis-webdav command.
|
2021-03-04 14:37:37 +01:00
|
|
|
func Execute(cfg *config.Config) error {
|
2020-09-18 12:56:12 +02:00
|
|
|
app := &cli.App{
|
2020-09-30 22:57:03 +02:00
|
|
|
Name: "webdav",
|
2020-09-18 12:56:12 +02:00
|
|
|
Version: version.String,
|
|
|
|
|
Usage: "Serve WebDAV API for oCIS",
|
|
|
|
|
Compiled: version.Compiled(),
|
|
|
|
|
|
|
|
|
|
Authors: []*cli.Author{
|
|
|
|
|
{
|
|
|
|
|
Name: "ownCloud GmbH",
|
|
|
|
|
Email: "support@owncloud.com",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Before: func(c *cli.Context) error {
|
2020-09-25 18:04:38 +02:00
|
|
|
cfg.Service.Version = version.String
|
2020-11-04 15:42:12 +01:00
|
|
|
return nil
|
2020-09-18 12:56:12 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
|
Server(cfg),
|
|
|
|
|
Health(cfg),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cli.HelpFlag = &cli.BoolFlag{
|
|
|
|
|
Name: "help,h",
|
|
|
|
|
Usage: "Show the help",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cli.VersionFlag = &cli.BoolFlag{
|
|
|
|
|
Name: "version,v",
|
|
|
|
|
Usage: "Print the version",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return app.Run(os.Args)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewLogger initializes a service-specific logger instance.
|
|
|
|
|
func NewLogger(cfg *config.Config) log.Logger {
|
|
|
|
|
return log.NewLogger(
|
|
|
|
|
log.Name("webdav"),
|
|
|
|
|
log.Level(cfg.Log.Level),
|
|
|
|
|
log.Pretty(cfg.Log.Pretty),
|
|
|
|
|
log.Color(cfg.Log.Color),
|
2021-03-17 15:40:23 +01:00
|
|
|
log.File(cfg.Log.File),
|
2020-09-18 12:56:12 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 13:45:15 +01:00
|
|
|
// ParseConfig loads graph configuration from known paths.
|
2020-09-18 12:56:12 +02:00
|
|
|
func ParseConfig(c *cli.Context, cfg *config.Config) error {
|
2021-11-05 13:45:15 +01:00
|
|
|
conf, err := ociscfg.BindSourcesToStructs("webdav", cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2020-09-18 12:56:12 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-05 13:45:15 +01:00
|
|
|
// load all env variables relevant to the config in the current context.
|
|
|
|
|
conf.LoadOSEnv(config.GetEnv(), false)
|
2020-09-18 12:56:12 +02:00
|
|
|
|
2021-11-05 13:45:15 +01:00
|
|
|
if err = cfg.UnmapEnv(conf); err != nil {
|
|
|
|
|
return err
|
2020-09-18 12:56:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-03-04 14:37:37 +01:00
|
|
|
|
|
|
|
|
// SutureService allows for the webdav command to be embedded and supervised by a suture supervisor tree.
|
|
|
|
|
type SutureService struct {
|
2021-03-11 15:50:01 +01:00
|
|
|
cfg *config.Config
|
2021-03-04 14:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSutureService creates a new webdav.SutureService
|
2021-03-11 15:50:01 +01:00
|
|
|
func NewSutureService(cfg *ociscfg.Config) suture.Service {
|
2021-10-31 00:08:46 +02:00
|
|
|
inheritLogging(cfg)
|
2021-03-10 11:10:46 +01:00
|
|
|
if cfg.Mode == 0 {
|
|
|
|
|
cfg.WebDAV.Supervised = true
|
|
|
|
|
}
|
2021-03-17 15:40:23 +01:00
|
|
|
cfg.WebDAV.Log.File = cfg.Log.File
|
2021-03-04 14:37:37 +01:00
|
|
|
return SutureService{
|
2021-03-11 15:50:01 +01:00
|
|
|
cfg: cfg.WebDAV,
|
2021-03-04 14:37:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 15:50:01 +01:00
|
|
|
func (s SutureService) Serve(ctx context.Context) error {
|
|
|
|
|
s.cfg.Context = ctx
|
2021-03-04 14:37:37 +01:00
|
|
|
if err := Execute(s.cfg); err != nil {
|
2021-03-11 15:50:01 +01:00
|
|
|
return err
|
2021-03-04 14:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 15:50:01 +01:00
|
|
|
return nil
|
2021-03-04 14:37:37 +01:00
|
|
|
}
|
2021-10-31 00:08:46 +02:00
|
|
|
|
|
|
|
|
// inheritLogging is a poor man's global logging state tip-toeing around circular dependencies. It sets the logging
|
|
|
|
|
// of the service to whatever is in the higher config (in this case coming from ocis.yaml) and sets them as defaults,
|
|
|
|
|
// being overwritten when the extension parses its config file / env variables.
|
|
|
|
|
func inheritLogging(cfg *ociscfg.Config) {
|
|
|
|
|
cfg.WebDAV.Log.File = cfg.Log.File
|
|
|
|
|
cfg.WebDAV.Log.Color = cfg.Log.Color
|
|
|
|
|
cfg.WebDAV.Log.Pretty = cfg.Log.Pretty
|
|
|
|
|
cfg.WebDAV.Log.Level = cfg.Log.Level
|
|
|
|
|
}
|