-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |