Skip to content

Commit 2f72290

Browse files
committed
init
0 parents  commit 2f72290

9 files changed

Lines changed: 461 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Build and Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*" # Trigger the workflow on tags starting with "v"
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
goos: [linux, darwin, windows]
15+
goarch: [386, amd64, arm, arm64]
16+
exclude:
17+
# Exclude certain combinations
18+
- goos: darwin
19+
goarch: 386
20+
- goos: darwin
21+
goarch: arm
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Set up Go
28+
uses: actions/setup-go@v5
29+
with:
30+
go-version: "1.22" # Adjust the Go version if necessary
31+
32+
- name: Build binary
33+
run: |
34+
mkdir -p bin
35+
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o bin/csv2json_${{ matrix.goos }}_${{ matrix.goarch }}$([[ ${{ matrix.goos }} == 'windows' ]] && echo .exe || echo '') ./cmd/csv2json/main.go
36+
37+
- name: List files for debug
38+
run: ls -la bin
39+
40+
- name: Move binary to release directory
41+
run: |
42+
mkdir -p release
43+
ls -la release
44+
mv bin/csv2json_${{ matrix.goos }}_${{ matrix.goarch }}* release/
45+
ls -la release
46+
47+
- name: List release directory for debug
48+
run: ls -la release
49+
50+
- name: Upload Release Assets
51+
uses: actions/upload-artifact@v4
52+
with:
53+
name: release-assets-${{ matrix.goos }}-${{ matrix.goarch }}
54+
path: release/
55+
56+
release:
57+
runs-on: ubuntu-latest
58+
needs: build
59+
60+
steps:
61+
- name: Download Release Assets
62+
uses: actions/download-artifact@v4
63+
with:
64+
path: release-assets
65+
pattern: release-assets-*
66+
merge-multiple: true
67+
68+
- name: Create directories for compression
69+
run: mkdir -p release-compressed
70+
71+
- name: Compress Release Assets
72+
run: |
73+
version=${GITHUB_REF#refs/tags/v}
74+
75+
# Set up name mappings
76+
declare -A goos_map=( ["linux"]="linux" ["darwin"]="macos" ["windows"]="windows" )
77+
declare -A goarch_map=( ["386"]="x86" ["amd64"]="x64" ["arm"]="arm" ["arm64"]="arm64" )
78+
79+
# Compress binaries
80+
for file in release-assets/*; do
81+
base_name=$(basename "$file")
82+
echo "Compressing $base_name"
83+
84+
# Extract goos and goarch
85+
parts=(${base_name//_/ })
86+
goos=${parts[1]}
87+
goarch_with_ext=${parts[2]}
88+
goarch=${goarch_with_ext%%.*} # Remove the file extension
89+
90+
friendly_goos=${goos_map[$goos]}
91+
friendly_goarch=${goarch_map[$goarch]}
92+
93+
# Rename the file to csv2json or csv2json.exe
94+
if [[ $goos == "windows" ]]; then
95+
mv "$file" release-assets/csv2json.exe
96+
file="release-assets/csv2json.exe"
97+
else
98+
mv "$file" release-assets/csv2json
99+
file="release-assets/csv2json"
100+
fi
101+
102+
# Use .zip for macOS and Windows, .tar.gz for Linux
103+
if [[ $goos == "windows" || $goos == "darwin" ]]; then
104+
zip_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.zip"
105+
zip release-compressed/${zip_filename} -j "$file"
106+
else
107+
tar_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.tar.gz"
108+
tar -czvf release-compressed/${tar_filename} -C release-assets csv2json
109+
fi
110+
done
111+
112+
- name: Create GitHub Release
113+
uses: softprops/action-gh-release@v2
114+
with:
115+
files: release-compressed/*
116+
env:
117+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.csv

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 TechMDW
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

cmd/csv2json/main.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"time"
10+
11+
csv2json "github.com/TechMDW/csv2json/pkg"
12+
)
13+
14+
func main() {
15+
filepath := flag.String("file", "", "Path to the CSV file")
16+
seperator := flag.String("seperator", "", "CSV seperator, default is auto detect")
17+
header := flag.Bool("header", false, "CSV has header")
18+
19+
flag.Parse()
20+
21+
var sep rune
22+
if *seperator != "" {
23+
sep = []rune(*seperator)[0]
24+
}
25+
26+
if *filepath != "" {
27+
data, err := csv2json.ParseFile(*filepath, sep)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
jsonData, err := data.ToJSON(*header)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
println(string(jsonData))
38+
return
39+
}
40+
41+
// Workaround to avoid blocking (io.ReadAll) on stdin when no data input.
42+
// Read lines from stdin and append to csvData until EOF.
43+
lineChan := make(chan string)
44+
errChan := make(chan error)
45+
doneChan := make(chan struct{})
46+
47+
go func() {
48+
scanner := bufio.NewScanner(os.Stdin)
49+
for scanner.Scan() {
50+
lineChan <- scanner.Text()
51+
}
52+
if err := scanner.Err(); err != nil && err != io.EOF {
53+
errChan <- err
54+
}
55+
close(doneChan)
56+
}()
57+
58+
timeout := 1000 * time.Millisecond
59+
timer := time.NewTimer(timeout)
60+
defer timer.Stop()
61+
62+
var csvData []byte
63+
64+
for {
65+
select {
66+
case line := <-lineChan:
67+
csvData = append(csvData, []byte(line+"\n")...)
68+
if !timer.Stop() {
69+
<-timer.C
70+
}
71+
timer.Reset(timeout)
72+
case err := <-errChan:
73+
panic(err)
74+
case <-doneChan:
75+
if len(csvData) == 0 {
76+
fmt.Println("No CSV data provided")
77+
return
78+
}
79+
data, err := csv2json.Parse(csvData, sep)
80+
if err != nil {
81+
panic(err)
82+
}
83+
84+
jsonData, err := data.ToJSON(*header)
85+
if err != nil {
86+
panic(err)
87+
}
88+
89+
fmt.Println(string(jsonData))
90+
return
91+
case <-timer.C:
92+
fmt.Println("No CSV data provided (timeout)")
93+
return
94+
}
95+
}
96+
}

cmd/generate/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"encoding/csv"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
9+
"github.com/TechMDW/randish"
10+
)
11+
12+
func main() {
13+
const numHeaders = 1000
14+
const numRows = 10000
15+
16+
file, err := os.Create("test.csv")
17+
if err != nil {
18+
fmt.Println("Error creating file:", err)
19+
return
20+
}
21+
defer file.Close()
22+
23+
writer := csv.NewWriter(file)
24+
defer writer.Flush()
25+
26+
headers := make([]string, numHeaders)
27+
for i := 0; i < numHeaders; i++ {
28+
headers[i] = "Column" + strconv.Itoa(i+1)
29+
}
30+
if err := writer.Write(headers); err != nil {
31+
fmt.Println("Error writing headers:", err)
32+
return
33+
}
34+
35+
rand := randish.RandS()
36+
37+
for i := 0; i < numRows; i++ {
38+
row := make([]string, numHeaders)
39+
for j := 0; j < numHeaders; j++ {
40+
row[j] = strconv.Itoa(rand.Intn(100) + 1)
41+
}
42+
if err := writer.Write(row); err != nil {
43+
fmt.Println("Error writing row:", err)
44+
return
45+
}
46+
}
47+
48+
fmt.Println("CSV file generated successfully.")
49+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/TechMDW/csv2json
2+
3+
go 1.22.0
4+
5+
require github.com/TechMDW/randish v0.0.0-20230622121239-ae1cb6f6bfa6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/TechMDW/randish v0.0.0-20230622121239-ae1cb6f6bfa6 h1:r4435wx45Wm++uZtyNg6/Sg7q2rDQFVroW/sAfRu4Bc=
2+
github.com/TechMDW/randish v0.0.0-20230622121239-ae1cb6f6bfa6/go.mod h1:X1PDNLQpmwGrhhbDUVvA+fJWUzZ1simGm0Fv/qmiSgk=

0 commit comments

Comments
 (0)