Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/elasticpath/epcc-cli/config"
"github.com/elasticpath/epcc-cli/external/logger"
"github.com/elasticpath/epcc-cli/globals"
log "github.com/sirupsen/logrus"
"github.com/thediveo/enumflag"
"os"
Expand Down Expand Up @@ -39,7 +40,7 @@ func init() {
"log",
"sets logging level; can be 'trace', 'debug', 'info', 'warn', 'error', 'fatal', 'panic'")
rootCmd.PersistentFlags().BoolVarP(&json.MonochromeOutput, "monochrome-output", "M", false, "By default, epcc will output using colors if the terminal supports this. Use this option to disable it.")

rootCmd.PersistentFlags().StringSliceVarP(&globals.RawHeaders, "header", "H", []string{}, "Extra headers and values to include in the request when sending HTTP to a server. You may specify any number of extra headers.")
}

var rootCmd = &cobra.Command{
Expand Down
18 changes: 18 additions & 0 deletions external/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"github.com/elasticpath/epcc-cli/config"
"github.com/elasticpath/epcc-cli/external/authentication"
"github.com/elasticpath/epcc-cli/external/version"
"github.com/elasticpath/epcc-cli/globals"
log "github.com/sirupsen/logrus"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -54,6 +56,10 @@ func doRequestInternal(ctx context.Context, method string, contentType string, p

req.Header.Add("User-Agent", fmt.Sprintf("epcc-cli/%s-%s", version.Version, version.Commit))

if err = AddHeaderByFlag(req); err != nil {
return nil, err
}

if len(config.Envs.EPCC_BETA_API_FEATURES) > 0 {
req.Header.Add("EP-Beta-Features", config.Envs.EPCC_BETA_API_FEATURES)
}
Expand Down Expand Up @@ -96,3 +102,15 @@ func EncodeForm(values map[string]string, filename string, paramName string, fil

return body, writer.FormDataContentType(), nil
}

func AddHeaderByFlag(r *http.Request) error {
for _, header := range globals.RawHeaders {
// Validation and formatting logic for headers could be improved
entries := strings.Split(header, ":")
if len(entries) < 2 {
return fmt.Errorf("header has invalid format")
}
r.Header.Add(entries[0], entries[1])
}
return nil
}
3 changes: 3 additions & 0 deletions globals/globals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package globals

var RawHeaders []string