feat: support url deps
parent
964453450d
commit
96ce3a18d5
|
@ -4,6 +4,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,7 +15,10 @@ import (
|
||||||
"github.com/urfave/cli/v2"
|
"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
|
// See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules
|
||||||
type Module struct {
|
type Module struct {
|
||||||
|
@ -49,18 +55,43 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
a.log("imported modules: %+v\n", importedModules)
|
a.log("imported modules: %+v\n", importedModules)
|
||||||
|
|
||||||
// Read pkg from file
|
// Try to parse url first
|
||||||
depsFileBytes, err := os.ReadFile(a.flags.depsFile)
|
var depsFileStr string
|
||||||
if err != nil {
|
depsFileURL, err := url.Parse(a.flags.depsFile)
|
||||||
if os.IsNotExist(err) {
|
if err == nil {
|
||||||
color.PrintAppWarning(name, fmt.Sprintf("deps file [%s] not found", a.flags.depsFile))
|
httpRsp, err := http.Get(depsFileURL.String())
|
||||||
return nil
|
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")
|
lines := strings.Split(depsFileStr, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
|
|
Loading…
Reference in New Issue