Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions pkg/component/compose_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,50 @@ func (c *ComposeFile) DeleteService(name string) error {
return c.deleteSectionEntry("services", name)
}

func (c *ComposeFile) DeleteVolume(name string) error {
return c.deleteSectionEntry("volumes", name)
}

func (c *ComposeFile) HasService(name string) bool {
_, ok := c.findService(name)
return ok
}

func (c *ComposeFile) HasVolume(name string) bool {
_, ok := c.findSectionEntry("volumes", name)
return ok
}

func (c *ComposeFile) ServiceBlock(name string) (string, bool) {
serviceIdx, ok := c.findService(name)
if !ok {
return "", false
}
end := findBlockEnd(c.lines, serviceIdx, 2)
return strings.Join(c.lines[serviceIdx:end], "\n"), true
return c.sectionEntryBlock("services", name)
}

func (c *ComposeFile) VolumeBlock(name string) (string, bool) {
return c.sectionEntryBlock("volumes", name)
}

func (c *ComposeFile) AddServiceBlock(name, block string) error {
return c.addSectionEntryBlock("services", name, block)
}

func (c *ComposeFile) AddVolumeBlock(name, block string) error {
return c.addSectionEntryBlock("volumes", name, block)
}

func (c *ComposeFile) addSectionEntryBlock(section, name, block string) error {
if strings.TrimSpace(block) == "" {
return fmt.Errorf("service block for %q is empty", name)
return fmt.Errorf("%s block for %q is empty", section, name)
}
if c.HasService(name) {
if _, ok := c.findSectionEntry(section, name); ok {
return nil
}

servicesIdx, ok := findMapKey(c.lines, 0, "services", 0)
sectionIdx, ok := findMapKey(c.lines, 0, section, 0)
if !ok {
c.lines = append(c.lines, "services:")
servicesIdx = len(c.lines) - 1
c.lines = append(c.lines, section+":")
sectionIdx = len(c.lines) - 1
}
insertAt := findBlockEnd(c.lines, servicesIdx, 0)
insertAt := findBlockEnd(c.lines, sectionIdx, 0)
c.lines = insertLines(c.lines, insertAt, strings.Split(strings.TrimRight(block, "\n"), "\n"))
return nil
}
Expand Down Expand Up @@ -119,10 +135,6 @@ func (c *ComposeFile) DeleteServiceKey(service, key string) error {
return nil
}

func (c *ComposeFile) DeleteVolume(name string) error {
return c.deleteSectionEntry("volumes", name)
}

func (c *ComposeFile) DeleteSectionEntry(section, key string) error {
return c.deleteSectionEntry(section, key)
}
Expand Down Expand Up @@ -201,6 +213,23 @@ func (c *ComposeFile) findService(service string) (int, bool) {
return findMapKey(c.lines, servicesIdx+1, service, 2)
}

func (c *ComposeFile) findSectionEntry(section, key string) (int, bool) {
sectionIdx, ok := findMapKey(c.lines, 0, section, 0)
if !ok {
return 0, false
}
return findMapKey(c.lines, sectionIdx+1, key, 2)
}

func (c *ComposeFile) sectionEntryBlock(section, key string) (string, bool) {
entryIdx, ok := c.findSectionEntry(section, key)
if !ok {
return "", false
}
end := findBlockEnd(c.lines, entryIdx, 2)
return strings.Join(c.lines[entryIdx:end], "\n"), true
}

type envBlockStyle int

const (
Expand Down
Loading