Skip to content

Commit

Permalink
move config to /opt/app-get + force run as admin
Browse files Browse the repository at this point in the history
  • Loading branch information
afelinczak committed Dec 27, 2022
1 parent 545d276 commit 6e6f7de
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var listCmd = &cobra.Command{
var apps = appRepo.Get()
fmt.Println("Installed applications")
for i := 0; i < len(apps); i++ {
fmt.Println(apps[i].App.Name + "(" + apps[i].Version + ")")
fmt.Println(apps[i].App.Name + " (" + apps[i].Version + ")")
}
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var updateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
var appRepo = infrastructure.AppRepository{}
var installedApps = appRepo.Get()
fmt.Println("APP-GET will update installed apps")

for i := 0; i < len(installedApps); i++ {
var version = infrastructure.GetLatestVersion(installedApps[i].App)
Expand Down
18 changes: 11 additions & 7 deletions infrastructure/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ import (
"log"
"os"
"os/user"
"strings"

"github.com/afelinczak/app-get/domain"
)

const APPS_FILE_PATH = "/home/{user}/.config/app-get"
const APPS_FILE_NAME = "apps.json"
const appFilePath = "/opt/app-get"
const appsFileName = "apps.json"

func getAppsPath() string {
// EnsureIsAdmin will panic the app-get if it is not runned with root priviliges
func EnsureIsAdmin() bool {
currentUser, _ := user.Current()
return strings.Replace(APPS_FILE_PATH, "{user}", currentUser.Username, 1)
if currentUser.Username != "root" {
println("APP-GET requires root access. Switch to root account or use sudo app-get.")
return false
}
return true
}

func getAppsFile() string {
return getAppsPath() + "/" + APPS_FILE_NAME
return appFilePath + "/" + appsFileName
}

// CreateInstalledAppListFile - creates empty apps.json file if not found
func CreateInstalledAppListFile() {
var _, err = os.Stat(getAppsFile())

if errors.Is(err, os.ErrNotExist) {
os.Mkdir(getAppsPath(), 0744)
os.Mkdir(appFilePath, 0744)
var newFileContent, errCreate = os.Create(getAppsFile())
if errCreate != nil {
log.Fatal(errCreate)
Expand Down
1 change: 1 addition & 0 deletions infrastructure/dpkgClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
)

// InstallApp runs dpkg using sudo
func InstallApp(path string) {
fmt.Println("Try to install deb package " + path)
var cmd = exec.Command("/bin/sh", "-c", "sudo dpkg -i "+path)
Expand Down
24 changes: 13 additions & 11 deletions infrastructure/githubClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import (
"github.com/afelinczak/app-get/domain"
)

const GITHUB_API_URL string = "https://api.github.com/"
const githubAPIURL string = "https://api.github.com/"

type GitHubAsset struct {
type gitHubAsset struct {
Name string `json:"name"`
BrowserDownloadUrl string `json:"browser_download_url"`
BrowserDownloadURL string `json:"browser_download_url"`
}

// GithubVersion contains latest version with list of assets
type GithubVersion struct {
Name string `json:"tag_name"`
Assets []GitHubAsset `json:"assets"`
Assets []gitHubAsset `json:"assets"`
}

// GetLatestVersion loads latest stable version of an app from github
func GetLatestVersion(app domain.App) GithubVersion {
var url = GITHUB_API_URL + "repos/" + app.SourceUrl + "/releases/latest"
var url = githubAPIURL + "repos/" + app.SourceUrl + "/releases/latest"
var resp, err = http.Get(url)
if err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -58,27 +60,27 @@ func GetInstallationFile(app domain.App, source GithubVersion) (string, bool) {
os.Exit(103)
}

var appUrl string
var appURL string
for i := 0; i < len(source.Assets); i++ {
var parts []string = strings.Split(source.Assets[i].BrowserDownloadUrl, "/")
var parts []string = strings.Split(source.Assets[i].BrowserDownloadURL, "/")
var fileName string = parts[len(parts)-1]

if strings.HasSuffix(fileName, "amd64.deb") {
var regVer string = strings.Replace(source.Name, ".", "\\.", 3)
match, _ := regexp.MatchString(app.Name+"[_-]{1}"+regVer+"[_-]{1}"+"amd64.deb", fileName)
if match {
appUrl = source.Assets[i].BrowserDownloadUrl
appURL = source.Assets[i].BrowserDownloadURL
break
}
}
}

if appUrl == "" {
fmt.Println("deb package not found - skip")
if appURL == "" {
fmt.Println(" " + app.Name + " skip -> deb package not found")
return "", false
}

var path, err = downloadFile(appUrl)
var path, err = downloadFile(appURL)

if err != nil {
fmt.Println("Error while downloading deb file - skip")
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"path"
)

const TEMP_FILES_DIR string = "/tmp/"
const tempFilesDir string = "/tmp/"

func downloadFile(url string) (filePath string, err error) {

var path string = TEMP_FILES_DIR + path.Base(url)
var path string = tempFilesDir + path.Base(url)
// Create the file
out, err := os.Create(path)
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions infrastructure/repo.go

This file was deleted.

6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func main() {
infrastructure.CreateInstalledAppListFile()
cmd.Execute()
if infrastructure.EnsureIsAdmin() == true {
infrastructure.CreateInstalledAppListFile()
cmd.Execute()
}
}

0 comments on commit 6e6f7de

Please sign in to comment.