commit bda181c670f16bebaebd483aad49246058217a5f
parent fed3c4c0ef228a8585948b9bdb7ecf2623f532c6
Author: Joel-Haeberli <haebu@rubigen.ch>
Date: Wed, 22 May 2024 16:56:04 +0200
fix: allow cli to use ini config with -c flag
Diffstat:
3 files changed, 74 insertions(+), 3 deletions(-)
diff --git a/cli/cli.go b/cli/cli.go
@@ -16,6 +16,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/crypto/argon2"
+ "gopkg.in/ini.v1"
)
const ACTION_HELP = "h"
@@ -43,11 +44,40 @@ var DB *pgxpool.Pool
// register provider -> read wallee, read space(id), read userid, read password, hash what needs to be hashed, save to database
func main() {
+
+ iniFilePath := ""
+ connstr := ""
+ if len(os.Args) > 1 {
+
+ nextIsConf := false
+ for i, arg := range os.Args {
+ if i == 0 {
+ continue
+ } else if nextIsConf {
+ iniFilePath = arg
+ nextIsConf = false
+ } else if arg == "-c" {
+ nextIsConf = true
+ } else if arg == "-h" {
+ showHelp()
+ os.Exit(0)
+ }
+ }
+ }
+
+ if iniFilePath != "" {
+ optionalPgConnStr, err := parseDbConnstrFromIni(iniFilePath)
+ if err != nil {
+ fmt.Println("failed parsing config:", err.Error())
+ }
+ fmt.Println("read connection string from ini:", optionalPgConnStr)
+ connstr = optionalPgConnStr
+ }
+
fmt.Println("What do you want to do?")
showHelp()
- optionalPghost := os.Getenv("PGHOST")
- if optionalPghost != "" {
- err := connectDbUsingString(optionalPghost)
+ if connstr != "" {
+ err := connectDbUsingString(connstr)
if err != nil {
fmt.Println("error while connecting to database, using connection string from PGHOST. error:", err.Error())
}
@@ -60,6 +90,44 @@ func main() {
}
}
+func parseDbConnstrFromIni(path string) (string, error) {
+
+ f, err := os.Open(path)
+ if err != nil {
+ return "", err
+ }
+ defer f.Close()
+
+ stat, err := f.Stat()
+ if err != nil {
+ return "", err
+ }
+
+ content := make([]byte, stat.Size())
+ _, err = f.Read(content)
+ if err != nil {
+ return "", err
+ }
+
+ ini, err := ini.Load(content)
+ if err != nil {
+ return "", err
+ }
+
+ section := ini.Section("database")
+ if section == nil {
+ fmt.Println("database section not configured in ini file")
+ os.Exit(0)
+ }
+
+ value, err := section.GetKey("CONFIG")
+ if err != nil {
+ return "", err
+ }
+
+ return value.String(), nil
+}
+
func registerWalleeProvider() error {
if DB == nil {
diff --git a/cli/go.mod b/cli/go.mod
@@ -14,4 +14,5 @@ require (
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
+ gopkg.in/ini.v1 v1.67.0 // indirect
)
diff --git a/cli/go.sum b/cli/go.sum
@@ -25,6 +25,8 @@ golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
+gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=