Files
ocis/graph/pkg/command/root.go
T

107 lines
2.5 KiB
Go
Raw Normal View History

2019-12-04 17:15:11 +01:00
package command
import (
2021-03-15 15:17:29 +00:00
"context"
2019-12-04 17:15:11 +01:00
"os"
2021-11-08 13:50:52 +01:00
"github.com/owncloud/ocis/ocis-pkg/shared"
2021-03-15 15:17:29 +00:00
"github.com/thejerf/suture/v4"
2021-02-04 11:46:32 +01:00
"github.com/owncloud/ocis/graph/pkg/config"
2021-03-15 15:17:29 +00:00
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
2021-02-16 13:55:34 +01:00
"github.com/owncloud/ocis/ocis-pkg/log"
2021-10-05 12:39:31 +02:00
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/urfave/cli/v2"
2019-12-04 17:15:11 +01:00
)
// Execute is the entry point for the ocis-graph command.
2021-03-15 15:17:29 +00:00
func Execute(cfg *config.Config) error {
2019-12-04 17:15:11 +01:00
app := &cli.App{
2021-03-15 15:17:29 +00:00
Name: "ocis-graph",
2019-12-04 17:15:11 +01:00
Version: version.String,
2019-12-06 13:14:21 +01:00
Usage: "Serve Graph API for oCIS",
2019-12-04 17:15:11 +01:00
Compiled: version.Compiled(),
2020-02-10 12:06:52 +01:00
Authors: []*cli.Author{
2019-12-04 17:15:11 +01:00
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
Before: func(c *cli.Context) error {
2021-03-15 15:17:29 +00:00
cfg.Server.Version = version.String
2020-03-18 18:44:43 +01:00
return ParseConfig(c, cfg)
2019-12-04 17:15:11 +01:00
},
2020-02-10 12:06:52 +01:00
Commands: []*cli.Command{
2019-12-04 17:15:11 +01:00
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)
}
2019-12-06 13:14:21 +01:00
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return log.NewLogger(
log.Name("graph"),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
2021-03-17 15:31:51 +01:00
log.File(cfg.Log.File),
2019-12-06 13:14:21 +01:00
)
2019-12-04 17:15:11 +01:00
}
2020-03-18 18:44:43 +01:00
2021-11-05 11:15:55 +01:00
// ParseConfig loads graph configuration from known paths.
2020-03-18 18:44:43 +01:00
func ParseConfig(c *cli.Context, cfg *config.Config) error {
2021-11-04 13:50:42 +01:00
conf, err := ociscfg.BindSourcesToStructs("graph", cfg)
if err != nil {
return err
2020-03-18 18:44:43 +01:00
}
2021-11-11 13:18:53 +01:00
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
cfg.Log = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
}
conf.LoadOSEnv(config.GetEnv(cfg), false)
2021-11-08 12:04:30 +01:00
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
2020-03-18 18:44:43 +01:00
}
2021-03-15 15:17:29 +00:00
// SutureService allows for the graph command to be embedded and supervised by a suture supervisor tree.
type SutureService struct {
cfg *config.Config
}
// NewSutureService creates a new graph.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
2021-11-11 13:18:53 +01:00
cfg.Graph.Commons = cfg.Commons
2021-03-15 15:17:29 +00:00
return SutureService{
cfg: cfg.Graph,
}
}
func (s SutureService) Serve(ctx context.Context) error {
s.cfg.Context = ctx
if err := Execute(s.cfg); err != nil {
return err
}
return nil
}