From 96ce3a18d5f6fb0d29b0ef3a8e6d562d1d0207c5 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Fri, 22 Jul 2022 21:26:07 +0700 Subject: [PATCH] feat: support url deps --- internal/cli/action_run.go | 49 +++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/internal/cli/action_run.go b/internal/cli/action_run.go index dbcfbed..da741e3 100644 --- a/internal/cli/action_run.go +++ b/internal/cli/action_run.go @@ -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)