Skip to content
Snippets Groups Projects
update.go 1.56 KiB
Newer Older
  • Learn to ignore specific revisions
  • azubieta's avatar
    azubieta committed
    package commands
    
    import (
    	"fmt"
    	"os"
    	"path/filepath"
    
    azubieta's avatar
    azubieta committed
    
    
    azubieta's avatar
    azubieta committed
    	"appimage-manager/app/utils"
    
    azubieta's avatar
    azubieta committed
    
    	"github.com/AppImageCrafters/appimage-update"
    )
    
    type UpdateCmd struct {
    
    	Target string `arg name:"target" help:"Updates the target application." type:"string"`
    
    azubieta's avatar
    azubieta committed
    }
    
    func (cmd *UpdateCmd) Run(*Context) (err error) {
    
    	filePath, err := cmd.getBundleFilePath()
    
    azubieta's avatar
    azubieta committed
    	if err != nil {
    		return err
    	}
    
    	updateMethod, err := update.NewUpdaterFor(filePath)
    	if err != nil {
    		return err
    	}
    
    	fmt.Println("Looking for updates of: ", filePath)
    	updateAvailable, err := updateMethod.Lookup()
    	if err != nil {
    		fmt.Println(err.Error())
    		return
    	}
    
    	if !updateAvailable {
    		fmt.Println("No updates were found for: ", filePath)
    		return
    	}
    
    	result, err := updateMethod.Download()
    	if err != nil {
    		fmt.Println("Error: ", err.Error())
    		return
    	}
    
    	fmt.Println("Update downloaded to: " + result)
    
    	return nil
    }
    
    
    func (cmd *UpdateCmd) getBundleFilePath() (string, error) {
    	if strings.HasPrefix(cmd.Target, "file://") {
    		cmd.Target = cmd.Target[7:]
    	}
    
    	if _, err := os.Stat(cmd.Target); err == nil {
    		return cmd.Target, nil
    	}
    
    
    azubieta's avatar
    azubieta committed
    	registry, err := utils.OpenRegistry()
    	if err != nil {
    		return "", err
    	}
    	registry.Update()
    
    
    	fileName, ok := registry.Lookup(cmd.Target)
    
    azubieta's avatar
    azubieta committed
    	if !ok {
    
    		fileName = cmd.Target
    
    azubieta's avatar
    azubieta committed
    	}
    
    	applicationsDir, err := utils.MakeApplicationsDirPath()
    	if err != nil {
    		return "", err
    	}
    	filePath := filepath.Join(applicationsDir, fileName)
    
    	if _, err := os.Stat(filePath); os.IsNotExist(err) {
    
    		return "", fmt.Errorf("application not found \"" + cmd.Target + "\"")
    
    azubieta's avatar
    azubieta committed
    	}
    	return filePath, nil
    }