Better binary defaults (#2)
continuous-integration/drone/push Build is passing Details

Reviewed-on: #2
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
main latest
jolheiser 2021-08-09 04:10:25 +00:00
parent b561b7c7ca
commit 67ed3dd681
No known key found for this signature in database
GPG Key ID: 454E7F878890995A
2 changed files with 74 additions and 54 deletions

View File

@ -4,6 +4,10 @@
**NOTE:** This URL may not remain permanent, it is simply usable at the time of writing this library.
## Installation
`go install git.jojodev.com/golang/dl/cmd/godl@main`
## License
[MIT](LICENSE)

View File

@ -17,9 +17,14 @@ import (
"github.com/schollz/progressbar/v3"
)
var f = false
func main() {
installFlag := flag.Bool("install", false, "Install/Update Go (Linux only)")
versionFlag := flag.Bool("version", false, "Only print version")
downloadFlag := flag.Bool("download", false, "Download latest Go")
installFlag := &f
if runtime.GOOS == "linux" {
installFlag = flag.Bool("install", false, "Install/Update Go")
}
outFlag := flag.String("out", ".", "Where to put the downloaded file")
flag.Parse()
@ -29,63 +34,47 @@ func main() {
return
}
if *versionFlag {
fmt.Println(latest.Version)
if !*downloadFlag && !*installFlag {
ver, err := goVersion()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Current: %sLatest: %s\n", ver, latest.Version)
return
}
outPath, err := download(latest.Filename, *outFlag)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(outPath)
outPath := filepath.Join(*outFlag, latest.Filename)
if !*installFlag {
return
if *downloadFlag {
if err := download(latest.Filename, outPath); err != nil {
fmt.Println(err)
return
}
fmt.Println(outPath)
}
if !strings.EqualFold(runtime.GOOS, "linux") {
fmt.Println("Install option is only for Linux. Mac and Windows users should run the downloaded installer.")
return
}
if os.Geteuid() != 0 {
fmt.Println(`This command must be run as root to perform the install.
Alternatively, you can run the following command manually, which is taken from https://golang.org/doc/install#install
rm -rf /usr/local/go && tar -C /usr/local -xzf ` + outPath)
return
}
fmt.Println("removing local installation")
rm := exec.Command("rm", "-rf", "/usr/local/go")
if err := rm.Run(); err != nil {
fmt.Printf("could not remove local go installation: %v\n", err)
return
}
fmt.Println("extracting new installation")
tar := exec.Command("tar", "-C", "/usr/local", "-xzf", outPath)
if err := tar.Run(); err != nil {
fmt.Printf("could not extract new installation: %v\n", err)
return
}
rmDl := exec.Command("rm", outPath)
if err := rmDl.Run(); err != nil {
fmt.Printf("could not remove downloaded file: %v\n", err)
return
if *installFlag {
if err := install(outPath); err != nil {
fmt.Println(err)
return
}
ver, err := goVersion()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Successfully installed: %s\n", ver)
}
}
func goVersion() (string, error) {
goVersion := exec.Command("/usr/local/go/bin/go", "version")
out, err := goVersion.Output()
if err != nil {
fmt.Printf("could not check go version: %v\n", err)
return
return "", fmt.Errorf("could not check go version: %v", err)
}
fmt.Printf("Successfully installed: %s\n", string(out))
return string(out), nil
}
func latestVersion() (*dl.File, error) {
@ -111,23 +100,22 @@ func latestVersion() (*dl.File, error) {
return &version, nil
}
func download(filename, out string) (string, error) {
outPath := filepath.Join(out, filename)
func download(filename, outPath string) error {
outFile, err := os.Create(outPath)
if err != nil {
return outPath, fmt.Errorf("could not create file for download: %v", err)
return fmt.Errorf("could not create file for download: %v", err)
}
defer outFile.Close()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://golang.org/dl/%s", filename), nil)
if err != nil {
return outPath, fmt.Errorf("could not create request: %v", err)
return fmt.Errorf("could not create request: %v", err)
}
req.Header.Set("User-Agent", "git.jojodev.com/golang/dl/cmd/godl")
res, err := http.DefaultClient.Do(req)
if err != nil {
return outPath, fmt.Errorf("could not download file: %v", err)
return fmt.Errorf("could not download file: %v", err)
}
defer res.Body.Close()
@ -137,8 +125,36 @@ func download(filename, out string) (string, error) {
)
if _, err := io.Copy(io.MultiWriter(outFile, bar), res.Body); err != nil {
return outPath, err
return err
}
return outPath, nil
return nil
}
func install(outPath string) error {
if os.Geteuid() != 0 {
return fmt.Errorf(`This command must be run as root to perform the install.
Alternatively, you can run the following command manually, which is taken from https://golang.org/doc/install#install
rm -rf /usr/local/go && tar -C /usr/local -xzf ` + outPath)
}
fmt.Println("removing local installation")
rm := exec.Command("rm", "-rf", "/usr/local/go")
if err := rm.Run(); err != nil {
return fmt.Errorf("could not remove local go installation: %v", err)
}
fmt.Println("extracting new installation")
tar := exec.Command("tar", "-C", "/usr/local", "-xzf", outPath)
if err := tar.Run(); err != nil {
return fmt.Errorf("could not extract new installation: %v", err)
}
rmDl := exec.Command("rm", outPath)
if err := rmDl.Run(); err != nil {
return fmt.Errorf("could not remove downloaded file: %v", err)
}
return nil
}