Pārlūkot izejas kodu

Load .env via godotenv for CHATTO_TOKEN; .env gitignored

Paul Klumpp 3 dienas atpakaļ
vecāks
revīzija
7287a6878f
5 mainītis faili ar 19 papildinājumiem un 6 dzēšanām
  1. 2 0
      .gitignore
  2. 3 3
      README.md
  3. 11 3
      cmd/cheapo/chatto.go
  4. 1 0
      go.mod
  5. 2 0
      go.sum

+ 2 - 0
.gitignore

@@ -3,3 +3,5 @@
 *.test
 *.out
 data/
+.env
+

+ 3 - 3
README.md

@@ -124,9 +124,9 @@ CLI commands (credentials deliberately provided by the operator, never
 fetched automatically):
 
 ```bash
-export CHATTO_TOKEN="<token>"        # set by the operator
-# Base URL default: https://chat.chatto.run
-# (overridable via CHATTO_BASE_URL)
+# .env in the project root (godotenv), never commit it — it is in .gitignore:
+CHATTO_TOKEN="<token>"               # set by the operator
+# CHATTO_BASE_URL=https://chat.chatto.run   # optional, is the default
 
 cheapo chatto-rooms                     # list rooms (client verification)
 cheapo chatto-send --room <room_id>     # post current free games

+ 11 - 3
cmd/cheapo/chatto.go

@@ -5,17 +5,25 @@ import (
 	"os"
 	"strings"
 
+	"github.com/joho/godotenv"
 	"github.com/spf13/cobra"
 
 	"gogs.netdome.biz/paul/cheapo/internal/chatto"
 	"gogs.netdome.biz/paul/cheapo/internal/game"
 )
 
+// loadEnv reads a .env file from the working directory if present (godotenv).
+// Existing environment variables take precedence over .env values.
+func loadEnv() {
+	_ = godotenv.Load()
+}
+
 // chattoClientFromEnv builds a Chatto client from the CHATTO_TOKEN
-// environment variable; CHATTO_BASE_URL defaults to the public Chatto
-// server (https://chat.chatto.run). The token is provided by the operator;
-// this tool never fetches credentials itself.
+// environment variable (or .env file); CHATTO_BASE_URL defaults to the public
+// Chatto server (https://chat.chatto.run). The token is provided by the
+// operator; this tool never fetches credentials itself.
 func chattoClientFromEnv() (*chatto.Client, error) {
+	loadEnv()
 	base := os.Getenv("CHATTO_BASE_URL")
 	if base == "" {
 		base = "https://chat.chatto.run"

+ 1 - 0
go.mod

@@ -4,6 +4,7 @@ go 1.27.1
 
 require (
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
+	github.com/joho/godotenv v1.5.1 // indirect
 	github.com/spf13/cobra v1.10.2 // indirect
 	github.com/spf13/pflag v1.0.9 // indirect
 	golang.org/x/net v0.62.0 // indirect

+ 2 - 0
go.sum

@@ -1,6 +1,8 @@
 github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
 github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
 github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=