Skip to content

Commit

Permalink
pkg/runtime/{compose,standalone}/service.go: prevent duplicate flags
Browse files Browse the repository at this point in the history
If storj-up is used to add a flag that already exists, check first
if the flag already exists, and replace the old flag if it does.

This change also fixes a bug that removed the script files if the
persist or remove flag command is used in standalone mode.

Change-Id: I419882c2bce8fbbaf2ca1dc5dc7edec432e88429
dlamarmorgan committed Sep 8, 2023
1 parent fb35284 commit 1c8d6d0
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pkg/runtime/compose/service.go
Original file line number Diff line number Diff line change
@@ -230,6 +230,15 @@ func (s *Service) AddFlag(flag string) error {
if err != nil {
return err
}
eqIndex := strings.Index(rendered, "=")
if eqIndex >= 0 {
commandIndex := slices.IndexFunc(s.project.Services[ix].Command, func(command string) bool {
return strings.HasPrefix(command, rendered[:eqIndex+1])
})
if commandIndex >= 0 {
s.project.Services[ix].Command = slices.Delete(s.project.Services[ix].Command, commandIndex, commandIndex+1)
}
}
s.project.Services[ix].Command = append(s.project.Services[ix].Command, rendered)
}
}
17 changes: 14 additions & 3 deletions pkg/runtime/standalone/service.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import (
"regexp"
"strings"

"github.com/zeebo/errs"
"golang.org/x/exp/slices"

"storj.io/storj-up/pkg/runtime/runtime"
)
@@ -50,11 +50,13 @@ func (s *service) Labels() []string {
}

func (s *service) RemoveFlag(flag string) error {
return errs.New("RemoveFlag for Standalone is not yet implemented")
fmt.Println("RemoveFlag for Standalone is not yet implemented!")
return nil
}

func (s *service) Persist(dir string) error {
return errs.New("Standalone runner doesn't use containers. No need to persist directories.")
// NOOP: Standalone runner doesn't use containers. No need to persist directories
return nil
}

func (s *service) ChangeImage(func(string) string) error {
@@ -105,6 +107,15 @@ func (s *service) AddFlag(flag string) error {
if err != nil {
return err
}
eqIndex := strings.Index(f, "=")
if eqIndex >= 0 {
commandIndex := slices.IndexFunc(s.Command, func(command string) bool {
return strings.HasPrefix(command, f[:eqIndex+1])
})
if commandIndex >= 0 {
s.Command = slices.Delete(s.Command, commandIndex, commandIndex+1)
}
}
s.Command = append(s.Command, f)
return nil
}

0 comments on commit 1c8d6d0

Please sign in to comment.