move to environment variables for secrets

This commit is contained in:
lyx0 2023-05-06 16:01:19 +02:00
parent a284e2e591
commit 1e59a7fedb
5 changed files with 29 additions and 13 deletions

2
.gitignore vendored
View file

@ -21,3 +21,5 @@ bin/
# Go workspace file
go.work
.env

View file

@ -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}

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

35
main.go
View file

@ -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()