-
-
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.
refactor: refactor DownloadGo function docs: update documentation chore: upgrade to go1.23
- Loading branch information
Showing
9 changed files
with
250 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ linters: | |
- gocyclo | ||
- ineffassign | ||
- misspell | ||
linters-settings: | ||
gofmt: | ||
simplify: true |
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 |
---|---|---|
@@ -1,97 +1,88 @@ | ||
/* | ||
Package AutomatedGo provides tools for automating Go version checks and downloads. | ||
**AutomatedGo Package** | ||
AutomatedGo provides tools for automated Go version management and updates. | ||
Features: | ||
- Detect current Go version from various file types (Dockerfile, go.mod, JSON configs, etc.) | ||
- Get the current Go version from a specified file or input | ||
- Check for the latest available Go version | ||
- Download the latest Go version if an update is available | ||
- Support for different operating systems and architectures | ||
- Compare Go versions to determine if an update is available | ||
- Download the latest Go version for different operating systems and architectures | ||
- Checksum validation for downloaded Go versions to ensure integrity | ||
Do's: | ||
1. Always check for errors returned by functions in this package. | ||
2. Use GetLatestVersion() to fetch the most recent Go version information. | ||
3. Specify the correct target OS and architecture when using DownloadGo(). | ||
4. Ensure you have necessary permissions to download and write files. | ||
5. Define Go version in your project files using one of these formats: | ||
- In go.mod: go 1.x | ||
- In Dockerfile: | ||
* FROM golang:1.x.x | ||
* ENV GO_VERSION=1.x.x | ||
- In other files: | ||
* go_version = "1.x.x" | ||
* GO_VERSION: 1.x.x | ||
* golang_version: "1.x.x" | ||
6. Use the package to automate version checks in your CI/CD pipelines. | ||
7. Verify checksums of downloaded Go versions for security. | ||
Usage Example: | ||
Don'ts: | ||
package main | ||
1. Don't assume the latest version is always compatible with your project. | ||
2. Avoid using this package to modify your system's Go installation directly. | ||
3. Don't use this package in production environments without thorough testing. | ||
4. Don't ignore version constraints specified in your go.mod file. | ||
5. Avoid manually modifying files downloaded by this package. | ||
6. Don't use non-standard formats for specifying Go versions in your project files. | ||
import ( | ||
Example usage: | ||
"fmt" | ||
"log" | ||
// Get the latest Go version | ||
latestVersion, err := pkg.GetLatestVersion() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("Latest Go version: %s\n", latestVersion) | ||
"github.com/Nicconike/AutomatedGo/v2/pkg" | ||
// Get current version from a file | ||
currentVersion, err := pkg.GetCurrentVersion("go.mod", "") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("Current Go version: %s\n", currentVersion) | ||
) | ||
// Check if update is needed | ||
if pkg.IsNewer(latestVersion, currentVersion) { | ||
fmt.Println("An update is available") | ||
func main() { | ||
// Create a new VersionService | ||
service := pkg.NewVersionService( | ||
&pkg.DefaultDownloader{}, | ||
&pkg.DefaultRemover{}, | ||
&pkg.DefaultChecksumCalculator{}, | ||
) | ||
// Download the latest Go version | ||
err = pkg.DownloadGo(latestVersion, "linux", "amd64") | ||
// Get the current Go version | ||
currentVersion, err := service.GetCurrentVersion("", "") | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Fatalf("Error getting current version: %v", err) | ||
} | ||
fmt.Println("Successfully downloaded new Go version") | ||
fmt.Printf("Current Go version: %s\n", currentVersion) | ||
// Verify checksum | ||
filename := fmt.Sprintf("go%s.linux-amd64.tar.gz", latestVersion) | ||
checksum, err := pkg.CalculateFileChecksum(filename) | ||
// Check for the latest Go version | ||
latestVersion, err := service.GetLatestVersion() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.Fatalf("Error getting latest version: %v", err) | ||
} | ||
fmt.Printf("Latest Go version: %s\n", latestVersion) | ||
// Check if an update is available | ||
if service.IsNewer(latestVersion, currentVersion) { | ||
fmt.Println("An update is available!") | ||
// Download the latest version | ||
err = service.DownloadGo(latestVersion, "", "", "/tmp", nil, nil) | ||
if err != nil { | ||
log.Fatalf("Error downloading Go: %v", err) | ||
} | ||
fmt.Printf("Successfully downloaded Go %s to /tmp\n", latestVersion) | ||
} else { | ||
fmt.Println("You have the latest version of Go.") | ||
} | ||
fmt.Printf("Checksum of downloaded file: %s\n", checksum) | ||
} else { | ||
fmt.Println("You have the latest version") | ||
} | ||
Functions: | ||
- GetLatestVersion() (string, error) | ||
Fetches the latest available Go version. | ||
This example demonstrates how to use the AutomatedGo package to check for updates, | ||
compare versions, and download the latest version of Go if an update is available. | ||
- GetCurrentVersion(filename, version string) (string, error) | ||
Detects the current Go version from a file or uses the provided version. | ||
Do's: | ||
- IsNewer(version1, version2 string) bool | ||
Compares two version strings and returns true if version1 is newer. | ||
1. Always check for errors returned by functions in this package. | ||
2. Use GetLatestVersion() to fetch the most recent Go version information. | ||
3. Use GetCurrentVersion() with appropriate parameters to determine the current version. | ||
4. Specify the correct target OS and architecture when using DownloadGo() if needed. | ||
5. Ensure you have necessary permissions to download and write files. | ||
6. Use IsNewer() to compare versions and determine if an update is needed. | ||
7. Use the package to automate version checks in your CI/CD pipelines. | ||
- DownloadGo(version, targetOS, arch string) error | ||
Downloads the specified Go version for the given OS and architecture. | ||
Don'ts: | ||
- CalculateFileChecksum(filename string) (string, error) | ||
Calculates the SHA256 checksum of the specified file. | ||
1. Don't assume the latest version is always compatible with your project. | ||
2. Avoid using this package to modify your system's Go installation directly. | ||
3. Don't use this package in production environments without thorough testing. | ||
4. Don't ignore version constraints specified in your go.mod file. | ||
5. Avoid manually modifying files downloaded by this package. | ||
For more detailed information and advanced usage, refer to the README.md file | ||
and the package documentation at https://pkg.go.dev/github.com/Nicconike/AutomatedGo/v2. | ||
Note: The package allows flexible version checking and downloading. You can provide | ||
specific version information or let the package determine versions automatically. | ||
*/ | ||
package AutomatedGo |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/Nicconike/AutomatedGo/v2 | ||
|
||
go 1.22 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/schollz/progressbar/v3 v3.16.1 | ||
|
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
Oops, something went wrong.