Skip to content

Commit 3278ac8

Browse files
authored
Merge pull request #6 from vinitkumar/feat/improve-linux-support
feat: improve linux support
2 parents 13562ae + f6f5e17 commit 3278ac8

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

main.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,87 @@ func (l *LinuxJoiner) GetName() string {
9191
return fmt.Sprintf("Linux %s", l.backend)
9292
}
9393

94+
// detectLinuxDistribution detects the Linux distribution
95+
func detectLinuxDistribution() string {
96+
// Check for common distribution files
97+
distFiles := map[string]string{
98+
"/etc/os-release": "",
99+
"/etc/debian_version": "debian",
100+
"/etc/redhat-release": "redhat",
101+
"/etc/fedora-release": "fedora",
102+
"/etc/arch-release": "arch",
103+
"/etc/SuSE-release": "suse",
104+
}
105+
106+
for file, dist := range distFiles {
107+
if fileExists(file) {
108+
if dist != "" {
109+
return dist
110+
}
111+
// Read os-release file for more detailed info
112+
if file == "/etc/os-release" {
113+
content, err := os.ReadFile(file)
114+
if err == nil {
115+
contentStr := strings.ToLower(string(content))
116+
if strings.Contains(contentStr, "ubuntu") || strings.Contains(contentStr, "debian") {
117+
return "debian"
118+
} else if strings.Contains(contentStr, "fedora") {
119+
return "fedora"
120+
} else if strings.Contains(contentStr, "centos") || strings.Contains(contentStr, "rhel") {
121+
return "redhat"
122+
} else if strings.Contains(contentStr, "arch") {
123+
return "arch"
124+
}
125+
}
126+
}
127+
}
128+
}
129+
130+
// Default to debian if we can't detect
131+
return "debian"
132+
}
133+
134+
// installPDFTools installs PDF tools based on the Linux distribution
135+
func installPDFTools(distribution string) error {
136+
var cmd *exec.Cmd
137+
138+
switch distribution {
139+
case "debian", "ubuntu":
140+
fmt.Println("Installing PDF tools for Debian/Ubuntu...")
141+
cmd = exec.Command("sudo", "apt-get", "update")
142+
if err := cmd.Run(); err != nil {
143+
return fmt.Errorf("failed to update package list: %v", err)
144+
}
145+
cmd = exec.Command("sudo", "apt-get", "install", "-y", "poppler-utils", "ghostscript")
146+
if err := cmd.Run(); err != nil {
147+
return fmt.Errorf("failed to install PDF tools: %v", err)
148+
}
149+
case "fedora":
150+
fmt.Println("Installing PDF tools for Fedora...")
151+
cmd = exec.Command("sudo", "dnf", "install", "-y", "poppler-utils", "ghostscript")
152+
if err := cmd.Run(); err != nil {
153+
return fmt.Errorf("failed to install PDF tools: %v", err)
154+
}
155+
case "redhat", "centos":
156+
fmt.Println("Installing PDF tools for RHEL/CentOS...")
157+
cmd = exec.Command("sudo", "yum", "install", "-y", "poppler-utils", "ghostscript")
158+
if err := cmd.Run(); err != nil {
159+
return fmt.Errorf("failed to install PDF tools: %v", err)
160+
}
161+
case "arch":
162+
fmt.Println("Installing PDF tools for Arch Linux...")
163+
cmd = exec.Command("sudo", "pacman", "-S", "--noconfirm", "poppler", "ghostscript")
164+
if err := cmd.Run(); err != nil {
165+
return fmt.Errorf("failed to install PDF tools: %v", err)
166+
}
167+
default:
168+
return fmt.Errorf("unsupported Linux distribution: %s", distribution)
169+
}
170+
171+
fmt.Println("PDF tools installed successfully!")
172+
return nil
173+
}
174+
94175
// NewLinuxJoiner creates a new LinuxJoiner with the best available backend
95176
func NewLinuxJoiner() (*LinuxJoiner, error) {
96177
// Priority order of backends to try
@@ -102,6 +183,23 @@ func NewLinuxJoiner() (*LinuxJoiner, error) {
102183
}
103184
}
104185

186+
// If no backend is available, try to install them
187+
fmt.Println("No PDF joining tools found. Attempting to install them...")
188+
189+
distribution := detectLinuxDistribution()
190+
fmt.Printf("Detected Linux distribution: %s\n", distribution)
191+
192+
if err := installPDFTools(distribution); err != nil {
193+
return nil, fmt.Errorf("failed to install PDF tools: %v", err)
194+
}
195+
196+
// Try again after installation
197+
for _, backend := range backends {
198+
if _, err := exec.LookPath(backend); err == nil {
199+
return &LinuxJoiner{backend: backend, command: backend}, nil
200+
}
201+
}
202+
105203
return nil, fmt.Errorf("no suitable PDF joining tool found on Linux. Please install one of: %s", strings.Join(backends, ", "))
106204
}
107205

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestNewPDFJoiner(t *testing.T) {
9292
// The test should work on both macOS and Linux
9393
switch runtime.GOOS {
9494
case "darwin":
95-
if err != nil && joiner == nil {
95+
if err != nil {
9696
// It's OK if macOS joiner is not available in test environment
9797
t.Logf("macOS PDF joiner not available in test environment: %v", err)
9898
return
@@ -101,7 +101,7 @@ func TestNewPDFJoiner(t *testing.T) {
101101
t.Errorf("Expected macOS joiner, got: %s", joiner.GetName())
102102
}
103103
case "linux":
104-
if err != nil && joiner == nil {
104+
if err != nil {
105105
// It's OK if Linux tools are not available in test environment
106106
t.Logf("Linux PDF tools not available in test environment: %v", err)
107107
return

0 commit comments

Comments
 (0)