feat(refacto): move everything in internal package

This allows to install katenary with `go install` and to clean up the
project folder.
This commit is contained in:
2025-08-03 15:54:58 +02:00
parent d1768e5742
commit 14ca5bf0ea
91 changed files with 291 additions and 282 deletions

57
internal/parser/main.go Normal file
View File

@@ -0,0 +1,57 @@
// Package parser is a wrapper around compose-go to parse compose files.
package parser
import (
"log"
"path/filepath"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
)
func init() {
// prepend compose.katenary.yaml to the list of default override file names
cli.DefaultOverrideFileNames = append([]string{
"compose.katenary.yml",
"compose.katenary.yaml",
}, cli.DefaultOverrideFileNames...)
// add podman-compose files
cli.DefaultOverrideFileNames = append(cli.DefaultOverrideFileNames,
[]string{
"podman-compose.katenary.yml",
"podman-compose.katenary.yaml",
"podman-compose.yml",
"podman-compose.yaml",
}...)
}
// Parse compose files and return a project. The project is parsed with dotenv, osenv and profiles.
func Parse(profiles []string, envFiles []string, dockerComposeFile ...string) (*types.Project, error) {
if len(dockerComposeFile) == 0 {
cli.DefaultOverrideFileNames = append(cli.DefaultOverrideFileNames, dockerComposeFile...)
}
// resolve absolute paths of envFiles
for i := range envFiles {
var err error
envFiles[i], err = filepath.Abs(envFiles[i])
if err != nil {
log.Fatal(err)
}
}
options, err := cli.NewProjectOptions(nil,
cli.WithProfiles(profiles),
cli.WithInterpolation(true),
cli.WithDefaultConfigPath,
cli.WithEnvFiles(envFiles...),
cli.WithOsEnv,
cli.WithDotEnv,
cli.WithNormalization(true),
cli.WithResolvedPaths(false),
)
if err != nil {
return nil, err
}
return cli.ProjectFromOptions(options)
}

View File

@@ -0,0 +1,60 @@
package parser
import (
"log"
"os"
"path/filepath"
"testing"
)
const composeFile = `
services:
app:
image: nginx:latest
`
func setupTest() (string, error) {
// write the composeFile to a temporary file
tmpDir, err := os.MkdirTemp("", "katenary-test-parse")
if err != nil {
return "", err
}
writeFile := filepath.Join(tmpDir, "compose.yaml")
writeErr := os.WriteFile(writeFile, []byte(composeFile), 0644)
return writeFile, writeErr
}
func tearDownTest(tmpDir string) {
if tmpDir != "" {
if err := os.RemoveAll(tmpDir); err != nil {
log.Fatalf("Failed to remove temporary directory %s: %s", tmpDir, err.Error())
}
}
}
func TestParse(t *testing.T) {
file, err := setupTest()
dirname := filepath.Dir(file)
currentDir, _ := os.Getwd()
if err := os.Chdir(dirname); err != nil {
t.Fatalf("Failed to change directory to %s: %s", dirname, err.Error())
}
defer func() {
tearDownTest(dirname)
if err := os.Chdir(currentDir); err != nil {
t.Fatalf("Failed to change back to original directory %s: %s", currentDir, err.Error())
}
}()
if err != nil {
t.Fatalf("Failed to setup test: %s", err.Error())
}
Project, err := Parse(nil, nil)
if err != nil {
t.Fatalf("Failed to parse compose file: %s", err.Error())
}
if Project == nil {
t.Fatal("Expected project to be not nil")
}
}