make FindDeprecatedFlagsCollisions private method

This commit is contained in:
Dmitry Verkhoturov
2022-01-31 00:40:03 +03:00
parent 8689b11e7c
commit 31af19e456
4 changed files with 46 additions and 40 deletions
+4 -8
View File
@@ -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
+14 -12
View File
@@ -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
}
+11 -10
View File
@@ -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)
}
+17 -10
View File
@@ -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