* Update command added
* Ensure that the upgraded version is really greater
* Update command should not update prerelease
* Minor presentation changes
* Fix command generation in containers from docker-compose file
- Refactored service creation
* Place the full command to the "cmd" package
* Update cobra to v1.4.0
* Updated build and release creation
* Created an install script
* Add more doc
* Add some tests...
* Add a test directive in Makefile
This commit is contained in:
2022-03-31 14:12:20 +02:00
committed by GitHub
parent d0576d4b81
commit 7b774e84d8
18 changed files with 1037 additions and 1035 deletions

View File

@@ -21,22 +21,40 @@ type Parser struct {
var Appname = ""
// NewParser create a Parser and parse the file given in filename.
func NewParser(filename string) *Parser {
// NewParser create a Parser and parse the file given in filename. If filename is empty, we try to parse the content[0] argument that should be a valid YAML content.
func NewParser(filename string, content ...string) *Parser {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
c := NewCompose()
dec := yaml.NewDecoder(f)
dec.Decode(c)
if filename != "" {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
dec := yaml.NewDecoder(f)
err = dec.Decode(c)
if err != nil {
log.Fatal(err)
}
} else {
dec := yaml.NewDecoder(strings.NewReader(content[0]))
err := dec.Decode(c)
if err != nil {
log.Fatal(err)
}
}
p := &Parser{Data: c}
return p
}
func (p *Parser) Parse(appname string) {
Appname = appname
services := make(map[string][]string)
// get the service list, to be sure that everything is ok
c := p.Data
for name, s := range c.Services {
if portlabel, ok := s.Labels[helm.LABEL_PORT]; ok {
services := strings.Split(portlabel, ",")
@@ -87,10 +105,4 @@ func NewParser(filename string) *Parser {
" for the \"" + name + "\" service \x1b[0m")
}
return p
}
func (p *Parser) Parse(appname string) {
Appname = appname
}

89
compose/parser_test.go Normal file
View File

@@ -0,0 +1,89 @@
package compose
import "testing"
const DOCKER_COMPOSE_YML1 = `
version: "3"
services:
# first service, very basic
web:
image: nginx
ports:
- "80:80"
environment:
FOO: bar
BAZ: qux
networks:
- frontend
database:
image: postgres
networks:
- frontend
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_DB: mydb
labels:
katenary.io/ports: "5432"
`
func TestParser(t *testing.T) {
p := NewParser("", DOCKER_COMPOSE_YML1)
p.Parse("test")
// check if the "web" and "database" service is parsed correctly
// by checking if the "ports" and "environment"
for name, service := range p.Data.Services {
if name == "web" {
if len(service.Ports) != 1 {
t.Errorf("Expected 1 port, got %d", len(service.Ports))
}
if service.Ports[0] != "80:80" {
t.Errorf("Expected port 80:80, got %s", service.Ports[0])
}
if len(service.Environment) != 2 {
t.Errorf("Expected 2 environment variables, got %d", len(service.Environment))
}
if service.Environment["FOO"] != "bar" {
t.Errorf("Expected FOO=bar, got %s", service.Environment["FOO"])
}
if service.Environment["BAZ"] != "qux" {
t.Errorf("Expected BAZ=qux, got %s", service.Environment["BAZ"])
}
}
// same for the "database" service
if name == "database" {
if len(service.Ports) != 1 {
t.Errorf("Expected 1 port, got %d", len(service.Ports))
}
if service.Ports[0] != "5432" {
t.Errorf("Expected port 5432, got %s", service.Ports[0])
}
if len(service.Environment) != 3 {
t.Errorf("Expected 3 environment variables, got %d", len(service.Environment))
}
if service.Environment["POSTGRES_USER"] != "postgres" {
t.Errorf("Expected POSTGRES_USER=postgres, got %s", service.Environment["POSTGRES_USER"])
}
if service.Environment["POSTGRES_PASSWORD"] != "mysecretpassword" {
t.Errorf("Expected POSTGRES_PASSWORD=mysecretpassword, got %s", service.Environment["POSTGRES_PASSWORD"])
}
if service.Environment["POSTGRES_DB"] != "mydb" {
t.Errorf("Expected POSTGRES_DB=mydb, got %s", service.Environment["POSTGRES_DB"])
}
// check labels
if len(service.Labels) != 1 {
t.Errorf("Expected 1 label, got %d", len(service.Labels))
}
// is label katenary.io/ports correct?
if service.Labels["katenary.io/ports"] != "5432" {
t.Errorf("Expected katenary.io/ports=5432, got %s", service.Labels["katenary.io/ports"])
}
}
}
}

View File

@@ -36,4 +36,5 @@ type Service struct {
EnvFiles []string `yaml:"env_file"`
RawBuild interface{} `yaml:"build"`
HealthCheck *HealthCheck `yaml:"healthcheck"`
Command []string `yaml:"command"`
}