feat: support url deps

main
sudo pacman -Syu 2022-07-22 21:26:07 +07:00
parent 964453450d
commit 96ce3a18d5
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
1 changed files with 40 additions and 9 deletions

View File

@ -4,6 +4,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
@ -12,7 +15,10 @@ import (
"github.com/urfave/cli/v2"
)
var ErrInvalidModuleVersion = errors.New("invalid module version")
var (
ErrInvalidModuleVersion = errors.New("invalid module version")
ErrFailedStatusCode = errors.New("failed status code")
)
// See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules
type Module struct {
@ -49,18 +55,43 @@ func (a *action) Run(c *cli.Context) error {
}
a.log("imported modules: %+v\n", importedModules)
// Read pkg from file
depsFileBytes, err := os.ReadFile(a.flags.depsFile)
if err != nil {
if os.IsNotExist(err) {
color.PrintAppWarning(name, fmt.Sprintf("deps file [%s] not found", a.flags.depsFile))
return nil
// Try to parse url first
var depsFileStr string
depsFileURL, err := url.Parse(a.flags.depsFile)
if err == nil {
httpRsp, err := http.Get(depsFileURL.String())
if err != nil {
return fmt.Errorf("failed to http get %s: %w", depsFileURL.String(), err)
}
defer httpRsp.Body.Close()
if httpRsp.StatusCode != http.StatusOK {
return fmt.Errorf("http status code not ok %d: %w", httpRsp.StatusCode, ErrFailedStatusCode)
}
return fmt.Errorf("failed to read file %s: %w", a.flags.depsFile, err)
depsFileBytes, err := io.ReadAll(httpRsp.Body)
if err != nil {
return fmt.Errorf("failed to read http response body: %w", err)
}
depsFileStr = string(depsFileBytes)
} else {
a.log("url parse error: %s", err)
// If not url, try to read local file
depsFileBytes, err := os.ReadFile(a.flags.depsFile)
if err != nil {
if os.IsNotExist(err) {
color.PrintAppWarning(name, fmt.Sprintf("deps file [%s] not found", a.flags.depsFile))
return nil
}
return fmt.Errorf("failed to read file %s: %w", a.flags.depsFile, err)
}
depsFileStr = strings.TrimSpace(string(depsFileBytes))
}
depsFileStr := strings.TrimSpace(string(depsFileBytes))
lines := strings.Split(depsFileStr, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)