diff --git a/cmd/list.go b/cmd/list.go index 69ab852..0ccb75f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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 + ")") } }, } diff --git a/cmd/update.go b/cmd/update.go index c7bd4e0..8008fc3 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -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) diff --git a/infrastructure/apps.go b/infrastructure/apps.go index 405cd38..4b044b4 100644 --- a/infrastructure/apps.go +++ b/infrastructure/apps.go @@ -6,21 +6,25 @@ 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 @@ -28,7 +32,7 @@ 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) diff --git a/infrastructure/dpkgClient.go b/infrastructure/dpkgClient.go index 71a8e01..aea742e 100644 --- a/infrastructure/dpkgClient.go +++ b/infrastructure/dpkgClient.go @@ -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) diff --git a/infrastructure/githubClient.go b/infrastructure/githubClient.go index e431407..5c116f1 100644 --- a/infrastructure/githubClient.go +++ b/infrastructure/githubClient.go @@ -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) @@ -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") diff --git a/infrastructure/httpClient.go b/infrastructure/httpClient.go index e6bf6ff..6337c56 100644 --- a/infrastructure/httpClient.go +++ b/infrastructure/httpClient.go @@ -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 { diff --git a/infrastructure/repo.go b/infrastructure/repo.go deleted file mode 100644 index bfe7427..0000000 --- a/infrastructure/repo.go +++ /dev/null @@ -1,18 +0,0 @@ -package infrastructure - -import ( - "encoding/json" - - "github.com/afelinczak/app-get/domain" -) - -const REPO_PATH string = "repo.json" -const APPS_PATH string = "apps.json" - -// GetRepo load list of available apps from repo.json -func GetRepo() []domain.App { - var result = readFromFile(REPO_PATH) - var apps []domain.App - json.Unmarshal([]byte(result), &apps) - return apps -} diff --git a/main.go b/main.go index 1c38672..69d0f75 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( ) func main() { - infrastructure.CreateInstalledAppListFile() - cmd.Execute() + if infrastructure.EnsureIsAdmin() == true { + infrastructure.CreateInstalledAppListFile() + cmd.Execute() + } }