From 1e59a7fedb55b63e4fa9448756bf263e9566d8dd Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 16:01:19 +0200 Subject: [PATCH] move to environment variables for secrets --- .gitignore | 2 ++ Makefile | 2 +- go.mod | 1 + go.sum | 2 ++ main.go | 35 +++++++++++++++++++++++------------ 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 86a6fc9..0d4b06e 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ bin/ # Go workspace file go.work + +.env diff --git a/Makefile b/Makefile index 77d3f05..86386e4 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ cup: sudo docker compose up xd: - go build -o ./bin/${BINARY_NAME} && ./bin/${BINARY_NAME} --homeserver="matrix.xxx" --username="xxx" --password="xxx" + go build -o ./bin/${BINARY_NAME} && ./bin/${BINARY_NAME} diff --git a/go.mod b/go.mod index 65fe23c..ccdb67a 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/kisielk/sqlstruct v0.0.0-20210630145711-dae28ed37023 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/pty v1.1.8 // indirect diff --git a/go.sum b/go.sum index bec62c2..0127091 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kisielk/sqlstruct v0.0.0-20210630145711-dae28ed37023 h1:/pb3UJ+3ZtSEUKWnufwsoVF7f0AX5ytPULbTwHMgbq4= github.com/kisielk/sqlstruct v0.0.0-20210630145711-dae28ed37023/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= diff --git a/main.go b/main.go index b38defe..f801fbb 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,13 @@ import ( "errors" "flag" "fmt" + "log" "os" "sync" "time" "github.com/chzyer/readline" + "github.com/joho/godotenv" _ "github.com/mattn/go-sqlite3" "github.com/rs/zerolog" "maunium.net/go/mautrix" @@ -18,21 +20,30 @@ import ( "maunium.net/go/mautrix/id" ) -var homeserver = flag.String("homeserver", "", "Matrix homeserver") -var username = flag.String("username", "", "Matrix username localpart") -var password = flag.String("password", "", "Matrix passsword") -var database = flag.String("database", "mautrix-example.db", "SQLite database path") +type config struct { + matrixHomeserver string + matrixUser string + matrixPass string + database string +} + var debug = flag.Bool("debug", false, "Enable debug logs") func main() { flag.Parse() - if *username == "" || *password == "" || *homeserver == "" { - _, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - flag.PrintDefaults() - os.Exit(1) + + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env") } - client, err := mautrix.NewClient(*homeserver, "", "") + var cfg config + cfg.matrixHomeserver = os.Getenv("MATRIX_HOMESERVER") + cfg.matrixUser = os.Getenv("MATRIX_USERNAME") + cfg.matrixPass = os.Getenv("MATRIX_PASSWORD") + cfg.database = os.Getenv("SQLITE_DATABASE") + + client, err := mautrix.NewClient(cfg.matrixHomeserver, "", "") if err != nil { panic(err) } @@ -83,15 +94,15 @@ func main() { } }) - cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), *database) + cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), cfg.database) if err != nil { panic(err) } cryptoHelper.LoginAs = &mautrix.ReqLogin{ Type: mautrix.AuthTypePassword, - Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username}, - Password: *password, + Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: cfg.matrixUser}, + Password: cfg.matrixPass, } err = cryptoHelper.Init()