diff --git a/backend/app/cmd/cmd.go b/backend/app/cmd/cmd.go index 534c1788..e1269582 100644 --- a/backend/app/cmd/cmd.go +++ b/backend/app/cmd/cmd.go @@ -22,7 +22,6 @@ type CommonOptionsCommander interface { SetCommon(commonOpts CommonOpts) Execute(args []string) error HandleDeprecatedFlags() []DeprecatedFlag - FindDeprecatedFlagsCollisions() []DeprecatedFlag } // CommonOpts sets externally from main, shared across all commands @@ -34,9 +33,10 @@ type CommonOpts struct { // DeprecatedFlag contains information about deprecated option type DeprecatedFlag struct { - Old string - New string - Version string + Old string + New string + Version string + Collision bool } // SetCommon satisfies CommonOptionsCommander interface and sets common option fields @@ -50,10 +50,6 @@ func (c *CommonOpts) SetCommon(commonOpts CommonOpts) { // HandleDeprecatedFlags sets new flags from deprecated and returns their list func (c *CommonOpts) HandleDeprecatedFlags() []DeprecatedFlag { return nil } -// FindDeprecatedFlagsCollisions returns list of flags collisions, e.g. when both deprecated -// and new flags are set to non-default values, and different from each other. -func (c *CommonOpts) FindDeprecatedFlagsCollisions() []DeprecatedFlag { return nil } - // fileParser used to convert template strings like blah-{{.SITE}}-{{.YYYYMMDD}} the final format type fileParser struct { site string diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 06ebcee1..454267c1 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -324,6 +324,7 @@ func (s *ServerCommand) Execute(_ []string) error { // HandleDeprecatedFlags sets new flags from deprecated returns their list. // Returned list has DeprecatedFlag.Old and DeprecatedFlag.Version set, and DeprecatedFlag.New is optional // (as some entries are removed without substitute). +// Also it returns flags found by findDeprecatedFlagsCollisions, with DeprecatedFlag.Collision flag set. func (s *ServerCommand) HandleDeprecatedFlags() (result []DeprecatedFlag) { if s.Auth.Email.Host != "" && s.SMTP.Host == "" { s.SMTP.Host = s.Auth.Email.Host @@ -379,39 +380,40 @@ func (s *ServerCommand) HandleDeprecatedFlags() (result []DeprecatedFlag) { if s.Notify.Telegram.API != "https://api.telegram.org/bot" { result = append(result, DeprecatedFlag{Old: "notify.telegram.api", Version: "1.9"}) } - return result + return append(result, s.findDeprecatedFlagsCollisions()...) } -// FindDeprecatedFlagsCollisions returns flags which are set both old (deprecated) and new way, +// findDeprecatedFlagsCollisions returns flags which are set both old (deprecated) and new way, // which means new ones are used and old ones are ignored by deprecated flag handler. -// It returns DeprecatedFlag list which always has only DeprecatedFlag.Old and DeprecatedFlag.New set. -func (s *ServerCommand) FindDeprecatedFlagsCollisions() (result []DeprecatedFlag) { +// It returns DeprecatedFlag list which always has only DeprecatedFlag.Old and DeprecatedFlag.New set, +// and DeprecatedFlag.Collision set to true. +func (s *ServerCommand) findDeprecatedFlagsCollisions() (result []DeprecatedFlag) { if stringsSetAndDifferent(s.Auth.Email.Host, s.SMTP.Host) { - result = append(result, DeprecatedFlag{Old: "auth.email.host", New: "smtp.host"}) + result = append(result, DeprecatedFlag{Old: "auth.email.host", New: "smtp.host", Collision: true}) } if s.Auth.Email.Port != 0 && s.SMTP.Port != 0 && s.Auth.Email.Port != s.SMTP.Port { - result = append(result, DeprecatedFlag{Old: "auth.email.port", New: "smtp.port"}) + result = append(result, DeprecatedFlag{Old: "auth.email.port", New: "smtp.port", Collision: true}) } if stringsSetAndDifferent(s.Auth.Email.SMTPUserName, s.SMTP.Username) { - result = append(result, DeprecatedFlag{Old: "auth.email.user", New: "smtp.username"}) + result = append(result, DeprecatedFlag{Old: "auth.email.user", New: "smtp.username", Collision: true}) } if stringsSetAndDifferent(s.Auth.Email.SMTPPassword, s.SMTP.Password) { - result = append(result, DeprecatedFlag{Old: "auth.email.passwd", New: "smtp.password"}) + result = append(result, DeprecatedFlag{Old: "auth.email.passwd", New: "smtp.password", Collision: true}) } const emailDefaultTimout = 10 * time.Second if s.Auth.Email.TimeOut != emailDefaultTimout && s.SMTP.TimeOut != emailDefaultTimout && s.Auth.Email.TimeOut != s.SMTP.TimeOut { - result = append(result, DeprecatedFlag{Old: "auth.email.timeout", New: "smtp.timeout"}) + result = append(result, DeprecatedFlag{Old: "auth.email.timeout", New: "smtp.timeout", Collision: true}) } if !(len(s.Notify.Type) == 1 && contains("none", s.Notify.Type)) && // default, "none" notify type (len(s.Notify.Users) != 0 || len(s.Notify.Admins) != 0) { // new notify param(s) are used, old ones will be ignored - result = append(result, DeprecatedFlag{Old: "notify.type", New: "notify.(users|admins)"}) + result = append(result, DeprecatedFlag{Old: "notify.type", New: "notify.(users|admins)", Collision: true}) } if stringsSetAndDifferent(s.Notify.Telegram.Token, s.Telegram.Token) { - result = append(result, DeprecatedFlag{Old: "notify.telegram.token", New: "telegram.token"}) + result = append(result, DeprecatedFlag{Old: "notify.telegram.token", New: "telegram.token", Collision: true}) } const telegramDefaultTimeout = time.Second * 5 if s.Notify.Telegram.Timeout != telegramDefaultTimeout && s.Telegram.Timeout != telegramDefaultTimeout && s.Notify.Telegram.Timeout != s.Telegram.Timeout { - result = append(result, DeprecatedFlag{Old: "notify.telegram.timeout", New: "telegram.timeout"}) + result = append(result, DeprecatedFlag{Old: "notify.telegram.timeout", New: "telegram.timeout", Collision: true}) } return result } diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index 6437263f..d17e5314 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -446,6 +446,7 @@ func TestServerApp_DeprecatedArgs(t *testing.T) { {Old: "img-proxy", New: "image-proxy.http2https", Version: "1.5"}, {Old: "notify.email.notify_admin", New: "notify.admins=email", Version: "1.9"}, {Old: "notify.type", New: "notify.(users|admins)", Version: "1.9"}, + {Old: "notify.type", New: "notify.(users|admins)", Collision: true}, {Old: "notify.telegram.token", New: "telegram.token", Version: "1.9"}, {Old: "notify.telegram.timeout", New: "telegram.timeout", Version: "1.9"}, {Old: "notify.telegram.api", Version: "1.9"}, @@ -485,17 +486,17 @@ func TestServerApp_DeprecatedArgsCollisions(t *testing.T) { } _, err := p.ParseArgs(args) require.NoError(t, err) - deprecatedFlagsCollisions := s.FindDeprecatedFlagsCollisions() + deprecatedFlagsCollisions := s.findDeprecatedFlagsCollisions() assert.ElementsMatch(t, []DeprecatedFlag{ - {Old: "notify.type", New: "notify.(users|admins)"}, - {Old: "auth.email.host", New: "smtp.host"}, - {Old: "auth.email.port", New: "smtp.port"}, - {Old: "auth.email.user", New: "smtp.username"}, - {Old: "auth.email.passwd", New: "smtp.password"}, - {Old: "auth.email.timeout", New: "smtp.timeout"}, - {Old: "notify.telegram.token", New: "telegram.token"}, - {Old: "notify.telegram.timeout", New: "telegram.timeout"}, + {Old: "notify.type", New: "notify.(users|admins)", Collision: true}, + {Old: "auth.email.host", New: "smtp.host", Collision: true}, + {Old: "auth.email.port", New: "smtp.port", Collision: true}, + {Old: "auth.email.user", New: "smtp.username", Collision: true}, + {Old: "auth.email.passwd", New: "smtp.password", Collision: true}, + {Old: "auth.email.timeout", New: "smtp.timeout", Collision: true}, + {Old: "notify.telegram.token", New: "telegram.token", Collision: true}, + {Old: "notify.telegram.timeout", New: "telegram.timeout", Collision: true}, }, deprecatedFlagsCollisions) @@ -510,7 +511,7 @@ func TestServerApp_DeprecatedArgsCollisions(t *testing.T) { } _, err = p.ParseArgs(args) require.NoError(t, err) - deprecatedFlagsCollisions = s.FindDeprecatedFlagsCollisions() + deprecatedFlagsCollisions = s.findDeprecatedFlagsCollisions() assert.Empty(t, []DeprecatedFlag{}, deprecatedFlagsCollisions) } diff --git a/backend/app/main.go b/backend/app/main.go index 77eeac9f..2726095d 100644 --- a/backend/app/main.go +++ b/backend/app/main.go @@ -45,16 +45,7 @@ func main() { SharedSecret: opts.SharedSecret, Revision: revision, }) - for _, entry := range c.FindDeprecatedFlagsCollisions() { - log.Print(fmt.Sprintf("[ERROR] deprecated --%s and new --%s options are set to different values, old one is ignored: please remove it", entry.Old, entry.New)) - } - for _, entry := range c.HandleDeprecatedFlags() { - deprecationNote := fmt.Sprintf("[WARN] --%s is deprecated since v%s and will be removed in the future", entry.Old, entry.Version) - if entry.New != "" { - deprecationNote += fmt.Sprintf(", please use --%s instead", entry.New) - } - log.Print(deprecationNote) - } + logDeprecatedParams(c.HandleDeprecatedFlags()) err := c.Execute(args) if err != nil { log.Printf("[ERROR] failed with %+v", err) @@ -79,6 +70,22 @@ func setupLog(dbg bool) { log.Setup(log.Msec, log.LevelBraces) } +// logs usual and "collision" deprecated parameters +func logDeprecatedParams(params []cmd.DeprecatedFlag) { + for _, entry := range params { + var deprecationNote string + if entry.Collision { + deprecationNote = fmt.Sprintf("[ERROR] deprecated --%s and new --%s options are set to different values, old one is ignored: please remove it", entry.Old, entry.New) + } else { + deprecationNote = fmt.Sprintf("[WARN] --%s is deprecated since v%s and will be removed in the future", entry.Old, entry.Version) + if entry.New != "" { + deprecationNote += fmt.Sprintf(", please use --%s instead", entry.New) + } + } + log.Print(deprecationNote) + } +} + // getDump reads runtime stack and returns as a string func getDump() string { maxSize := 5 * 1024 * 1024