From 82811442a4e27cd300be7dab2d177209ee8bbdc1 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Fri, 4 Oct 2024 14:39:00 +1000 Subject: [PATCH] add command line flags --- .drone.sh | 37 +++++++++++++++++++++++++++++++ .drone.yml | 32 +++++++++++++++++++++++++++ .gitignore | 2 ++ README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 9 ++++++-- 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 .drone.sh create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 README.md diff --git a/.drone.sh b/.drone.sh new file mode 100644 index 0000000..bb85b11 --- /dev/null +++ b/.drone.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +# disable CGO for cross-compiling +export CGO_ENABLED=0 + +commit=$(git rev-parse HEAD) +buildtime=$(TZ=Australia/Sydney date +%Y-%m-%dT%T%z) +git_version=$(git describe --tags --always --long --dirty) +package_name=generate-chart + +#platforms=("linux/amd64" "darwin/amd64") +platforms=("linux/amd64", "windows/amd64") + +echo Building $package_name with git version: $git_version +for platform in "${platforms[@]}" +do + platform_split=(${platform//\// }) + GOOS=${platform_split[0]} + GOARCH=${platform_split[1]} + output_name=$package_name'-'$GOOS'-'$GOARCH + if [ $GOOS = "windows" ]; then + output_name+='.exe' + fi + + starttime=$(TZ=Australia/Sydney date +%Y-%m-%dT%T%z) + echo "build commences at $starttime" + env GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -ldflags="-X main.sha1ver=$commit -X main.buildTime=$buildtime" -o $output_name $package + if [ $? -ne 0 ]; then + echo 'An error has occurred! Aborting the script execution...' + exit 1 + fi + gzip $output_name + echo "build complete at $buildtime : $output_name" + sha256sum ${output_name}.gz > ${output_name}_checksum.txt +done + +ls -lah diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..06eaa3e --- /dev/null +++ b/.drone.yml @@ -0,0 +1,32 @@ +kind: pipeline +type: docker +name: default + +# Docs at https://docs.drone.io/pipeline/exec/overview/ +# Also see https://github.com/harness/drone-cli/blob/master/.drone.yml + +steps: + +- name: build + image: golang + environment: + CGO_ENABLED: 0 + commands: + - sh ./.drone.sh + + +- name: dell-sftp-deploy + image: hypervtechnics/drone-sftp + settings: + host: deft.dell.com + username: + from_secret: DELLFTP_USER + password: + from_secret: DELLFTP_PASS + port: 22 + source: ./ + filter: generate-chart* + clean: false + target: / + overwrite: true + verbose: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14eaf51 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.json +*.xlsx \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4ef0092 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# Excel Chart Generator + +Using the excellent open source [excelize](https://github.com/qax-os/excelize) library, convert json data tracking numbers over time into an excel workbook of graphs. + +## Input data + +Here is some sample input data. Note that some of these key names are hard coded in the source code in order to reduce complexity. + +```json +{ + "Tracking": { + "AVG data 1": { + "Low": { + "DC1": { + "September-2024": 17948.94, + "August-2024": 17947.40, + "July-2024": 17470.34, + "June-2024": 17648.02, + "May-2024": 17314.25 + }, + "DC2": { + "September-2024": 21226.84, + "August-2024": 20956.31, + "July-2024": 20021.72, + "June-2024": 19713.80, + "May-2024": 19945.32 + } + }, + "Ultra": { + "DC1": { + "September-2024": 4544, + "August-2024": 4544, + "July-2024": 4685.90, + "June-2024": 4741.55, + "May-2024": 4830.26 + }, + "DC2": { + "September-2024": 5369.58, + "August-2024": 5372, + "July-2024": 5357.38, + "June-2024": 5344.6, + "May-2024": 5306.26 + } + } + }, + "count 1": { + "DC1": { + "September-2024": 6801.80, + "August-2024": 6804.71, + "July-2024": 6664.03, + "June-2024": 6673.87, + "May-2024": 6656.42 + }, + "DC2": { + "September-2024": 6751.17, + "August-2024": 6765.42, + "July-2024": 6597.65, + "June-2024": 6510.47, + "May-2024": 6487.19 + } + } + } +} +``` \ No newline at end of file diff --git a/main.go b/main.go index 2404496..fe59bc5 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "io" "log" @@ -239,6 +240,10 @@ func AvgChart(f *excelize.File, worksheetName string, location string, avgCpuCol func main() { var err error + inputFile := flag.String("input", "input.json", "The filename from which to load historical data") + outputFile := flag.String("output", "book1.xlsx", "The filename to use when writing excel workbook") + flag.Parse() + // Create the workbook f := excelize.NewFile() defer func() { @@ -252,7 +257,7 @@ func main() { } // Load the JSON data from file - file, err := os.Open("input.json") + file, err := os.Open(*inputFile) if err != nil { log.Fatalf("Failed to open input.json: %v", err) } @@ -271,7 +276,7 @@ func main() { GenerateCharts(f, data.Tracking.VmCount, "VM Count", "N60") // Save workbook - if err := f.SaveAs("Book1.xlsx"); err != nil { + if err := f.SaveAs(*outputFile); err != nil { fmt.Println(err) } }