Closes #5 Reviewed-on: #9 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
const App_Name = "catapult"
|
|
|
|
type App struct {
|
|
ApiUrl string
|
|
AuthUrl string
|
|
ServiceUsername string
|
|
ServicePassphrase string
|
|
}
|
|
|
|
func Load() (*App, error) {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
cwd, _ := os.Getwd()
|
|
envPath := path.Join(cwd, "../..", ".env")
|
|
if err = godotenv.Load(envPath); err != nil {
|
|
prevPath := path.Join(envPath, "../..", ".env")
|
|
if err = godotenv.Load(prevPath); err != nil {
|
|
return nil, fmt.Errorf("Error loading .env file: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
apiUrl := os.Getenv("API_URL")
|
|
authUrl := os.Getenv("AUTH_URL")
|
|
serviceUsername := os.Getenv("SERVICE_USERNAME")
|
|
servicePassphrase := os.Getenv("SERVICE_PASSPHRASE")
|
|
|
|
if len(apiUrl) == 0 {
|
|
return nil, fmt.Errorf("Api Url not provided")
|
|
} else if len(authUrl) == 0 {
|
|
return nil, fmt.Errorf("Auth url not provided")
|
|
} else if len(serviceUsername) == 0 {
|
|
return nil, fmt.Errorf("Service username not provided")
|
|
} else if len(servicePassphrase) == 0 {
|
|
return nil, fmt.Errorf("Service passphrase not provided")
|
|
} else {
|
|
return &App{
|
|
ApiUrl: apiUrl, AuthUrl: authUrl, ServiceUsername: serviceUsername, ServicePassphrase: servicePassphrase,
|
|
}, nil
|
|
}
|
|
}
|