Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: interface #29

Merged
merged 28 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
77f5821
refactor: renamed model to rimo
mathisdrn Aug 28, 2023
92c28cc
feat: defined interface
mathisdrn Aug 28, 2023
f714bff
refactor: .gitignore
mathisdrn Aug 28, 2023
6f6bd24
feat: added FileWriter interface
mathisdrn Aug 29, 2023
bfc6fba
feat: fileWriter interface with test
mathisdrn Aug 30, 2023
c23f3ad
feat: loader for JSONL
mathisdrn Aug 30, 2023
4b19cca
test: rimo interface test
mathisdrn Aug 30, 2023
00e39ab
feat: added filesReader interface
mathisdrn Aug 30, 2023
82f1dbb
test: FilesReader with 2 files
mathisdrn Aug 30, 2023
b4c6b28
refactor: re adding to model package to avoid circular dependency
mathisdrn Aug 31, 2023
257d7ef
feat(rimo): driven_test.go
mathisdrn Aug 31, 2023
978c352
refactor(rimo): improve Writer name
mathisdrn Aug 31, 2023
816fdda
refactor: renamed testWriter() and added GetBase() method
mathisdrn Aug 31, 2023
4d9af3c
feat: TestWriter improv (similar to prev commit)
mathisdrn Aug 31, 2023
88e9b64
refactor: minor typo
mathisdrn Aug 31, 2023
5ab51c0
test: RIMO pipeline infra_test.go
mathisdrn Aug 31, 2023
adaa585
refactor: added cobra command using interface
mathisdrn Aug 31, 2023
f6e473c
refactor: more explicit variable naming
mathisdrn Aug 31, 2023
275d66e
refactor: removed unusued function
mathisdrn Aug 31, 2023
9105812
refactor: added test to compare pipeline with expected output
mathisdrn Aug 31, 2023
979b9df
refactor: fix : giving filesReader proper filepath
mathisdrn Aug 31, 2023
297b18e
refactor: almost work as expected
mathisdrn Sep 15, 2023
1480c83
refactor: updated schema from rimo pkg to model pkg
mathisdrn Sep 15, 2023
5e562b2
refactor: work as expected
mathisdrn Sep 15, 2023
d89b38a
fix: remove old analyse command
Sep 27, 2023
7a66d45
chore: remove dead code
Sep 27, 2023
6b4220f
docs: add GPLv3 license header in new files
Sep 27, 2023
a3f1e1f
fix: remove output test from git
Sep 27, 2023
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
74 changes: 64 additions & 10 deletions cmd/rimo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"os"
"path/filepath"

"github.com/cgi-fr/rimo/pkg/analyse"
"github.com/cgi-fr/rimo/pkg/io"
"github.com/cgi-fr/rimo/internal/infra"
"github.com/cgi-fr/rimo/pkg/model"
"github.com/cgi-fr/rimo/pkg/rimo"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -67,6 +67,7 @@ func main() { //nolint:funlen
},
}

// Make use of interface instead of analyse/pkg
rimoAnalyseCmd := &cobra.Command{ //nolint:exhaustruct
Use: "analyse [inputDir] [outputDir]",
Short: "Generate a rimo.yaml from a directory of .jsonl files",
Expand All @@ -75,21 +76,33 @@ func main() { //nolint:funlen
inputDir := args[0]
outputDir := args[1]

// List .jsonl files in input directory
if err := io.ValidateDirPath(inputDir); err != nil {
log.Fatal().Msgf("error validating input directory: %v", err)
}
// Reader

inputList, err := FilesList(inputDir, ".jsonl")
inputList, err := BuildFilepathList(inputDir, ".jsonl")
if err != nil {
log.Fatal().Msgf("error listing files: %v", err)
}

if len(inputList) == 0 {
log.Fatal().Msgf("no .jsonl files found in %s", inputDir)
reader, err := infra.FilesReaderFactory(inputList)
if err != nil {
log.Fatal().Msgf("error creating reader: %v", err)
}

// Writer
// (could be relocated to infra.FilesReader)
baseName, _, err := infra.ExtractName(inputList[0])
if err != nil {
log.Fatal().Msgf("error extracting base name: %v", err)
}

outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.yaml", baseName))

writer, err := infra.YAMLWriterFactory(outputPath)
if err != nil {
log.Fatal().Msgf("error creating writer: %v", err)
}

err = analyse.Orchestrator(inputList, outputDir)
err = rimo.AnalyseBase(reader, writer)
if err != nil {
log.Fatal().Msgf("error generating rimo.yaml: %v", err)
}
Expand Down Expand Up @@ -117,3 +130,44 @@ func FilesList(path string, extension string) ([]string, error) {

return files, nil
}

var ErrNoFile = fmt.Errorf("no file found")

func BuildFilepathList(path string, extension string) ([]string, error) {
err := ValidateDirPath(path)
if err != nil {
return nil, fmt.Errorf("failed to validate input directory: %w", err)
}

pattern := filepath.Join(path, "*"+extension)

files, err := filepath.Glob(pattern)
if err != nil {
return nil, fmt.Errorf("error listing files: %w", err)
}

if len(files) == 0 {
return nil, fmt.Errorf("%w : no %s files found in %s", ErrNoFile, extension, path)
}

return files, nil
}

func ValidateDirPath(path string) error {
fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
return fmt.Errorf("%w: %s", infra.ErrDirDoesNotExist, path)
} else if err != nil {
return fmt.Errorf("failed to get directory info: %w", err)
}

if !fileInfo.IsDir() {
return fmt.Errorf("%w: %s", infra.ErrPathIsNotDir, path)
}

if fileInfo.Mode().Perm()&infra.WriteDirPerm != infra.WriteDirPerm {
return fmt.Errorf("%w: %s", infra.ErrWriteDirPermission, path)
}

return nil
}
81 changes: 81 additions & 0 deletions internal/infra/fileWriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (C) 2023 CGI France
//
// This file is part of RIMO.
//
// RIMO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RIMO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RIMO. If not, see <http://www.gnu.org/licenses/>.

package infra
youen marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"

"github.com/cgi-fr/rimo/pkg/model"
"gopkg.in/yaml.v3"
)

// Terminal writter interface

type StdoutWriter struct{}

func StdoutWriterFactory() *StdoutWriter {
writer := StdoutWriter{}

return &writer
}

func (w *StdoutWriter) Export(base *model.Base) error {
fmt.Printf("%v\n", base)

return nil
}

// YAML Writter interface

type YAMLWriter struct {
outputPath string
}

func YAMLWriterFactory(filepath string) (*YAMLWriter, error) {
err := ValidateOutputPath(filepath)
if err != nil {
return nil, fmt.Errorf("failed to validate file path: %w", err)
}

writer := YAMLWriter{
outputPath: filepath,
}

return &writer, nil
}

// Write a YAML file from RIMO base at outputPath.
func (w *YAMLWriter) Export(base *model.Base) error {
outputFile, err := os.Create(w.outputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer outputFile.Close()

// Encode Base to YAML.
encoder := yaml.NewEncoder(outputFile)
defer encoder.Close()

err = encoder.Encode(base)
if err != nil {
return fmt.Errorf("failed to encode Base to YAML: %w", err)
}

return nil
}
13 changes: 8 additions & 5 deletions pkg/io/export_test.go → internal/infra/fileWriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
// You should have received a copy of the GNU General Public License
// along with RIMO. If not, see <http://www.gnu.org/licenses/>.

package io_test
package infra_test

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

"github.com/cgi-fr/rimo/pkg/io"
"github.com/cgi-fr/rimo/internal/infra"
"github.com/cgi-fr/rimo/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -32,7 +32,7 @@ const (
dataDir = "../../testdata/"
)

func TestExport(t *testing.T) {
func TestWriterYAML(t *testing.T) {
t.Parallel()

base := model.Base{
Expand All @@ -54,8 +54,11 @@ func TestExport(t *testing.T) {
// Create a temporary file for the output
outputFile := filepath.Join(tempDir, "output.yaml")

// Export the base to the output file
err = io.Export(base, outputFile)
// Create the writer
writer, err := infra.YAMLWriterFactory(outputFile)
require.NoError(t, err)

err = writer.Export(&base)
require.NoError(t, err)

// Read the output file and check its contents
Expand Down
Loading
Loading