-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nodetec/pterm-list
feat: implement strfry relay
- Loading branch information
Showing
15 changed files
with
677 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
|
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 |
---|---|---|
@@ -1,52 +1,56 @@ | ||
package manager | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"fmt" | ||
"github.com/pterm/pterm" | ||
"log" | ||
"os/exec" | ||
) | ||
|
||
// Function to check if a command exists | ||
func commandExists(command string) bool { | ||
_, err := exec.LookPath(command) | ||
return err == nil | ||
// Function to check if a package is installed | ||
func IsPackageInstalled(packageName string) bool { | ||
out, err := exec.Command("dpkg-query", "-W", "-f='${Status}'", packageName).Output() | ||
|
||
if err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
errorCode := exitError.ExitCode() | ||
// Package not found | ||
if errorCode == 1 { | ||
return false | ||
} else { | ||
log.Fatalf("Error checking if package is installed: %v", err) | ||
} | ||
} | ||
} | ||
|
||
status := string(out) | ||
|
||
if status == "'unknown ok not-installed'" { | ||
return false | ||
} else if status == "'install ok installed'" { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// Function to install necessary packages | ||
func AptInstallPackages() { | ||
spinner, _ := pterm.DefaultSpinner.Start("Updating and installing packages...") | ||
exec.Command("apt", "update", "-qq").Run() | ||
|
||
// Check if nginx is installed, install if not | ||
if commandExists("nginx") { | ||
spinner.UpdateText("nginx is already installed.") | ||
} else { | ||
spinner.UpdateText("Installing nginx...") | ||
exec.Command("apt", "install", "-y", "-qq", "nginx").Run() | ||
} | ||
exec.Command("apt", "update", "-qq").Run() | ||
|
||
// Check if Certbot is installed, install if not | ||
if commandExists("certbot") { | ||
spinner.UpdateText("Certbot is already installed.") | ||
} else { | ||
spinner.UpdateText("Installing Certbot...") | ||
exec.Command("apt", "install", "-y", "-qq", "certbot", "python3-certbot-nginx").Run() | ||
} | ||
packages := []string{"nginx", "certbot", "python3-certbot-nginx", "ufw", "fail2ban"} | ||
|
||
// Check if ufw is installed, install if not | ||
if commandExists("ufw") { | ||
spinner.UpdateText("ufw is already installed.") | ||
} else { | ||
spinner.UpdateText("Installing ufw...") | ||
exec.Command("apt", "install", "-y", "-qq", "ufw").Run() | ||
// Check if package is installed, install if not | ||
for _, p := range packages { | ||
if IsPackageInstalled(p) { | ||
spinner.UpdateText(fmt.Sprintf("%s is already installed.", p)) | ||
} else { | ||
spinner.UpdateText(fmt.Sprintf("Installing %s...", p)) | ||
exec.Command("apt", "install", "-y", "-qq", p).Run() | ||
} | ||
} | ||
|
||
spinner.Success("Packages updated and installed successfully.") | ||
} | ||
|
||
// Function to check if a package is installed | ||
func isPackageInstalled(packageName string) bool { | ||
cmd := exec.Command("dpkg", "-l", packageName) | ||
err := cmd.Run() | ||
return err == nil | ||
} |
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
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
Oops, something went wrong.