mirror of
https://github.com/aykhans/dodo.git
synced 2025-07-02 00:16:20 +00:00
✨ Add yaml file reader to config
This commit is contained in:
@ -27,17 +27,17 @@ const (
|
||||
var SupportedProxySchemes []string = []string{"http", "socks5", "socks5h"}
|
||||
|
||||
type RequestConfig struct {
|
||||
Method string `json:"method"`
|
||||
URL url.URL `json:"url"`
|
||||
Timeout time.Duration `json:"timeout"`
|
||||
DodosCount uint `json:"dodos"`
|
||||
RequestCount uint `json:"requests"`
|
||||
Yes bool `json:"yes"`
|
||||
Params types.Params `json:"params"`
|
||||
Headers types.Headers `json:"headers"`
|
||||
Cookies types.Cookies `json:"cookies"`
|
||||
Body types.Body `json:"body"`
|
||||
Proxies types.Proxies `json:"proxies"`
|
||||
Method string
|
||||
URL url.URL
|
||||
Timeout time.Duration
|
||||
DodosCount uint
|
||||
RequestCount uint
|
||||
Yes bool
|
||||
Params types.Params
|
||||
Headers types.Headers
|
||||
Cookies types.Cookies
|
||||
Body types.Body
|
||||
Proxies types.Proxies
|
||||
}
|
||||
|
||||
func NewRequestConfig(conf *Config) *RequestConfig {
|
||||
@ -111,17 +111,17 @@ func (rc *RequestConfig) Print() {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Method *string `json:"method"`
|
||||
URL *types.RequestURL `json:"url"`
|
||||
Timeout *types.Timeout `json:"timeout"`
|
||||
DodosCount *uint `json:"dodos"`
|
||||
RequestCount *uint `json:"requests"`
|
||||
Yes *bool `json:"yes"`
|
||||
Params types.Params `json:"params"`
|
||||
Headers types.Headers `json:"headers"`
|
||||
Cookies types.Cookies `json:"cookies"`
|
||||
Body types.Body `json:"body"`
|
||||
Proxies types.Proxies `json:"proxy"`
|
||||
Method *string `json:"method" yaml:"method"`
|
||||
URL *types.RequestURL `json:"url" yaml:"url"`
|
||||
Timeout *types.Timeout `json:"timeout" yaml:"timeout"`
|
||||
DodosCount *uint `json:"dodos" yaml:"dodos"`
|
||||
RequestCount *uint `json:"requests" yaml:"requests"`
|
||||
Yes *bool `json:"yes" yaml:"yes"`
|
||||
Params types.Params `json:"params" yaml:"params"`
|
||||
Headers types.Headers `json:"headers" yaml:"headers"`
|
||||
Cookies types.Cookies `json:"cookies" yaml:"cookies"`
|
||||
Body types.Body `json:"body" yaml:"body"`
|
||||
Proxies types.Proxies `json:"proxy" yaml:"proxy"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
|
@ -7,40 +7,54 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aykhans/dodo/types"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var supportedFileTypes = []string{"json", "yaml", "yml"}
|
||||
|
||||
func (config *Config) ReadFile(filePath types.ConfigFile) error {
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if filePath.LocationType() == types.FileLocationTypeRemoteHTTP {
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
fileExt := filePath.Extension()
|
||||
if slices.Contains(supportedFileTypes, fileExt) {
|
||||
if filePath.LocationType() == types.FileLocationTypeRemoteHTTP {
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := client.Get(filePath.String())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch config file from %s", filePath)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err = io.ReadAll(io.Reader(resp.Body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read config file from %s", filePath)
|
||||
}
|
||||
} else {
|
||||
data, err = os.ReadFile(filePath.String())
|
||||
if err != nil {
|
||||
return errors.New("failed to read config file from " + filePath.String())
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := client.Get(filePath.String())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch config file from %s", filePath)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err = io.ReadAll(io.Reader(resp.Body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read config file from %s", filePath)
|
||||
}
|
||||
} else {
|
||||
data, err = os.ReadFile(filePath.String())
|
||||
if err != nil {
|
||||
return errors.New("failed to read config file from " + filePath.String())
|
||||
if fileExt == "json" {
|
||||
return parseJSONConfig(data, config)
|
||||
} else if fileExt == "yml" || fileExt == "yaml" {
|
||||
return parseYAMLConfig(data, config)
|
||||
}
|
||||
}
|
||||
|
||||
return parseJSONConfig(data, config)
|
||||
return fmt.Errorf("unsupported config file type (supported types: %v)", strings.Join(supportedFileTypes, ", "))
|
||||
}
|
||||
|
||||
func parseJSONConfig(data []byte, config *Config) error {
|
||||
@ -58,3 +72,12 @@ func parseJSONConfig(data []byte, config *Config) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseYAMLConfig(data []byte, config *Config) error {
|
||||
err := yaml.Unmarshal(data, &config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("YAML Config file: %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user