diff --git a/go.mod b/go.mod index 7510094..0c4bfec 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module anemon go 1.22.0 -require github.com/spf13/cobra v1.8.1 +require ( + github.com/spf13/cobra v1.8.1 + gopkg.in/yaml.v3 v3.0.1 +) require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index 912390a..a01295b 100644 --- a/go.sum +++ b/go.sum @@ -6,5 +6,7 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/adapters/input/input_test.go b/internal/adapters/input/input_test.go index 8645f13..121d71a 100644 --- a/internal/adapters/input/input_test.go +++ b/internal/adapters/input/input_test.go @@ -9,6 +9,21 @@ import ( ) var( + yamlContent = ` +info: + name: Doe + firstname: John + number: "12345" + mail: john.doe@example.com + github: johndoe + linkedin: john-doe-linkedin +variante: + optionA: + - "value1" + - "value2" + optionB: + - "value3" +` work_input = ` # Back-End Intern ## February 2024 -- August 2024 @@ -152,3 +167,44 @@ func TestSections(t *testing.T) { }) } + + +func TestGetParamsFrom(t *testing.T) { + tempDir, err := os.MkdirTemp("", "test") + if err != nil { t.Fatalf("Failed to create temp directory: %v", err) } + defer os.RemoveAll(tempDir) + + yamlFilePath := filepath.Join(tempDir, "params.yml") + err = os.WriteFile(yamlFilePath, []byte(yamlContent), 0644) + if err != nil { t.Fatalf("Failed to write YAML file: %v", err) } + + source := &YamlSource{} + params, err := source.GetParamsFrom(tempDir) + if err != nil { + t.Fatalf("GetParamsFrom returned an error: %v", err) + } + expectedParams := Params{ + Info: struct { + Name string `yaml:"name"` + FirstName string `yaml:"firstname"` + Number string `yaml:"number"` + Mail string `yaml:"mail"` + GitHub string `yaml:"github"` + LinkedIn string `yaml:"linkedin"` + }{ + Name: "Doe", + FirstName: "John", + Number: "12345", + Mail: "john.doe@example.com", + GitHub: "johndoe", + LinkedIn: "john-doe-linkedin", + }, + Variante: map[string][]string{ + "optionA": {"value1", "value2"}, + "optionB": {"value3"}, + }, + } + if !reflect.DeepEqual(params, expectedParams) { + t.Errorf("Expected %+v, but got %+v", expectedParams, params) + } +} diff --git a/internal/adapters/input/yaml.go b/internal/adapters/input/yaml.go new file mode 100644 index 0000000..477584a --- /dev/null +++ b/internal/adapters/input/yaml.go @@ -0,0 +1,33 @@ +package input + +import ( + "os" + "gopkg.in/yaml.v3" +) + +type Params struct { + Info struct { + Name string `yaml:"name"` + FirstName string `yaml:"firstname"` + Number string `yaml:"number"` + Mail string `yaml:"mail"` + GitHub string `yaml:"github"` + LinkedIn string `yaml:"linkedin"` + } `yaml:"info"` + Variante map[string][]string `yaml:"variante"` +} + +type YamlSource struct{} + +func (*YamlSource) GetParamsFrom(root string) (Params, error) { + params := Params{} + yamlFile, err := os.ReadFile(root + "/params.yml") + if err != nil { + return params, err + } + err = yaml.Unmarshal(yamlFile, ¶ms) + if err != nil { + return params, err + } + return params, nil +} diff --git a/internal/core/ports.go b/internal/core/ports.go index c077540..4d147fc 100644 --- a/internal/core/ports.go +++ b/internal/core/ports.go @@ -32,7 +32,7 @@ type Section struct { Title string Paragraphes []Paragraphe } - + type Paragraphe struct { H1 string H2 string