Skip to content

Commit

Permalink
Add parsing of new files
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsibtihon committed Aug 4, 2024
1 parent c1f5363 commit a6eb568
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
18 changes: 15 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (c *Config) AddConfigFile(filePath string) (*ConfigFile, error) {
return nil, fmt.Errorf("file %s already exists", filePath)
}

func (c *Config) ParseFile(filePath string) error {
return c.parseRecursively(filePath)
}

func (c *Config) parse() error {
c.parsedFiles = make(map[string]*rawparser.Config)

Expand Down Expand Up @@ -231,10 +235,18 @@ func (c *Config) parseRecursively(configFilePath string) error {
}

func (c *Config) parseFilesByPath(filePath string, override bool) ([]*rawparser.Config, error) {
files, err := filepath.Glob(filePath)
var files []string

if err != nil {
return nil, err
stat, err := os.Stat(filePath)

if err == nil && stat.Mode().IsRegular() {
files = []string{filePath}
} else {
files, err = filepath.Glob(filePath)

if err != nil {
return nil, err
}
}

var trees []*rawparser.Config
Expand Down
27 changes: 27 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

var sitesEnabledPath = "../test/nginx/sites-enabled"
var nginxConfigFilePath = "../test/nginx/nginx.conf"
var example2ConfigFilePath = "../test/nginx/sites-enabled/example2.com.conf"
var exampleConfigFilePath = "../test/nginx/sites-enabled/example.com.conf"
Expand Down Expand Up @@ -99,6 +101,31 @@ func TestAddConfigFile(t *testing.T) {
assert.Len(t, httpBlocks, 1)
}

func TestParseFile(t *testing.T) {
config := parseConfig(t)
serverBlocks := config.FindServerBlocksByServerName(".example3.com")
assert.Empty(t, serverBlocks)

path := "../test/example3.com.conf"

b, err := os.ReadFile(path)
assert.Nil(t, err)

example3ConfigFilePath := filepath.Join(sitesEnabledPath, "example3.com.conf")
err = os.WriteFile(example3ConfigFilePath, b, 0644)
assert.Nil(t, err)
defer os.Remove(example3ConfigFilePath)

err = config.ParseFile("sites-enabled/example3.com.conf")
assert.Nil(t, err)

serverBlocks = config.FindServerBlocksByServerName(".example3.com")
assert.Len(t, serverBlocks, 2)

serverBlocks = config.FindServerBlocksByServerName("example2.com")
assert.Len(t, serverBlocks, 1)
}

func parseConfig(t *testing.T) *Config {
config, err := GetConfig("../test/nginx", "", false)
assert.Nilf(t, err, "could not create config: %v", err)
Expand Down
26 changes: 26 additions & 0 deletions test/example3.com.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .example3.com;

# SSL
# Some comment
ssl_certificate /opt/webmng/test/certificate/example.com.crt; # inline comment
ssl_certificate_key /opt/webmng/test/certificate/example.com.key;
ssl_trusted_certificate /opt/webmng/test/certificate/example.com.issuer.crt;
return 301 https://www.example3.com$request_uri;
}

# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name .example3.com;
include nginxconfig.io/letsencrypt.conf;

# first comment
# second comment
location / { # inline comment
return 301 https://www.example3.com$request_uri;
}
}

0 comments on commit a6eb568

Please sign in to comment.