Skip to content

Commit

Permalink
Support using JSONC as config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhinLiang committed May 29, 2023
1 parent bc03536 commit f8b44e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/xhinliang/simplex
go 1.19

require github.com/xhinliang/gosimplifier v0.0.1

require muzzammil.xyz/jsonc v1.0.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/xhinliang/gosimplifier v0.0.1 h1:O13hnQ11M6bD+p+f2Hs87vSZBLA1te2U0Lbh2UQn/YA=
github.com/xhinliang/gosimplifier v0.0.1/go.mod h1:u/umizOiafnyc4rXW0WLctnGk41ixUYjADWKNzSGsKo=
muzzammil.xyz/jsonc v1.0.0 h1:B6kaT3wHueZ87mPz3q1nFuM1BlL32IG0wcq0/uOsQ18=
muzzammil.xyz/jsonc v1.0.0/go.mod h1:rFv8tUUKe+QLh7v02BhfxXEf4ZHhYD7unR93HL/1Uvo=
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/xhinliang/gosimplifier"
"muzzammil.xyz/jsonc"
)

var configFile string
Expand All @@ -19,26 +20,36 @@ func init() {
}

func main() {
// Config file handling
if configFile == "" {
if _, err := os.Stat(".simplex.json"); err == nil {
configFile = ".simplex.json"
} else if _, err := os.Stat(".simplex.jsonc"); err == nil {
configFile = ".simplex.jsonc"
} else if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".simplex.json")); err == nil {
configFile = filepath.Join(os.Getenv("HOME"), ".simplex.json")
} else if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".simplex.jsonc")); err == nil {
configFile = filepath.Join(os.Getenv("HOME"), ".simplex.jsonc")
} 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)
configData, err := os.ReadFile(configFile)
if err != nil {
fmt.Printf("Error reading configuration file: %s\n", err)
os.Exit(1)
}

// If it is a JSONC file, convert it to JSON
if filepath.Ext(configFile) == ".jsonc" {
configData = jsonc.ToJSON(configData)
}

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

0 comments on commit f8b44e9

Please sign in to comment.