mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: MacOS MDM based Deployment (#277)
* fix: MacOS MDM deployment script * fix: Handle shell alias for bash on macos * fix: Code review fixes * fix: Code review fixes * feat: Add support for global config file * feat: Add support for global config file * fix: Code review fixes * fix: Avoid blocking CLI for analytics flush
This commit is contained in:
+28
-76
@@ -1,8 +1,6 @@
|
||||
package alias
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -154,20 +152,20 @@ func (a *AliasManager) IsInstalled() (bool, error) {
|
||||
}
|
||||
|
||||
for _, shell := range a.config.Shells {
|
||||
configPath := filepath.Join(homeDir, shell.Path())
|
||||
for _, configPath := range shell.CandidateRcFiles(homeDir) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Warnf("Warning: could not read %s (%s)", configPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Warnf("Warning: could not read %s (%s)", shell.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(string(data), a.config.RcFileName) {
|
||||
return true, nil
|
||||
if strings.Contains(string(data), a.config.RcFileName) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,10 +188,18 @@ func (a *AliasManager) sourceRcFile() error {
|
||||
return err
|
||||
}
|
||||
|
||||
primary := PrimaryShellName()
|
||||
for _, shell := range a.config.Shells {
|
||||
configPath := filepath.Join(homeDir, shell.Path())
|
||||
if err := a.addSourceLine(configPath, shell.Source(a.rcFileManager.GetRcPath())); err != nil {
|
||||
files, err := shell.InstallRcFiles(homeDir, shell.Name() == primary)
|
||||
if err != nil {
|
||||
log.Warnf("Warning: skipping %s (%s)", shell.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, configPath := range files {
|
||||
if err := a.addSourceLine(configPath, shell.Source(a.rcFileManager.GetRcPath())); err != nil {
|
||||
log.Warnf("Warning: skipping %s (%s)", configPath, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,70 +213,16 @@ func (a *AliasManager) removeSourceLinesFromShells() error {
|
||||
return err
|
||||
}
|
||||
|
||||
drop := func(line string) bool {
|
||||
return strings.Contains(line, a.config.RcFileName) ||
|
||||
strings.TrimSpace(line) == strings.TrimSpace(commentForRemovingShellSource)
|
||||
}
|
||||
|
||||
for _, shell := range a.config.Shells {
|
||||
configPath := filepath.Join(homeDir, shell.Path())
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
for _, configPath := range shell.CandidateRcFiles(homeDir) {
|
||||
if err := RewriteFileDroppingLines(configPath, drop); err != nil {
|
||||
log.Warnf("Warning: failed to update %s: %s", configPath, err)
|
||||
}
|
||||
|
||||
log.Warnf("Warning: skipping %s (%s)", shell.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Get original file permissions
|
||||
info, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
log.Warnf("Warning: skipping %s (%s)", shell.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Create temp file
|
||||
tempFile, err := os.CreateTemp(filepath.Dir(configPath), ".tmp-"+filepath.Base(configPath))
|
||||
if err != nil {
|
||||
log.Warnf("Warning: failed to create temporary file for %s: %s", configPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
tempPath := tempFile.Name()
|
||||
|
||||
// Write filtered content
|
||||
scanner := bufio.NewScanner(bytes.NewReader(data))
|
||||
writer := bufio.NewWriter(tempFile)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
// Skip source lines and comment
|
||||
if strings.Contains(line, a.config.RcFileName) ||
|
||||
strings.TrimSpace(line) == strings.TrimSpace(commentForRemovingShellSource) {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := writer.WriteString(line + "\n"); err != nil {
|
||||
log.Warnf("Warning: failed to write to temporary file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := writer.Flush(); err != nil {
|
||||
log.Warnf("Warning: failed to flush temporary file: %s", err)
|
||||
}
|
||||
|
||||
if err := tempFile.Close(); err != nil {
|
||||
log.Warnf("Warning: failed to close temporary file: %s", err)
|
||||
}
|
||||
|
||||
// Set permissions on temporary file to match original file.
|
||||
if err := os.Chmod(tempPath, info.Mode()); err != nil {
|
||||
log.Warnf("Warning: failed to set permissions on temporary file for %s: %s", configPath, err)
|
||||
}
|
||||
|
||||
// Replace original file
|
||||
if err := os.Rename(tempPath, configPath); err != nil {
|
||||
_ = os.Remove(tempPath)
|
||||
log.Warnf("Warning: failed to update %s: %s", configPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user