Skip to content

Commit

Permalink
PR for v0.1.0 (#1)
Browse files Browse the repository at this point in the history
* add README

* created the directory and file structure.

* modiry diretory structure

* initial parse code and test

* implementing parser

* implementing delete column at parser

* implementing ocnverter

* implementing writer (without test)

* implementing writer

* implementing main.go

* split main.go into ddc.go

* add gitignore

* add valid.md

* went through TODO in ddc_test.go

* modiry README.md
  • Loading branch information
zawazawa0316 authored Jan 3, 2024
1 parent ea0ec9d commit 5095149
Show file tree
Hide file tree
Showing 18 changed files with 748 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore ddc binary
ddc
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
# designDocConverter
CLI for migrating Excel design documents to markdown
# DesignDocConverter (ddc)

## Overview
DesignDocConverter (ddc) is a CLI tool that converts Excel files into Markdown format. It can process a specific xlsx file or all xlsx files within a directory.

## Usage
After building the tool, execute `ddc` using the following command:

```sh
./ddc <path_to_excel_file_or_directory>
```

- `<path_to_excel_file_or_directory>` should be the path to the Excel file you want to convert or a directory containing Excel files.
- The output is generated in the same directory as the input file.
- Sheets are converted to Markdown headings, and cell contents are formatted in a table structure.

### Output Sample
For a simple Excel file with multiple sheets, the Markdown output will look like this:

```
## Sheet1
| A1 Content | B1 Content |
| A2 Content | B2 Content |
## Sheet2
| A1 Content |
| A2 Content |
```

This represents two sheets with their respective cell contents arranged in table format, without header rows.

## Build Instructions
Ensure your environment is set up with Go, then build the project using the following command:

```sh
go build -o ddc github.com/zawazawa0316/designDocConverter/cmd/ddc
```

This will create the `ddc` binary in your project's root directory.

## Running Tests
To run the project tests, use the following command:

```sh
go test github.com/zawazawa0316/designDocConverter/cmd/ddc
```

This command runs tests to verify the accuracy of the conversion process.

---
71 changes: 71 additions & 0 deletions cmd/ddc/ddc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ddc

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/zawazawa0316/designDocConverter/internal/converter"
"github.com/zawazawa0316/designDocConverter/internal/parser"
"github.com/zawazawa0316/designDocConverter/internal/writer"
)

type CLI struct {
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
}

func (cli *CLI) Run(args []string) error {
if len(args) < 2 {
fmt.Fprintln(cli.Stderr, "Usage: designDocConverter <path_to_excel_file_or_directory>")
return fmt.Errorf("insufficient arguments")
}
inputPath := args[1]

fileInfo, err := os.Stat(inputPath)
if err != nil {
fmt.Fprintf(cli.Stderr, "Error accessing input path: %v\n", err)
return err
}

if fileInfo.IsDir() {
return filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(cli.Stderr, "Error accessing path %s: %v\n", path, err)
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".xlsx") {
return processFile(cli, path)
}
return nil
})
} else {
return processFile(cli, inputPath)
}
}

func processFile(cli *CLI, filePath string) error {
excelData, err := parser.ParseExcelFile(filePath)
if err != nil {
fmt.Fprintf(cli.Stderr, "Failed to parse Excel file %s: %v\n", filePath, err)
return err
}

markdownData, err := converter.ConvertToMarkdown(excelData)
if err != nil {
fmt.Fprintf(cli.Stderr, "Failed to convert Excel file to Markdown %s: %v\n", filePath, err)
return err
}

err = writer.WriteMarkdown(filePath, markdownData)
if err != nil {
fmt.Fprintf(cli.Stderr, "Failed to write Markdown file for %s: %v\n", filePath, err)
return err
}

fmt.Fprintf(cli.Stdout, "Markdown file created successfully for %s\n", filePath)
return nil
}
165 changes: 165 additions & 0 deletions cmd/ddc/ddc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package ddc

import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)

// copyFile copies a file from the source path to the destination path.
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}

func TestCLIRunWithInsufficientArgs(t *testing.T) {
cli := CLI{
Stdout: new(bytes.Buffer),
Stderr: new(bytes.Buffer),
}

args := []string{"ddc"}
err := cli.Run(args)

if err == nil {
t.Errorf("Expected an error for insufficient arguments, but got none")
}
}

// TestCLIRunWithSingleFile tests the CLI's ability to process a single Excel file.
func TestCLIRunWithSingleFile(t *testing.T) {
// Create a temporary directory
tempDir, err := ioutil.TempDir("", "ddc_test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// Copy testdata.xlsx to the temporary directory
testFilePath := filepath.Join(tempDir, "testdata.xlsx")
originalTestFilePath := "../../testdata/valid.xlsx"
if err := copyFile(originalTestFilePath, testFilePath); err != nil {
t.Fatalf("Failed to copy testdata.xlsx to temp directory: %v", err)
}

cli := CLI{
Stdout: new(bytes.Buffer),
Stderr: new(bytes.Buffer),
}

args := []string{"ddc", testFilePath}
err = cli.Run(args)

if err != nil {
t.Errorf("Failed to process a valid file: %v", err)
}

// Read the expected output from valid.md
expectedOutput, err := ioutil.ReadFile("../../testdata/valid.md")
if err != nil {
t.Fatalf("Failed to read the expected output file: %v", err)
}

// Read the actual output generated by the CLI
outputPath := filepath.Join(tempDir, "testdata.md") // Adjust the output file name as per CLI implementation
actualOutput, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read the actual output file: %v", err)
}

// Compare the actual output with the expected output and display both if they don't match
if !reflect.DeepEqual(actualOutput, expectedOutput) {
t.Errorf("The actual output does not match the expected output\nActual:\n%s\nExpected:\n%s",
string(actualOutput), string(expectedOutput))
}

}

// TestCLIRunWithDirectory tests the CLI's ability to process all Excel files in a directory.
func TestCLIRunWithDirectory(t *testing.T) {
// Create a temporary directory
tempDir, err := ioutil.TempDir("", "ddc_test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// Copy testdata.xlsx to the temporary directory as testdata1.xlsx and testdata2.xlsx
originalTestFilePath := "../../testdata/valid.xlsx"
testFilePath1 := filepath.Join(tempDir, "testdata1.xlsx")
testFilePath2 := filepath.Join(tempDir, "testdata2.xlsx")
if err := copyFile(originalTestFilePath, testFilePath1); err != nil {
t.Fatalf("Failed to copy testdata.xlsx to temp directory as testdata1.xlsx: %v", err)
}
if err := copyFile(originalTestFilePath, testFilePath2); err != nil {
t.Fatalf("Failed to copy testdata.xlsx to temp directory as testdata2.xlsx: %v", err)
}

cli := CLI{
Stdout: new(bytes.Buffer),
Stderr: new(bytes.Buffer),
}

args := []string{"ddc", tempDir}
err = cli.Run(args)

if err != nil {
t.Errorf("Failed to process files in a valid directory: %v", err)
}

// Read the expected output from valid.md
expectedOutput, err := ioutil.ReadFile("../../testdata/valid.md")
if err != nil {
t.Fatalf("Failed to read the expected output file: %v", err)
}

// Define a helper function to verify the output
verifyOutput := func(testFileName string, expectedOutput []byte) {
outputPath := filepath.Join(tempDir, testFileName) // Adjust as per CLI implementation
actualOutput, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read the actual output file for %s: %v", testFileName, err)
}
if !reflect.DeepEqual(actualOutput, expectedOutput) {
t.Errorf("The actual output for %s does not match the expected output\nActual:\n%s\nExpected:\n%s",
testFileName, string(actualOutput), string(expectedOutput))
}
}

// Verify the output for testdata1.xlsx and testdata2.xlsx
verifyOutput("testdata1.md", expectedOutput)
verifyOutput("testdata2.md", expectedOutput)
}

func TestCLIRunWithInvalidPath(t *testing.T) {
cli := CLI{
Stdout: new(bytes.Buffer),
Stderr: new(bytes.Buffer),
}

args := []string{"ddc", "/invalid/path"}
err := cli.Run(args)

if err == nil {
t.Errorf("Expected an error for an invalid path, but got none")
}
}
Empty file added config/config.yml
Empty file.
Empty file added config/template.xlsx
Empty file.
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/zawazawa0316/designDocConverter

go 1.21.4

require github.com/xuri/excelize/v2 v2.8.0

require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca // indirect
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/text v0.12.0 // indirect
)
71 changes: 71 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM=
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg=
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.8.0 h1:Vd4Qy809fupgp1v7X+nCS/MioeQmYVVzi495UCTqB7U=
github.com/xuri/excelize/v2 v2.8.0/go.mod h1:6iA2edBTKxKbZAa7X5bDhcCg51xdOn1Ar5sfoXRGrQg=
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a h1:Mw2VNrNNNjDtw68VsEj2+st+oCSn4Uz7vZw6TbhcV1o=
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 5095149

Please sign in to comment.