Skip to content

Commit

Permalink
Fix directive adding
Browse files Browse the repository at this point in the history
  • Loading branch information
r2dtools committed Sep 22, 2024
1 parent a6eb568 commit 86445e4
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
serverBlocks := config.FindServerBlocksByServerName("example.com")
serverBlock := serverBlocks[0]
directive := nginxConfig.NewDirective("ssl_certificate", []string{"/path/to/certificate"})
serverBlock.AddDirective(directive, false)
serverBlock.AddDirective(directive, false, true)

err = config.Dump()

Expand Down
4 changes: 2 additions & 2 deletions config/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (b *Block) FindBlocks(blockName string) []Block {
return blocks
}

func (b *Block) AddDirective(directive Directive, begining bool) {
addDirective(b.rawBlock, directive, begining)
func (b *Block) AddDirective(directive Directive, begining bool, endWithNewLine bool) {
addDirective(b.rawBlock, directive, begining, endWithNewLine)
}

func (b *Block) DeleteDirective(directive Directive) {
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func TestAddConfigFile(t *testing.T) {
assert.Nil(t, err)

directive := NewDirective("directive", []string{"test"})
configFile.AddDirective(directive, true)
configFile.AddDirective(directive, true, true)

httpBlock := configFile.AddHttpBlock()
directive = NewDirective("http_directive", []string{"http", "directive"})
httpBlock.AddDirective(directive, false)
httpBlock.AddDirective(directive, false, true)

err = configFile.Dump()
assert.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (c *ConfigFile) DeleteDirectiveByName(directiveName string) {
deleteDirectiveByName(c.configFile, directiveName)
}

func (c *ConfigFile) AddDirective(directive Directive, begining bool) {
addDirective(c.configFile, directive, begining)
func (c *ConfigFile) AddDirective(directive Directive, begining bool, endWithNewLine bool) {
addDirective(c.configFile, directive, begining, endWithNewLine)
}

func (c *ConfigFile) AddHttpBlock() HttpBlock {
Expand Down
2 changes: 1 addition & 1 deletion config/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAddDirectiveInConfigFile(t *testing.T) {
testWithConfigFileRollback(t, example2ConfigFilePath, func(t *testing.T) {
configFile := getConfigFile(t, example2ConfigFileName)
directive := NewDirective("test", []string{"test_value"})
configFile.AddDirective(directive, true)
configFile.AddDirective(directive, true, true)
err := configFile.Dump()
assert.Nil(t, err)

Expand Down
13 changes: 8 additions & 5 deletions config/directivehelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,28 @@ func deleteDirectiveInEntityContainer(c entryContainer, callback func(directive
setEntries(c, dEntries)
}

func addDirective(c entryContainer, directive Directive, begining bool) {
func addDirective(c entryContainer, directive Directive, toBegining bool, endWithNewLine bool) {
entries := c.GetEntries()
directive.setContainer(c)
entry := &rawparser.Entry{
Directive: directive.rawDirective,
EndNewLines: []string{"\n"},
Directive: directive.rawDirective,
}

if endWithNewLine {
entry.EndNewLines = []string{"\n"}
}

var prevEntry *rawparser.Entry

if len(entries) > 0 && !begining {
if len(entries) > 0 && !toBegining {
prevEntry = entries[len(entries)-1]
}

if prevEntry == nil || len(prevEntry.EndNewLines) == 0 {
entry.StartNewLines = []string{"\n"}
}

if begining {
if toBegining {
entries = append([]*rawparser.Entry{entry}, entries...)
} else {
entries = append(entries, entry)
Expand Down
4 changes: 2 additions & 2 deletions config/serverblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func TestAddDirectiveInServerBlock(t *testing.T) {
testWithConfigFileRollback(t, example2ConfigFilePath, func(t *testing.T) {
config, serverBlock := getServerBlock(t, "example2.com")
directive := NewDirective("test", []string{"test_value"})
serverBlock.AddDirective(directive, true)
serverBlock.AddDirective(directive, true, true)
err := config.Dump()
assert.Nil(t, err)

config, directive = getServerBlockDirective(t, "example2.com", "test")
_, directive = getServerBlockDirective(t, "example2.com", "test")
assert.Equal(t, "test_value", directive.GetFirstValue())
})
}
Expand Down
2 changes: 1 addition & 1 deletion config/upstreamblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (b *UpstreamBlock) GetServers() []UpstreamServer {
}

func (b *UpstreamBlock) AddServer(upstreamServer UpstreamServer) {
b.AddDirective(upstreamServer.Directive, false)
b.AddDirective(upstreamServer.Directive, false, true)
}

func (b *UpstreamBlock) SetServers(upstreamServers []UpstreamServer) {
Expand Down

0 comments on commit 86445e4

Please sign in to comment.