-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc596b0
commit fbb19c5
Showing
4 changed files
with
125 additions
and
80 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
|
||
"github.com/pterm/pterm" | ||
"github.com/spf13/cobra" | ||
"github.com/uniget-org/cli/pkg/archive" | ||
"github.com/uniget-org/cli/pkg/logging" | ||
) | ||
|
||
func initSelfUpgradeCmd() { | ||
rootCmd.AddCommand(selfUpgradeCmd) | ||
} | ||
|
||
var selfUpgradeCmd = &cobra.Command{ | ||
Use: "self-upgrade", | ||
Short: "Self upgrade " + projectName, | ||
Long: header + "\nUpgrade " + projectName + " to latest version", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
versionRegex := regexp.MustCompile(`^\d+\.\d+\.\d+(-\w+)?$`) | ||
if !versionRegex.MatchString(version) { | ||
pterm.Warning.Printf("Version is %s and does not match a.b.c\n", version) | ||
return nil | ||
} | ||
|
||
selfExe := filepath.Base(os.Args[0]) | ||
if selfExe == "." { | ||
return fmt.Errorf("failed to get base name for %s", os.Args[0]) | ||
} | ||
if selfExe != "uniget" { | ||
pterm.Warning.Printf("Binary must be called uniget but is %s\n", selfExe) | ||
return nil | ||
} | ||
|
||
path, err := exec.LookPath(selfExe) | ||
if err != nil { | ||
pterm.Error.Printfln("Failed to find %s in PATH", selfExe) | ||
return fmt.Errorf("failed to find %s in PATH: %s", selfExe, err) | ||
} | ||
fmt.Printf("%s is available at %s\n", selfExe, path) | ||
selfDir := filepath.Dir(path) | ||
|
||
url := fmt.Sprintf("https://github.com/%s/releases/latest/download/uniget_%s_%s.tar.gz", projectRepository, runtime.GOOS, arch) | ||
logging.Debug.Printfln("Downloading %s", url) | ||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %s", err) | ||
} | ||
req.Header.Set("Accept", "application/octet-stream") | ||
req.Header.Set("User-Agent", fmt.Sprintf("%s/%s", projectName, version)) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to download %s: %s", url, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return fmt.Errorf("failed to download %s: %s", url, resp.Status) | ||
} | ||
|
||
logging.Debug.Printfln("Extracting tar.gz") | ||
err = os.Chdir(selfDir) | ||
if err != nil { | ||
return fmt.Errorf("error changing directory to %s: %s", selfDir, err) | ||
} | ||
err = os.Remove(selfExe) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove %s: %s", selfExe, err) | ||
} | ||
err = archive.ExtractTarGz(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to extract tar.gz: %s", err) | ||
} | ||
|
||
return 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