fix program flags

This commit is contained in:
David Christofas
2020-04-21 10:58:27 +02:00
parent 3d26974593
commit 0b757c8713
9 changed files with 133 additions and 51 deletions
+45 -40
View File
@@ -5,10 +5,10 @@ import (
"strings"
"github.com/micro/cli/v2"
"github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-thumbnails/pkg/config"
"github.com/owncloud/ocis-thumbnails/pkg/flagset"
"github.com/owncloud/ocis-thumbnails/pkg/version"
"github.com/owncloud/ocis-pkg/v2/log"
"github.com/spf13/viper"
)
@@ -32,45 +32,7 @@ func Execute() error {
Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("THUMBNAILS")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("thumbnails")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}
return nil
return ParseConfig(c, cfg)
},
Commands: []*cli.Command{
@@ -101,3 +63,46 @@ func NewLogger(cfg *config.Config) log.Logger {
log.Color(cfg.Log.Color),
)
}
// ParseConfig loads configuration from Viper known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("THUMBNAILS")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("thumbnails")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}
return nil
}
+3 -1
View File
@@ -30,7 +30,8 @@ func Server(cfg *config.Config) *cli.Command {
Flags: flagset.ServerWithConfig(cfg),
Before: func(c *cli.Context) error {
cfg.Thumbnail.Resolutions = c.StringSlice("thumbnail-resolution")
return nil
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
@@ -134,6 +135,7 @@ func Server(cfg *config.Config) *cli.Command {
grpc.Namespace(cfg.Server.Namespace),
grpc.Address(cfg.Server.Address),
grpc.Metrics(metrics),
grpc.Flags(flagset.RootWithConfig(config.New())),
)
gr.Add(func() error {
+2
View File
@@ -27,12 +27,14 @@ func RootWithConfig(cfg *config.Config) []cli.Flag {
},
&cli.BoolFlag{
Name: "log-pretty",
Value: true,
Usage: "Enable pretty logging",
EnvVars: []string{"THUMBNAILS_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
&cli.BoolFlag{
Name: "log-color",
Value: true,
Usage: "Enable colored logging",
EnvVars: []string{"THUMBNAILS_LOG_COLOR"},
Destination: &cfg.Log.Color,
+9
View File
@@ -3,6 +3,7 @@ package grpc
import (
"context"
"github.com/micro/cli/v2"
"github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-thumbnails/pkg/config"
"github.com/owncloud/ocis-thumbnails/pkg/metrics"
@@ -20,6 +21,7 @@ type Options struct {
Config *config.Config
Metrics *metrics.Metrics
Namespace string
Flags []cli.Flag
}
// newOptions initializes the available default options.
@@ -81,3 +83,10 @@ func Namespace(val string) Option {
o.Namespace = val
}
}
// Flags provides a function to set the flags option.
func Flags(flags []cli.Flag) Option {
return func(o *Options) {
o.Flags = append(o.Flags, flags...)
}
}
+1
View File
@@ -18,6 +18,7 @@ func NewService(opts ...Option) grpc.Service {
grpc.Version(version.String),
grpc.Address(options.Address),
grpc.Context(options.Context),
grpc.Flags(options.Flags...),
)
var thumbnail proto.ThumbnailServiceHandler
+7 -3
View File
@@ -12,6 +12,12 @@ import (
"github.com/owncloud/ocis-thumbnails/pkg/thumbnail/storage"
)
type contextKey string
const (
authorization contextKey = imgsource.WebDavAuth
)
// NewService returns a service implementation for Service.
func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
options := newOptions(opts...)
@@ -48,7 +54,6 @@ type Thumbnail struct {
func (g Thumbnail) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rsp *v0proto.GetResponse) error {
encoder := thumbnail.EncoderForType(req.Filetype.String())
if encoder == nil {
// TODO: better error responses
return fmt.Errorf("can't be encoded. filetype %s not supported", req.Filetype.String())
}
r := g.resolutions.ClosestMatch(int(req.Width), int(req.Height))
@@ -67,8 +72,7 @@ func (g Thumbnail) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rs
}
auth := req.Authorization
sCtx := context.WithValue(ctx, imgsource.WebDavAuth, auth)
// TODO: clean up error handling
sCtx := context.WithValue(ctx, authorization, auth)
img, err := g.source.Get(sCtx, tr.ImagePath)
if err != nil {
return err