-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version and version check command (#304)
* add current version * add version check command
- Loading branch information
1 parent
2d94d87
commit f34e92e
Showing
5 changed files
with
80 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"github.com/blang/semver" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func check() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "check", | ||
Short: "Check for a patch for the CLI", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client := http.Client{Timeout: time.Minute * 3} | ||
reqUrl := fmt.Sprintf("https://raw.githubusercontent.com/arlonproj/arlon/v%s/version", strings.Join(strings.Split(cliVersion, ".")[:2], ".")) | ||
req, err := http.NewRequest(http.MethodGet, reqUrl, nil) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer func(Body io.ReadCloser) { | ||
_ = Body.Close() | ||
}(res.Body) | ||
version, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return err | ||
} | ||
latestVersion := strings.TrimSpace(string(version)) | ||
ver, err := semver.Parse(latestVersion) | ||
if err != nil { | ||
return err | ||
} | ||
currentVersion, err := semver.Parse(cliVersion) | ||
if err != nil { | ||
return err | ||
} | ||
if ver.Compare(currentVersion) == 1 { | ||
fmt.Printf("Arlon CLI version %s is outdated. New patch %s available\n", cliVersion, ver.String()) | ||
return nil | ||
} | ||
fmt.Printf("Arlon CLI version %s up-to-date\n", cliVersion) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cliVersion string | ||
|
||
func NewCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "check for arlon CLI version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("Arlon CLI Version: %s\n", cliVersion) | ||
}, | ||
} | ||
cmd.AddCommand(check()) | ||
return cmd | ||
} |
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