diff --git a/go.mod b/go.mod index a97094d..e4a4a9e 100644 --- a/go.mod +++ b/go.mod @@ -3,25 +3,22 @@ module github.com/aykhans/dodo go 1.24 require ( + github.com/fatih/color v1.18.0 github.com/go-playground/validator/v10 v10.25.0 github.com/jedib0t/go-pretty/v6 v6.6.7 github.com/valyala/fasthttp v1.59.0 golang.org/x/net v0.37.0 ) -require ( - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect -) - require ( github.com/andybalholm/brotli v1.1.1 // indirect - github.com/fatih/color v1.18.0 github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/klauspost/compress v1.17.11 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/readers/json.go b/readers/json.go index fe65b7e..06ba65e 100644 --- a/readers/json.go +++ b/readers/json.go @@ -2,17 +2,46 @@ package readers import ( "encoding/json" + "fmt" + "net/http" "os" + "strings" + "time" + + "io" "github.com/aykhans/dodo/config" customerrors "github.com/aykhans/dodo/custom_errors" ) func JSONConfigReader(filePath string) (*config.JSONConfig, error) { - data, err := os.ReadFile(filePath) - if err != nil { - return nil, customerrors.OSErrorFormater(err) + var ( + data []byte + err error + ) + + if strings.HasPrefix(filePath, "http://") || strings.HasPrefix(filePath, "https://") { + client := &http.Client{ + Timeout: 10 * time.Second, + } + + resp, err := client.Get(filePath) + if err != nil { + return nil, fmt.Errorf("failed to fetch JSON config from %s", filePath) + } + defer resp.Body.Close() + + data, err = io.ReadAll(io.Reader(resp.Body)) + if err != nil { + return nil, fmt.Errorf("failed to read JSON config from %s", filePath) + } + } else { + data, err = os.ReadFile(filePath) + if err != nil { + return nil, customerrors.OSErrorFormater(err) + } } + jsonConf := config.NewJSONConfig( config.NewConfig("", 0, 0, 0, nil), nil, nil, nil, nil, nil, @@ -32,5 +61,6 @@ func JSONConfigReader(filePath string) (*config.JSONConfig, error) { } return nil, customerrors.NewInvalidFileError(filePath, err) } + return jsonConf, nil }