Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
johnuopini committed May 3, 2022
0 parents commit b214ff1
Show file tree
Hide file tree
Showing 14 changed files with 1,624 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# workflow name
name: Generate release-artifacts

# on events
on:
release:
types:
- created

# workflow tasks
jobs:
generate:
name: Generate cross-platform builds
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Generate build files
uses: thatisuday/go-cross-build@v1
with:
platforms: 'linux/amd64, darwin/amd64, darwin/arm64, linux/arm64'
package: 'csvexec'
name: 'csvexec'
compress: 'false'
dest: 'dist'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
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) [year] [fullname]

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.
81 changes: 81 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cmd

import (
"fmt"
"github.com/johnuopini/csvexec/internal/display"
"github.com/johnuopini/csvexec/internal/runner"
"github.com/johnuopini/csvexec/internal/utils"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Use: "csvexec csv_file template_file",
Short: "Execute a given command for every CSV line in parallel",
Args: cobra.MinimumNArgs(2),
Long: `This tool allows splitting a CSV job into multiple file using a template
engine to create execution scripts, tool will collect success/errors and write a
resulting CSV files with failed entries`,
Run: func(cmd *cobra.Command, args []string) {
csvFile := args[0]
if !utils.Exists(csvFile) {
panic(fmt.Errorf("file %v does not exist", csvFile))
}
tplFile := args[1]
if !utils.Exists(tplFile) {
panic(fmt.Errorf("file %v does not exist", csvFile))
}
workdir, _ := cmd.Flags().GetString("workdir")
if !utils.Exists(workdir) {
err := os.Mkdir(workdir, 0700)
if err != nil {
panic(err)
}
}
initLogging(workdir)
// Get jobs and start runner
jobs, _ := cmd.Flags().GetUint32("jobs")
runner, err := runner.NewCsvRunner(csvFile, tplFile, workdir, jobs)
if err != nil {
panic(err)
}
// Stop at
stopAt, _ := cmd.Flags().GetUint64("limit")
if stopAt == 0 {
stopAt = uint64(runner.Lines)
}
// Init progress
progress := display.New(int(stopAt), &runner)
progress.PrintHeader()
// Start
status, err := runner.Process(stopAt, progress.Callback)
if err != nil {
panic(err)
}
progress.PrintFooter(status)
},
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func initLogging(path string) {
f, err := os.OpenFile(fmt.Sprintf("%v/log.txt", path), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err)
}
// Set output of logs to f
log.SetOutput(f)
log.Printf("Log started")
}

func init() {
rootCmd.PersistentFlags().Uint32("jobs", 8, "number of jobs to execute in parallel")
rootCmd.PersistentFlags().Uint64("limit", 0, "stop after executing limit jobs")
rootCmd.PersistentFlags().String("workdir", "/tmp/csvexec", "job working directory")
}
Loading

0 comments on commit b214ff1

Please sign in to comment.