fix: rename envfile flag to env-file with backwards compatibility

This commit is contained in:
AJ ONeal 2026-08-13 14:59:47 -06:00
parent 71671b7852
commit 6a1e49f20a
No known key found for this signature in database
2 changed files with 24 additions and 4 deletions

View File

@ -108,8 +108,8 @@ func main() {
QueryParamNames: nil, // []string{"access_token", "token"}, QueryParamNames: nil, // []string{"access_token", "token"},
} }
// Peek for --envfile early // Peek for --env-file early
envPath := peekOption(os.Args[1:], []string{"-envfile", "--envfile"}, ".env") envPath := peekOption(os.Args[1:], []string{"-env-file", "--env-file", "-envfile", "--envfile"}, ".env")
_ = godotenv.Load(envPath) // silent if missing _ = godotenv.Load(envPath) // silent if missing
// Override defaults from env // Override defaults from env
@ -608,7 +608,8 @@ func (cli *MainConfig) authenticate(r *http.Request) (auth.BasicPrinciple, error
return cred, err return cred, err
} }
// peekOption looks for a flag value without parsing the full set // peekOption looks for a flag value without parsing the full set.
// Used to handle --env-file before flag.Parse so it doesn't conflict with other flags.
func peekOption(args []string, names []string, def string) string { func peekOption(args []string, names []string, def string) string {
for i := range len(args) { for i := range len(args) {
for _, name := range names { for _, name := range names {

View File

@ -78,7 +78,8 @@ func main() {
maxDelay: 2 * time.Minute, maxDelay: 2 * time.Minute,
} }
_ = godotenv.Load("./.env") envPath := peekOption(os.Args[1:], []string{"-env-file", "--env-file", "-envfile", "--envfile"}, ".env")
_ = godotenv.Load(envPath)
// note: we could also use twilio, or whatever // note: we could also use twilio, or whatever
var sender SMSSender = androidsmsgateway.New( var sender SMSSender = androidsmsgateway.New(
@ -104,6 +105,9 @@ func main() {
now := time.Now() now := time.Now()
zoneName, offset := now.Zone() zoneName, offset := now.Zone()
// --env-file is loaded via peekOption above; flag registered here for help output.
_ = flag.String("env-file", ".env", "path to .env file (default: .env in current directory)")
flag.BoolVar(&cfg.confirmed, "y", false, "Confirm without prompting") flag.BoolVar(&cfg.confirmed, "y", false, "Confirm without prompting")
flag.BoolVar(&cfg.verbose, "verbose", false, "Show parse warnings and other debug info") flag.BoolVar(&cfg.verbose, "verbose", false, "Show parse warnings and other debug info")
flag.BoolVar(&cfg.dryRun, "dry-run", false, "Skip sending messages and sleeping, runs without confirmation") flag.BoolVar(&cfg.dryRun, "dry-run", false, "Skip sending messages and sleeping, runs without confirmation")
@ -620,3 +624,18 @@ func parseClock(s string, ref time.Time) (t time.Time, err error) {
t = time.Date(ref.Year(), ref.Month(), ref.Day(), hour, min, 0, 0, ref.Location()) t = time.Date(ref.Year(), ref.Month(), ref.Day(), hour, min, 0, 0, ref.Location())
return t, nil return t, nil
} }
// peekOption looks for a flag value without parsing the full set.
// Used to handle --env-file before flag.Parse so it doesn't conflict with other flags.
func peekOption(args []string, names []string, def string) string {
for i := range len(args) {
for _, name := range names {
if args[i] == name {
if i+1 < len(args) {
return args[i+1]
}
}
}
}
return def
}