Better binary defaults
continuous-integration/drone/pr Build is passing Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/2/head
jolheiser 2021-08-08 23:04:59 -05:00
parent b561b7c7ca
commit 6ed733d794
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
2 changed files with 73 additions and 53 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. **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 ## License
[MIT](LICENSE) [MIT](LICENSE)

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -18,8 +19,8 @@ import (
) )
func main() { func main() {
downloadFlag := flag.Bool("download", false, "Download latest Go")
installFlag := flag.Bool("install", false, "Install/Update Go (Linux only)") installFlag := flag.Bool("install", false, "Install/Update Go (Linux only)")
versionFlag := flag.Bool("version", false, "Only print version")
outFlag := flag.String("out", ".", "Where to put the downloaded file") outFlag := flag.String("out", ".", "Where to put the downloaded file")
flag.Parse() flag.Parse()
@ -29,63 +30,47 @@ func main() {
return return
} }
if *versionFlag { if !*downloadFlag && !*installFlag {
fmt.Println(latest.Version) ver, err := goVersion()
return
}
outPath, err := download(latest.Filename, *outFlag)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Printf("Current: %sLatest: %s\n", ver, latest.Version)
return
}
outPath := filepath.Join(*outFlag, latest.Filename)
if *downloadFlag {
if err := download(latest.Filename, outPath); err != nil {
fmt.Println(err)
return
}
fmt.Println(outPath) fmt.Println(outPath)
if !*installFlag {
return
} }
if !strings.EqualFold(runtime.GOOS, "linux") { if *installFlag {
fmt.Println("Install option is only for Linux. Mac and Windows users should run the downloaded installer.") if err := install(outPath); err != nil {
fmt.Println(err)
return return
} }
ver, err := goVersion()
if os.Geteuid() != 0 { if err != nil {
fmt.Println(`This command must be run as root to perform the install. fmt.Println(err)
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 return
} }
fmt.Printf("Successfully installed: %s\n", ver)
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
} }
func goVersion() (string, error) {
goVersion := exec.Command("/usr/local/go/bin/go", "version") goVersion := exec.Command("/usr/local/go/bin/go", "version")
out, err := goVersion.Output() out, err := goVersion.Output()
if err != nil { if err != nil {
fmt.Printf("could not check go version: %v\n", err) return "", fmt.Errorf("could not check go version: %v", err)
return
} }
return string(out), nil
fmt.Printf("Successfully installed: %s\n", string(out))
} }
func latestVersion() (*dl.File, error) { func latestVersion() (*dl.File, error) {
@ -111,23 +96,22 @@ func latestVersion() (*dl.File, error) {
return &version, nil return &version, nil
} }
func download(filename, out string) (string, error) { func download(filename, outPath string) error {
outPath := filepath.Join(out, filename)
outFile, err := os.Create(outPath) outFile, err := os.Create(outPath)
if err != nil { 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() defer outFile.Close()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://golang.org/dl/%s", filename), nil) req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://golang.org/dl/%s", filename), nil)
if err != 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") req.Header.Set("User-Agent", "git.jojodev.com/golang/dl/cmd/godl")
res, err := http.DefaultClient.Do(req) res, err := http.DefaultClient.Do(req)
if err != nil { 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() defer res.Body.Close()
@ -137,8 +121,40 @@ func download(filename, out string) (string, error) {
) )
if _, err := io.Copy(io.MultiWriter(outFile, bar), res.Body); err != nil { 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 !strings.EqualFold(runtime.GOOS, "linux") {
return errors.New("Install option is only for Linux. Mac and Windows users should run the downloaded installer.")
}
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
} }