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. **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

@ -17,9 +17,14 @@ import (
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
) )
var f = false
func main() { func main() {
installFlag := flag.Bool("install", false, "Install/Update Go (Linux only)") downloadFlag := flag.Bool("download", false, "Download latest Go")
versionFlag := flag.Bool("version", false, "Only print version") installFlag := &f
if runtime.GOOS == "linux" {
installFlag = flag.Bool("install", false, "Install/Update Go")
}
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 +34,47 @@ func main() {
return return
} }
if *versionFlag { if !*downloadFlag && !*installFlag {
fmt.Println(latest.Version) ver, err := goVersion()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Current: %sLatest: %s\n", ver, latest.Version)
return return
} }
outPath, err := download(latest.Filename, *outFlag) outPath := filepath.Join(*outFlag, latest.Filename)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(outPath)
if !*installFlag { if *downloadFlag {
return if err := download(latest.Filename, outPath); err != nil {
fmt.Println(err)
return
}
fmt.Println(outPath)
} }
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 {
return fmt.Println(err)
} return
}
if os.Geteuid() != 0 { ver, err := goVersion()
fmt.Println(`This command must be run as root to perform the install. if err != nil {
Alternatively, you can run the following command manually, which is taken from https://golang.org/doc/install#install fmt.Println(err)
return
rm -rf /usr/local/go && tar -C /usr/local -xzf ` + outPath) }
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 +100,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 +125,36 @@ 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 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
} }