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
31 changes: 30 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,39 @@ package main

import (
"fmt"
"github.com/elasticpath/epcc-cli/external/command"
"github.com/elasticpath/epcc-cli/external/help"
_ "github.com/elasticpath/epcc-cli/external/resources"
"os"
)

var commands = []command.Command{
help.Command,
}

func main() {
fmt.Printf("Hello %s", "World!")
argsWithoutProg := os.Args[1:]

if (len(argsWithoutProg)) == 0 {
fmt.Printf("No command specified")
os.Exit(1)
}

commandToRun := argsWithoutProg[0]

cmds := make(map[string]command.Command)

for _, cmd := range commands {
cmds[cmd.Keyword] = cmd
}

for _, cmd := range commands {
if cmd.Keyword == commandToRun {
argsWithoutCmd := argsWithoutProg[1:]
os.Exit(cmd.Execute(cmds, argsWithoutCmd))
}
}

fmt.Printf("Unknown command %s specified", commandToRun)
os.Exit(0)
}
15 changes: 15 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "testing"

func TestNoDuplicateCommands(t *testing.T) {
set := make(map[string]bool)

for _, command := range commands {
set[command.Keyword] = true
}

if len(set) != len(commands) {
t.Fatalf("Duplicate commands have been registered since the length of the keyword set is not the same as the array")
}
}
10 changes: 10 additions & 0 deletions external/command/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package command

type Command struct {
// The keyword the command should use
Keyword string
// A one-line description of the command
Description string
// The function that will be executed
Execute func(cmds map[string]Command, args []string) int
}
44 changes: 44 additions & 0 deletions external/help/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package help

import (
"fmt"
"github.com/elasticpath/epcc-cli/external/command"
"sort"
)

var Command = command.Command{
Keyword: "help",
Description: "Displays this screen",
Execute: func(cmds map[string]command.Command, args []string) int {

keys := make([]string, 0, len(cmds))
for k := range cmds {
keys = append(keys, k)
}
sort.Strings(keys)

fmt.Printf(`
Setup

The EPCC CLI tool uses environment variables for configuration and in particular a tool like https://direnv.net/ which
auto populates your shell with environment variables when you switch directories. This allows you to store a context in a folder,
and come back to it at any time.

Environment Variables

- EPCC_API_BASE_URL - The API endpoint that we will hit
- EPCC_CLIENT_ID - The client id (available in Commerce Manager)
- EPCC_CLIENT_SECRET - The client secret (available in Commerce Manager)
- EPCC_BETA_API_FEATURES - Beta features in the API we want to enable.

The following commands are supported:

`)

for _, key := range keys {
fmt.Printf(" %s - %s", cmds[key].Keyword, cmds[key].Description)
}

return 0
},
}
10 changes: 0 additions & 10 deletions external/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources

import (
_ "embed"
"fmt"
"gopkg.in/yaml.v3"
)

Expand All @@ -18,9 +17,6 @@ func init() {
panic("Couldn't load the resource meta data")
}

// You can delete this line later.
fmt.Printf("Loaded data for %d resources: ", len(Resources))

for key, val := range Resources {
// Fix the key
val.Type = key
Expand All @@ -29,13 +25,7 @@ func init() {
// Fix the key
attributeVal.Key = attributeName
}

// Delete me later
fmt.Printf("%s,", key)
}

// Delete me later
fmt.Print("\n")
}

type Resource struct {
Expand Down