package command import ( "context" "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/config/envdecode" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/proxy/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) // GetCommands provides all commands for this service func GetCommands(cfg *config.Config) cli.Commands { return []*cli.Command{ // start this service Server(cfg), // interaction with this service // infos about this service Health(cfg), Version(cfg), } } // Execute is the entry point for the ocis-proxy command. func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-proxy", Usage: "proxy for oCIS", Before: func(c *cli.Context) error { cfg.Service.Version = version.String return ParseConfig(c, cfg) }, Commands: GetCommands(cfg), }) cli.HelpFlag = &cli.BoolFlag{ Name: "help,h", Usage: "Show the help", } return app.Run(os.Args) } // ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) if err != nil { return err } // 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 = &config.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 = &config.Log{} } // load all env variables relevant to the config in the current context. if err := envdecode.Decode(cfg); err != nil { // no environment variable set for this config is an expected "error" if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { return err } } return nil } // SutureService allows for the proxy command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config } // NewSutureService creates a new proxy.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { cfg.Proxy.Commons = cfg.Commons return SutureService{ cfg: cfg.Proxy, } } func (s SutureService) Serve(ctx context.Context) error { s.cfg.Context = ctx if err := Execute(s.cfg); err != nil { return err } return nil }