func install()

in dev-tools/mage/linter.go [69:120]


func install(force bool) error {
	dirPath := filepath.Dir(linterBinaryFile)
	err := os.MkdirAll(dirPath, 0700)
	if err != nil {
		return fmt.Errorf("failed to create path %q: %w", dirPath, err)
	}

	_, err = os.Stat(linterBinaryFile)
	if !force && err == nil {
		log.Println("The linter has been already installed, skipping...")
		return nil
	}
	if err != nil && !errors.Is(err, os.ErrNotExist) {
		return fmt.Errorf("failed check if file %q exists: %w", linterBinaryFile, err)
	}

	log.Println("Preparing the installation script file...")

	installScript, err := os.OpenFile(linterInstallFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0700)
	if err != nil {
		return fmt.Errorf("failed to create file %q: %w", linterInstallFile, err)
	}
	defer installScript.Close()

	log.Println("Downloading the linter installation script...")
	//nolint:noctx // valid use since there is no context
	resp, err := http.Get(linterInstallURL)
	if err != nil {
		return fmt.Errorf("cannot download the linter installation script from %q: %w", linterInstallURL, err)
	}
	defer resp.Body.Close()

	lr := io.LimitReader(resp.Body, 1024*100) // not more than 100 KB, just to be safe
	_, err = io.Copy(installScript, lr)
	if err != nil {
		return fmt.Errorf("failed to finish downloading the linter installation script: %w", err)
	}

	err = installScript.Close() // otherwise we cannot run the script
	if err != nil {
		return fmt.Errorf("failed to close file %q: %w", linterInstallFile, err)
	}

	binaryDir := filepath.Dir(linterBinaryFile)
	err = os.MkdirAll(binaryDir, 0700)
	if err != nil {
		return fmt.Errorf("cannot create path %q: %w", binaryDir, err)
	}

	// there must be no space after `-b`, otherwise the script does not work correctly ¯\_(ツ)_/¯
	return sh.Run(linterInstallFile, "-b"+binaryDir, linterVersion)
}