Skip to content

Commit

Permalink
session 1 example 8
Browse files Browse the repository at this point in the history
  • Loading branch information
marianina8 committed Apr 9, 2023
1 parent 9706085 commit c44cffc
Show file tree
Hide file tree
Showing 26 changed files with 10,142 additions and 0 deletions.
27 changes: 27 additions & 0 deletions session1/examples/example8/cmd/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/marianina8/example8/utils"
)

func main() {
filename := flag.String("file", "", "Input file name")
flag.Parse()

if *filename == "" {
log.Fatal("Error: no input file specified")
}

yamlData, err := utils.Transform(*filename)
if err != nil {
log.Fatalf("Error: unable to transform the file; unexpected format: %v", err)
os.Exit(1)
}

fmt.Println(string(yamlData))
}
13 changes: 13 additions & 0 deletions session1/examples/example8/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "Building Modern CLI Applications with Go",
"instructors": [
{
"name": "Marian Montagnino",
"email": "mmontagnino@netflix.com"
},
{
"name": "Romeric Philogene",
"email": "rphiologene@qovery.com"
}
]
}
10 changes: 10 additions & 0 deletions session1/examples/example8/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2: Sed in leo vestibulum, viverra libero eu, facilisis velit.
3: Fusce faucibus nibh sed turpis sagittis bibendum.
4: Integer luctus leo vel libero molestie, vitae consequat turpis venenatis.
5: Quisque et tortor rutrum, lobortis arcu in, congue metus.
6: Praesent dapibus sapien id nibh suscipit, at commodo quam suscipit.
7: Nullam ullamcorper lorem eu sapien interdum, non posuere ipsum euismod.
8: Pellentesque vel mauris nec turpis ultrices auctor.
9: Curabitur gravida justo vel lacinia tempus.
10: Donec commodo sapien vitae justo finibus sagittis.
5 changes: 5 additions & 0 deletions session1/examples/example8/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/marianina8/example8

go 1.19

require gopkg.in/yaml.v2 v2.4.0 // indirect
3 changes: 3 additions & 0 deletions session1/examples/example8/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
11 changes: 11 additions & 0 deletions session1/examples/example8/models/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type Instructor struct {
Name string `json:"name"`
Email string `json:"email"`
}

type Workshop struct {
Title string `json:"title"`
Instructors []Instructor `json:"instructors"`
}
36 changes: 36 additions & 0 deletions session1/examples/example8/utils/transform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/marianina8/example8/models"
"gopkg.in/yaml.v2"
)

func Transform(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

bytes, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}

var jsonData models.Workshop
err = json.Unmarshal(bytes, &jsonData)
if err != nil {
return nil, err
}

yamlData, err := yaml.Marshal(&jsonData)
if err != nil {
return nil, err
}

return yamlData, nil
}
17 changes: 17 additions & 0 deletions session1/examples/example8/vendor/gopkg.in/yaml.v2/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions session1/examples/example8/vendor/gopkg.in/yaml.v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions session1/examples/example8/vendor/gopkg.in/yaml.v2/LICENSE.libyaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions session1/examples/example8/vendor/gopkg.in/yaml.v2/NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c44cffc

Please # to comment.