-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func LoadProfile(name string) error { | ||
if name == "" { | ||
return fmt.Errorf("profile name cannot be empty") | ||
} | ||
|
||
currentUser, err := user.Current() | ||
if err != nil { | ||
return fmt.Errorf("failed to get current user: %v", err) | ||
} | ||
|
||
configPath := filepath.Join("/home", currentUser.Username, ".config", ProjectName, configFileName) | ||
configContent, err := os.ReadFile(configPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read config file: %v", err) | ||
} | ||
|
||
var profileDir string | ||
for _, line := range strings.Split(string(configContent), "\n") { | ||
if strings.HasPrefix(line, "PROFILE_DIR=") { | ||
profileDir = strings.TrimPrefix(line, "PROFILE_DIR=") | ||
profileDir = strings.Split(profileDir, "#")[0] | ||
profileDir = strings.TrimSpace(profileDir) | ||
break | ||
} | ||
} | ||
|
||
if profileDir == "" { | ||
return fmt.Errorf("PROFILE_DIR not found in config") | ||
} | ||
|
||
profilePath := filepath.Join(profileDir, name+".env") | ||
file, err := os.Open(profilePath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open profile: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
line = strings.TrimSpace(line) | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
parts := strings.SplitN(line, "=", 2) | ||
if len(parts) == 2 { | ||
key := strings.TrimSpace(parts[0]) | ||
value := strings.TrimSpace(parts[1]) | ||
if key != "" { | ||
os.Setenv(key, value) | ||
} | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return fmt.Errorf("error reading profile: %v", err) | ||
} | ||
|
||
fmt.Printf("Loaded profile: %s\n", name) | ||
return nil | ||
} |