gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-cashless2ecash] branch master updated: fix: allow cli to use ini


From: gnunet
Subject: [taler-cashless2ecash] branch master updated: fix: allow cli to use ini config with -c flag
Date: Wed, 22 May 2024 16:56:11 +0200

This is an automated email from the git hooks/post-receive script.

joel-haeberli pushed a commit to branch master
in repository cashless2ecash.

The following commit(s) were added to refs/heads/master by this push:
     new bda181c  fix: allow cli to use ini config with -c flag
bda181c is described below

commit bda181c670f16bebaebd483aad49246058217a5f
Author: Joel-Haeberli <haebu@rubigen.ch>
AuthorDate: Wed May 22 16:56:04 2024 +0200

    fix: allow cli to use ini config with -c flag
---
 cli/cli.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 cli/go.mod |  1 +
 cli/go.sum |  2 ++
 3 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/cli/cli.go b/cli/cli.go
index 94e73ff..9ac86a9 100644
--- 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
index c02aebc..7ba65f5 100644
--- 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
index 8c3d6b9..6e584dc 100644
--- 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=

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]