Skip to content

Commit

Permalink
fix: fix update detection
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed May 30, 2021
1 parent 44a2d23 commit d8f67f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
26 changes: 25 additions & 1 deletion pcli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pcli

import (
"errors"
"io/ioutil"
"net/http"
"runtime"
Expand All @@ -23,6 +24,13 @@ func SetRootCmd(cmd *cobra.Command) {
rootCmd = cmd
}

type meta struct {
Username string
Reponame string
}

var AppInfo = meta{}

// Setup replaces some cobra functions with pcli functions for nicer output.
func Setup() {
rootCmd.AddCommand(GetCiCommand())
Expand All @@ -37,9 +45,25 @@ func Setup() {
rootCmd.SetErr(Err())
}

// SetRepo must be set to your repository path (eg. pterm/cli-template).
func SetRepo(repo string) error {
parts := strings.Split(repo, "/")
if len(parts) != 2 {
return errors.New("repo must be set in this pattern: username/reponame, eg.: pterm/cli-template")
}
AppInfo.Username = parts[0]
AppInfo.Reponame = parts[1]

return nil
}

func getRepoPath() string {
return AppInfo.Username + "/" + AppInfo.Reponame
}

// CheckForUpdates checks if a new version of your application is pushed, and notifies the user, if DisableUpdateChecking is true.
func CheckForUpdates() error {
if !DisableUpdateChecking {
if !DisableUpdateChecking && AppInfo.Username != "" && AppInfo.Reponame != "" {
resp, err := http.Get(pterm.Sprintf("https://api.github.com/repos/%s/releases/latest", getRepoPath()))
if err != nil {
return err
Expand Down
13 changes: 1 addition & 12 deletions pterm-ci-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func detectOriginURL() (url string) {
output := string(out)

for _, s := range strings.Split(output, "\n") {
s = strings.TrimSpace(strings.TrimLeft(s, "origin"))
s = strings.TrimSpace(strings.TrimPrefix(s, "origin"))
if strings.HasPrefix(s, "https://github.com/") && strings.Contains(s, "push") {
pterm.Debug.Printfln("Detected GitHub Repo: %s", s)
url = strings.TrimSpace(strings.TrimRight(s, "(push)"))
Expand All @@ -119,17 +119,6 @@ func detectOriginURL() (url string) {
return
}

func getRepo() (username, reponame string) {
projectParts := strings.Split(strings.TrimPrefix(detectOriginURL(), "https://github.com/"), "/")

return projectParts[0], projectParts[1]
}

func getRepoPath() string {
username, reponame := getRepo()
return pterm.Sprintf("%s/%s", username, reponame)
}

func walkOverExt(path, exts string, f func(path string)) {
_ = filepath.Walk(getPathTo(path), func(path string, info fs.FileInfo, err error) error {
for _, ext := range strings.Split(exts, ",") {
Expand Down

0 comments on commit d8f67f9

Please sign in to comment.