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
16 changes: 8 additions & 8 deletions cmd/commercemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"net/url"
)

var ApiHostToUrlMap = map[string]string{
"api.moltin.com": "https://euwest.cm.elasticpath.com/",
"useast.api.elasticpath.com": "https://useast.cm.elasticpath.com/",
}
var cmCommand = &cobra.Command{
Use: "commerce-manager",
Short: "Open commerce manager",
Expand All @@ -18,17 +22,13 @@ var cmCommand = &cobra.Command{
fmt.Println(err)
return err
}
var cmUrl string
switch u.Host {
case "api.moltin.com":
cmUrl = "https://euwest.cm.elasticpath.com/"
case "useast.api.elasticpath.com":
cmUrl = "https://useast.cm.elasticpath.com/"
}

if cmUrl == "" {
cmUrl, ok := ApiHostToUrlMap[u.Host]

if !ok {
return fmt.Errorf("Don't know where Commerce Manager is for $EPCC_API_BASE_URL=%s \n", u)
}

err = shared.OpenUrl(cmUrl)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"strings"
)

var logsClear = &cobra.Command{
var LogsClear = &cobra.Command{
Use: "clear",
Short: "Clears all Http logs",
Short: "Clears all HTTP request and response logs",
RunE: func(cmd *cobra.Command, args []string) error {
os.RemoveAll(shared.LogDirectory)
return nil
},
}

var logsList = &cobra.Command{
var LogsList = &cobra.Command{
Use: "list",
Short: "List All Http logs",
Short: "List all HTTP logs",
RunE: func(cmd *cobra.Command, args []string) error {
files := shared.AllFilesSortedByDate(shared.LogDirectory)
for i := 0; i < len(files); i++ {
Expand All @@ -30,9 +30,9 @@ var logsList = &cobra.Command{
},
}

var logsShow = &cobra.Command{
var LogsShow = &cobra.Command{
Use: "show <NUMBER>",
Short: "Show Http logs for specific number",
Short: "Show HTTP logs for specific number",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
files := shared.AllFilesSortedByDate(shared.LogDirectory)
Expand All @@ -53,4 +53,4 @@ var logsShow = &cobra.Command{
},
}

var logs = &cobra.Command{Use: "logs"}
var Logs = &cobra.Command{Use: "logs"}
19 changes: 11 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"fmt"
"github.com/elasticpath/epcc-cli/config"
"github.com/elasticpath/epcc-cli/external/logger"
"github.com/elasticpath/epcc-cli/external/version"
"github.com/elasticpath/epcc-cli/globals"
log "github.com/sirupsen/logrus"
"github.com/thediveo/enumflag"
Expand All @@ -22,36 +24,36 @@ func init() {
panic("Could not parse environment variables")
}

rootCmd.AddCommand(
RootCmd.AddCommand(
cmCommand,
docsCommand,
testJson,
get,
create,
delete,
update,
logs,
Logs,
resourceListCommand,
aliasesCmd,
login,
logout,
)
logs.AddCommand(logsList, logsShow, logsClear)
Logs.AddCommand(LogsList, LogsShow, LogsClear)

testJson.Flags().BoolVarP(&noWrapping, "no-wrapping", "", false, "if set, we won't wrap the output the json in a data tag")
testJson.Flags().BoolVarP(&compliant, "compliant", "", false, "if set, we wrap most keys in an attributes tage automatically.")

rootCmd.PersistentFlags().Var(
RootCmd.PersistentFlags().Var(
enumflag.New(&logger.Loglevel, "log", logger.LoglevelIds, enumflag.EnumCaseInsensitive),
"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.")
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.")

aliasesCmd.AddCommand(aliasListCmd, aliasClearCmd)
}

var rootCmd = &cobra.Command{
var RootCmd = &cobra.Command{
Use: os.Args[0],
Short: "A command line interface for interacting with the Elastic Path Commerce Cloud API",
Long: `The EPCC CLI tool provides a powerful command line interface for interacting with the Elastic Path Commerce Cloud API.
Expand All @@ -71,10 +73,11 @@ Environment Variables
log.SetLevel(logger.Loglevel)
},
SilenceUsage: true,
Version: fmt.Sprintf("EPCC CLI %s (Commit %s)", version.Version, version.Commit),
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
if err := RootCmd.Execute(); err != nil {
log.Errorf("Error occured while processing command %s", err)
os.Exit(1)
}
Expand Down
5 changes: 4 additions & 1 deletion external/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
Expand All @@ -36,6 +37,8 @@ func DoFileRequest(ctx context.Context, path string, payload io.Reader, contentT
return doRequestInternal(ctx, "POST", contentType, path, "", payload)
}

var UserAgent = fmt.Sprintf("epcc-cli/%s-%s (%s/%s)", version.Version, version.Commit, runtime.GOOS, runtime.GOARCH)

// DoRequest makes a html request to the EPCC API and handles the response.
func doRequestInternal(ctx context.Context, method string, contentType string, path string, query string, payload io.Reader) (response *http.Response, error error) {
reqURL, err := url.Parse(config.Envs.EPCC_API_BASE_URL)
Expand Down Expand Up @@ -66,7 +69,7 @@ func doRequestInternal(ctx context.Context, method string, contentType string, p

req.Header.Add("Content-Type", contentType)

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

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