Skip to content

Commit

Permalink
First version.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhinLiang committed May 29, 2023
1 parent 71079d3 commit 902241d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
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/xhinliang/simplex

go 1.19

require github.com/xhinliang/gosimplifier v0.0.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/xhinliang/gosimplifier v0.0.1 h1:O13hnQ11M6bD+p+f2Hs87vSZBLA1te2U0Lbh2UQn/YA=
github.com/xhinliang/gosimplifier v0.0.1/go.mod h1:u/umizOiafnyc4rXW0WLctnGk41ixUYjADWKNzSGsKo=
81 changes: 81 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"

"github.com/xhinliang/gosimplifier"
)

var configFile string

func init() {
flag.StringVar(&configFile, "c", "", "Path to configuration file.")
flag.Parse()
}

func main() {
if configFile == "" {
if _, err := os.Stat(".simplex.json"); err == nil {
configFile = ".simplex.json"
} else if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".simplex.json")); err == nil {
configFile = filepath.Join(os.Getenv("HOME"), ".simplex.json")
} else {
fmt.Println("No configuration file found. Please provide one using the -c option.")
os.Exit(1)
}
}

// Load configuration file
config, err := os.ReadFile(configFile)
if err != nil {
fmt.Printf("Error reading configuration file: %s\n", err)
os.Exit(1)
}

// Create new simplifier
simplifier, err := gosimplifier.NewSimplifier(string(config))
if err != nil {
fmt.Printf("Error creating simplifier: %s\n", err)
os.Exit(1)
}

// Create a new Scanner that scans os.Stdin.
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input := scanner.Text()

// Unmarshal input JSON
var original map[string]interface{}
err := json.Unmarshal([]byte(input), &original)
if err != nil {
fmt.Printf("Error decoding JSON: %s\n", err)
os.Exit(1)
}

// Simplify JSON
simplified, err := simplifier.Simplify(original)
if err != nil {
fmt.Printf("Error simplifying JSON: %s\n", err)
os.Exit(1)
}

// Marshal JSON back to string
simplifiedJSON, err := json.Marshal(simplified)
if err != nil {
fmt.Printf("Error encoding JSON: %s\n", err)
os.Exit(1)
}

// Output simplified JSON
fmt.Println(string(simplifiedJSON))
}
if scanner.Err() != nil {
fmt.Printf("Scanner error: %s\n", scanner.Err())
os.Exit(1)
}
}

0 comments on commit 902241d

Please sign in to comment.