Skip to content

Commit

Permalink
make FindDeprecatedFlagsCollisions private method
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Jan 30, 2022
1 parent 8689b11 commit 31af19e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
12 changes: 4 additions & 8 deletions backend/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
26 changes: 14 additions & 12 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 11 additions & 10 deletions backend/app/cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}

Expand Down
27 changes: 17 additions & 10 deletions backend/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 31af19e

Please sign in to comment.