Skip to content

Commit

Permalink
add flags
Browse files Browse the repository at this point in the history
  • Loading branch information
do-i-need-a-username committed Nov 29, 2023
1 parent 0a084a1 commit e2dd0fc
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
Expand All @@ -10,12 +11,16 @@ import (
)

func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a json file as a command line argument.")
input := flag.String("input", "", "Input JSON file")
output := flag.String("output", "", "Output file")
flag.Parse()

if *input == "" {
fmt.Println("Please provide an input json file using the -input flag.")
os.Exit(1)
}

jsonFile, err := os.Open(os.Args[1])
jsonFile, err := os.Open(*input)
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(1)
Expand All @@ -35,7 +40,10 @@ func main() {
os.Exit(1)
}

outputFileName := strings.TrimSuffix(os.Args[1], filepath.Ext(os.Args[1])) + ".ndjson"
outputFileName := *output
if outputFileName == "" {
outputFileName = strings.TrimSuffix(*input, filepath.Ext(*input)) + ".ndjson"
}
outputFile, err := os.Create(outputFileName)
if err != nil {
fmt.Println("Error creating output file:", err)
Expand Down

0 comments on commit e2dd0fc

Please sign in to comment.