Files
velero/pkg/util/dotenv/dotenv.go
T
Xun Jiang/Bruce JiangandGitHub 25402b6209
Run the E2E test on kind / get-go-version (push) Successful in 59s
Run the E2E test on kind / setup-test-matrix (push) Successful in 4s
Main CI / get-go-version (push) Successful in 14s
Run the E2E test on kind / build (push) Failing after 1m48s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 25s
[1.18] Deprecate inactive maintained packages (#9912)
* Replace github.com/robfig/cron/v3 by github.com/netresearch/go-cron

Replace k8s.io/utils/pointer with k8s.io/utils/ptr

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace gopkg.in/yaml.v3 by go.yaml.in/yaml/v3

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace github.com/joho/godotenv.

Move the needed code into Velero repository.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

* Replace github.com/pkg/errors by github.com/cockroachdb/errors

Change errors.Cause to errors.Is, because github.com/cockroachdb/errors
New() function create a error with error stack with depth 1, but
github.com/pkg/errors's New() function create error with no depth.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>

---------

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>
Signed-off-by: Xun Jiang/Bruce Jiang <59276555+blackpiglet@users.noreply.github.com>
2026-07-10 11:20:34 +08:00

166 lines
3.6 KiB
Go

/*
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dotenv
import (
"bufio"
"fmt"
"os"
"strings"
)
// Read parses dotenv-style files and returns merged key/value pairs.
func Read(filenames ...string) (map[string]string, error) {
filenames = filenamesOrDefault(filenames)
envMap := make(map[string]string)
for _, filename := range filenames {
fileMap, err := readFile(filename)
if err != nil {
return nil, err
}
for key, value := range fileMap {
envMap[key] = value
}
}
return envMap, nil
}
// Overload loads dotenv-style files into process env vars, overriding existing values.
func Overload(filenames ...string) error {
filenames = filenamesOrDefault(filenames)
for _, filename := range filenames {
envMap, err := readFile(filename)
if err != nil {
return err
}
for key, value := range envMap {
if err := os.Setenv(key, value); err != nil {
return err
}
}
}
return nil
}
func filenamesOrDefault(filenames []string) []string {
if len(filenames) == 0 {
return []string{".env"}
}
return filenames
}
func readFile(filename string) (map[string]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
envMap := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
line = stripInlineComment(line)
key, value, err := parseLine(line)
if err != nil {
return nil, err
}
envMap[key] = value
}
if err := scanner.Err(); err != nil {
return nil, err
}
return envMap, nil
}
func stripInlineComment(line string) string {
inSingle := false
inDouble := false
for i, r := range line {
switch r {
case '\'':
if !inDouble {
inSingle = !inSingle
}
case '"':
if !inSingle {
inDouble = !inDouble
}
case '#':
if !inSingle && !inDouble {
return strings.TrimSpace(line[:i])
}
}
}
return line
}
func parseLine(line string) (string, string, error) {
if strings.HasPrefix(line, "export ") {
line = strings.TrimSpace(strings.TrimPrefix(line, "export "))
}
sep := strings.Index(line, "=")
colon := strings.Index(line, ":")
if sep == -1 || (colon != -1 && colon < sep) {
sep = colon
}
if sep == -1 {
return "", "", fmt.Errorf("invalid dotenv line: %q", line)
}
key := strings.TrimSpace(line[:sep])
rawValue := strings.TrimSpace(line[sep+1:])
if key == "" {
return "", "", fmt.Errorf("invalid dotenv line: %q", line)
}
value := parseValue(rawValue)
return key, value, nil
}
func parseValue(value string) string {
if len(value) >= 2 {
if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
unquoted := strings.TrimSuffix(strings.TrimPrefix(value, `"`), `"`)
unquoted = strings.ReplaceAll(unquoted, `\n`, "\n")
unquoted = strings.ReplaceAll(unquoted, `\r`, "\r")
unquoted = strings.ReplaceAll(unquoted, `\\`, `\`)
unquoted = strings.ReplaceAll(unquoted, `\"`, `"`)
return unquoted
}
if strings.HasPrefix(value, "'") && strings.HasSuffix(value, "'") {
return strings.TrimSuffix(strings.TrimPrefix(value, "'"), "'")
}
}
return value
}