Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
JocularMarrow committed May 31, 2024
0 parents commit 2f72290
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 0 deletions.
117 changes: 117 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: Build and Release

on:
push:
tags:
- "v*" # Trigger the workflow on tags starting with "v"

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
goos: [linux, darwin, windows]
goarch: [386, amd64, arm, arm64]
exclude:
# Exclude certain combinations
- goos: darwin
goarch: 386
- goos: darwin
goarch: arm

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22" # Adjust the Go version if necessary

- name: Build binary
run: |
mkdir -p bin
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
- name: List files for debug
run: ls -la bin

- name: Move binary to release directory
run: |
mkdir -p release
ls -la release
mv bin/csv2json_${{ matrix.goos }}_${{ matrix.goarch }}* release/
ls -la release
- name: List release directory for debug
run: ls -la release

- name: Upload Release Assets
uses: actions/upload-artifact@v4
with:
name: release-assets-${{ matrix.goos }}-${{ matrix.goarch }}
path: release/

release:
runs-on: ubuntu-latest
needs: build

steps:
- name: Download Release Assets
uses: actions/download-artifact@v4
with:
path: release-assets
pattern: release-assets-*
merge-multiple: true

- name: Create directories for compression
run: mkdir -p release-compressed

- name: Compress Release Assets
run: |
version=${GITHUB_REF#refs/tags/v}
# Set up name mappings
declare -A goos_map=( ["linux"]="linux" ["darwin"]="macos" ["windows"]="windows" )
declare -A goarch_map=( ["386"]="x86" ["amd64"]="x64" ["arm"]="arm" ["arm64"]="arm64" )
# Compress binaries
for file in release-assets/*; do
base_name=$(basename "$file")
echo "Compressing $base_name"
# Extract goos and goarch
parts=(${base_name//_/ })
goos=${parts[1]}
goarch_with_ext=${parts[2]}
goarch=${goarch_with_ext%%.*} # Remove the file extension
friendly_goos=${goos_map[$goos]}
friendly_goarch=${goarch_map[$goarch]}
# Rename the file to csv2json or csv2json.exe
if [[ $goos == "windows" ]]; then
mv "$file" release-assets/csv2json.exe
file="release-assets/csv2json.exe"
else
mv "$file" release-assets/csv2json
file="release-assets/csv2json"
fi
# Use .zip for macOS and Windows, .tar.gz for Linux
if [[ $goos == "windows" || $goos == "darwin" ]]; then
zip_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.zip"
zip release-compressed/${zip_filename} -j "$file"
else
tar_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.tar.gz"
tar -czvf release-compressed/${tar_filename} -C release-assets csv2json
fi
done
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
files: release-compressed/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.csv
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 TechMDW

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added README.md
Empty file.
96 changes: 96 additions & 0 deletions cmd/csv2json/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"time"

csv2json "github.com/TechMDW/csv2json/pkg"
)

func main() {
filepath := flag.String("file", "", "Path to the CSV file")
seperator := flag.String("seperator", "", "CSV seperator, default is auto detect")
header := flag.Bool("header", false, "CSV has header")

flag.Parse()

var sep rune
if *seperator != "" {
sep = []rune(*seperator)[0]
}

if *filepath != "" {
data, err := csv2json.ParseFile(*filepath, sep)
if err != nil {
panic(err)
}

jsonData, err := data.ToJSON(*header)
if err != nil {
panic(err)
}

println(string(jsonData))
return
}

// Workaround to avoid blocking (io.ReadAll) on stdin when no data input.
// Read lines from stdin and append to csvData until EOF.
lineChan := make(chan string)
errChan := make(chan error)
doneChan := make(chan struct{})

go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lineChan <- scanner.Text()
}
if err := scanner.Err(); err != nil && err != io.EOF {
errChan <- err
}
close(doneChan)
}()

timeout := 1000 * time.Millisecond
timer := time.NewTimer(timeout)
defer timer.Stop()

var csvData []byte

for {
select {
case line := <-lineChan:
csvData = append(csvData, []byte(line+"\n")...)
if !timer.Stop() {
<-timer.C
}
timer.Reset(timeout)
case err := <-errChan:
panic(err)
case <-doneChan:
if len(csvData) == 0 {
fmt.Println("No CSV data provided")
return
}
data, err := csv2json.Parse(csvData, sep)
if err != nil {
panic(err)
}

jsonData, err := data.ToJSON(*header)
if err != nil {
panic(err)
}

fmt.Println(string(jsonData))
return
case <-timer.C:
fmt.Println("No CSV data provided (timeout)")
return
}
}
}
49 changes: 49 additions & 0 deletions cmd/generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"encoding/csv"
"fmt"
"os"
"strconv"

"github.com/TechMDW/randish"
)

func main() {
const numHeaders = 1000
const numRows = 10000

file, err := os.Create("test.csv")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()

writer := csv.NewWriter(file)
defer writer.Flush()

headers := make([]string, numHeaders)
for i := 0; i < numHeaders; i++ {
headers[i] = "Column" + strconv.Itoa(i+1)
}
if err := writer.Write(headers); err != nil {
fmt.Println("Error writing headers:", err)
return
}

rand := randish.RandS()

for i := 0; i < numRows; i++ {
row := make([]string, numHeaders)
for j := 0; j < numHeaders; j++ {
row[j] = strconv.Itoa(rand.Intn(100) + 1)
}
if err := writer.Write(row); err != nil {
fmt.Println("Error writing row:", err)
return
}
}

fmt.Println("CSV file generated successfully.")
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/TechMDW/csv2json

go 1.22.0

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

0 comments on commit 2f72290

Please sign in to comment.