2
This commit is contained in:
63
install.go
Normal file
63
install.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InstallPlan struct {
|
||||
ManagerName string
|
||||
Command []string
|
||||
}
|
||||
|
||||
func (p InstallPlan) DisplayCommand() string {
|
||||
return strings.Join(p.Command, " ")
|
||||
}
|
||||
|
||||
func execLookPath(file string) (string, error) {
|
||||
return exec.LookPath(file)
|
||||
}
|
||||
|
||||
func DetectSambaInstallPlan(lookPath LookPathFunc, isRoot bool) (InstallPlan, bool) {
|
||||
sudoPrefix := []string{}
|
||||
if !isRoot {
|
||||
if _, err := lookPath("sudo"); err == nil {
|
||||
sudoPrefix = []string{"sudo"}
|
||||
}
|
||||
}
|
||||
|
||||
plans := []struct {
|
||||
bin string
|
||||
name string
|
||||
command []string
|
||||
}{
|
||||
{bin: "apt-get", name: "apt", command: []string{"apt-get", "update"}},
|
||||
{bin: "apt-get", name: "apt", command: []string{"apt-get", "install", "-y", "samba"}},
|
||||
{bin: "dnf", name: "dnf", command: []string{"dnf", "install", "-y", "samba"}},
|
||||
{bin: "yum", name: "yum", command: []string{"yum", "install", "-y", "samba"}},
|
||||
{bin: "pacman", name: "pacman", command: []string{"pacman", "-Sy", "--noconfirm", "samba"}},
|
||||
{bin: "zypper", name: "zypper", command: []string{"zypper", "--non-interactive", "install", "samba"}},
|
||||
{bin: "apk", name: "apk", command: []string{"apk", "add", "samba"}},
|
||||
}
|
||||
|
||||
if _, err := lookPath("apt-get"); err == nil {
|
||||
return InstallPlan{
|
||||
ManagerName: "apt",
|
||||
Command: append(
|
||||
append([]string{}, sudoPrefix...),
|
||||
"sh", "-c", "apt-get update && apt-get install -y samba",
|
||||
),
|
||||
}, true
|
||||
}
|
||||
|
||||
for _, plan := range plans[2:] {
|
||||
if _, err := lookPath(plan.bin); err == nil {
|
||||
return InstallPlan{
|
||||
ManagerName: plan.name,
|
||||
Command: append(append([]string{}, sudoPrefix...), plan.command...),
|
||||
}, true
|
||||
}
|
||||
}
|
||||
|
||||
return InstallPlan{}, false
|
||||
}
|
||||
Reference in New Issue
Block a user