Skip to content

Commit

Permalink
lint: introduce lint and fix lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Yadav <rohit@apache.org>
  • Loading branch information
rohityadavcloud committed Apr 13, 2018
1 parent 88fcf8f commit 0eb9c6b
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 86 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ go:

script:
- make all
- make lint
55 changes: 23 additions & 32 deletions cli/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,27 @@ import (
"github.com/chzyer/readline/runes"
)

type CliCompleter struct {
type autoCompleter struct {
Config *config.Config
}

var completer *CliCompleter

func buildApiCacheMap(apiMap map[string][]*config.Api) map[string][]*config.Api {
func buildAPICacheMap(apiMap map[string][]*config.API) map[string][]*config.API {
for _, cmd := range cmd.AllCommands() {
verb := cmd.Name
if cmd.SubCommands != nil && len(cmd.SubCommands) > 0 {
for _, scmd := range cmd.SubCommands {
dummyApi := &config.Api{
dummyAPI := &config.API{
Name: scmd,
Verb: verb,
}
apiMap[verb] = append(apiMap[verb], dummyApi)
apiMap[verb] = append(apiMap[verb], dummyAPI)
}
} else {
dummyApi := &config.Api{
dummyAPI := &config.API{
Name: "",
Verb: verb,
}
apiMap[verb] = append(apiMap[verb], dummyApi)
apiMap[verb] = append(apiMap[verb], dummyAPI)
}
}
return apiMap
Expand Down Expand Up @@ -87,9 +85,9 @@ func doInternal(line []rune, pos int, lineLen int, argName []rune) (newLine [][]
return
}

func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {

apiMap := buildApiCacheMap(t.Config.GetApiVerbMap())
apiMap := buildAPICacheMap(t.Config.GetAPIVerbMap())

var verbs []string
for verb := range apiMap {
Expand Down Expand Up @@ -138,7 +136,7 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
}

// Find API
var apiFound *config.Api
var apiFound *config.API
for _, api := range apiMap[verbFound] {
if api.Noun == nounFound {
apiFound = api
Expand All @@ -165,7 +163,7 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
return
}

var autocompleteApi *config.Api
var autocompleteAPI *config.API
var relatedNoun string
if arg.Name == "id" || arg.Name == "ids" {
relatedNoun = apiFound.Noun
Expand All @@ -179,23 +177,23 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
}
for _, related := range apiMap["list"] {
if relatedNoun == related.Noun {
autocompleteApi = related
autocompleteAPI = related
break
}
}

if autocompleteApi == nil {
if autocompleteAPI == nil {
return nil, 0
}

r := cmd.NewRequest(nil, shellConfig, nil, nil)
autocompleteApiArgs := []string{"listall=true"}
if autocompleteApi.Noun == "templates" {
autocompleteApiArgs = append(autocompleteApiArgs, "templatefilter=all")
r := cmd.NewRequest(nil, completer.Config, nil, nil)
autocompleteAPIArgs := []string{"listall=true"}
if autocompleteAPI.Noun == "templates" {
autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=all")
}
response, _ := cmd.NewAPIRequest(r, autocompleteApi.Name, autocompleteApiArgs)
response, _ := cmd.NewAPIRequest(r, autocompleteAPI.Name, autocompleteAPIArgs)

var autocompleteOptions []SelectOption
var autocompleteOptions []selectOption
for _, v := range response {
switch obj := v.(type) {
case []interface{}:
Expand All @@ -207,9 +205,9 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
if !ok {
continue
}
opt := SelectOption{}
opt := selectOption{}
if resource["id"] != nil {
opt.Id = resource["id"].(string)
opt.ID = resource["id"].(string)
}
if resource["name"] != nil {
opt.Name = resource["name"].(string)
Expand All @@ -232,15 +230,15 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {
return autocompleteOptions[i].Name < autocompleteOptions[j].Name
})
fmt.Println()
selectedOption := ShowSelector(autocompleteOptions)
selectedOption := showSelector(autocompleteOptions)
if strings.HasSuffix(arg.Name, "id") || strings.HasSuffix(arg.Name, "ids") {
selected = selectedOption.Id
selected = selectedOption.ID
} else {
selected = selectedOption.Name
}
} else {
if len(autocompleteOptions) == 1 {
selected = autocompleteOptions[0].Id
selected = autocompleteOptions[0].ID
}
}
options = [][]rune{[]rune(selected + " ")}
Expand All @@ -250,10 +248,3 @@ func (t *CliCompleter) Do(line []rune, pos int) (options [][]rune, offset int) {

return options, offset
}

func NewCompleter(cfg *config.Config) *CliCompleter {
completer = &CliCompleter{
Config: cfg,
}
return completer
}
1 change: 1 addition & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/chzyer/readline"
)

// ExecCmd executes a single provided command
func ExecCmd(cfg *config.Config, args []string, shell *readline.Instance) error {
if len(args) < 1 {
return nil
Expand Down
34 changes: 17 additions & 17 deletions cli/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,47 @@ import (
"github.com/manifoldco/promptui"
)

type SelectOption struct {
Id string
type selectOption struct {
ID string
Name string
Detail string
}

type Selector struct {
type selector struct {
InUse bool
}

var selector Selector
var optionSelector selector

func init() {
selector = Selector{
optionSelector = selector{
InUse: false,
}
}

func (s Selector) lock() {
func (s selector) lock() {
s.InUse = true
}

func (s Selector) unlock() {
func (s selector) unlock() {
s.InUse = false
}

func ShowSelector(options []SelectOption) SelectOption {
if selector.InUse {
return SelectOption{}
func showSelector(options []selectOption) selectOption {
if optionSelector.InUse {
return selectOption{}
}
selector.lock()
defer selector.unlock()
optionSelector.lock()
defer optionSelector.unlock()

templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "▶ {{ .Name | cyan }} ({{ .Id | red }})",
Inactive: " {{ .Name | cyan }} ({{ .Id | red }})",
Selected: "👊Selected: {{ .Name | cyan }} ({{ .Id | red }})",
Active: "▶ {{ .Name | cyan }} ({{ .ID | red }})",
Inactive: " {{ .Name | cyan }} ({{ .ID | red }})",
Selected: "👊Selected: {{ .Name | cyan }} ({{ .ID | red }})",
Details: `
--------- Current Selection ----------
{{ "Id:" | faint }} {{ .Id }}
{{ "ID:" | faint }} {{ .ID }}
{{ "Name:" | faint }} {{ .Name }}
{{ "Info:" | faint }} {{ .Detail }}`,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func ShowSelector(options []SelectOption) SelectOption {

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return SelectOption{}
return selectOption{}
}

return options[i]
Expand Down
10 changes: 7 additions & 3 deletions cli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ import (
"github.com/chzyer/readline"
)

var shellConfig *config.Config
var completer *autoCompleter

// ExecShell starts a shell
func ExecShell(cfg *config.Config) {
shellConfig = cfg
completer = &autoCompleter{
Config: cfg,
}

shell, err := readline.NewEx(&readline.Config{
Prompt: cfg.GetPrompt(),
HistoryFile: cfg.HistoryFile,
AutoComplete: NewCompleter(cfg),
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
VimMode: false,
Expand Down
1 change: 1 addition & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

var apiCommand *Command

// GetAPIHandler returns a catchall command handler
func GetAPIHandler() *Command {
return apiCommand
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
)

// Command describes a CLI command
type Command struct {
Name string
Help string
Expand All @@ -32,14 +33,17 @@ type Command struct {
var commands []*Command
var commandMap map[string]*Command

// FindCommand finds command handler for a command string
func FindCommand(name string) *Command {
return commandMap[name]
}

// AllCommands returns all available commands
func AllCommands() []*Command {
return commands
}

// AddCommand adds a command to internal list
func AddCommand(cmd *Command) {
commands = append(commands, cmd)
if commandMap == nil {
Expand All @@ -48,6 +52,7 @@ func AddCommand(cmd *Command) {
commandMap[cmd.Name] = cmd
}

// PrintUsage prints help usage for a command
func PrintUsage() {
commandHelp := ""
for _, cmd := range commands {
Expand Down
9 changes: 5 additions & 4 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func encodeRequestParams(params url.Values) string {
return buf.String()
}

// NewAPIRequest makes an API request to configured management server
func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{}, error) {
params := make(url.Values)
params.Add("command", api)
Expand All @@ -66,7 +67,7 @@ func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{
}
}

apiKey := r.Config.Core.ActiveProfile.ApiKey
apiKey := r.Config.Core.ActiveProfile.APIKey
secretKey := r.Config.Core.ActiveProfile.SecretKey

if len(apiKey) > 0 {
Expand All @@ -81,10 +82,10 @@ func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature))

apiUrl := fmt.Sprintf("%s?%s", r.Config.Core.ActiveProfile.Url, encodedParams)
apiURL := fmt.Sprintf("%s?%s", r.Config.Core.ActiveProfile.URL, encodedParams)

//fmt.Println("[debug] Requesting: ", apiUrl)
response, err := http.Get(apiUrl)
//fmt.Println("[debug] Requesting: ", apiURL)
response, err := http.Get(apiURL)
if err != nil {
fmt.Println("Error:", err)
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions cmd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"github.com/chzyer/readline"
)

// Request describes a command request
type Request struct {
Command *Command
Config *config.Config
Shell *readline.Instance
Args []string
}

// NewRequest creates a new request from a command
func NewRequest(cmd *Command, cfg *config.Config, shell *readline.Instance, args []string) *Request {
return &Request{
Command: cmd,
Expand Down
2 changes: 1 addition & 1 deletion cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
}
subCommand := r.Args[0]
value := strings.Join(r.Args[1:], " ")
r.Config.UpdateGlobalConfig(subCommand, value)
r.Config.UpdateConfig(subCommand, value)

if subCommand == "profile" && r.Shell != nil {
r.Shell.SetPrompt(r.Config.GetPrompt())
Expand Down
3 changes: 3 additions & 0 deletions config/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ package config

import "fmt"

// Name of the CLI
func (c *Config) Name() string {
return "Apache CloudStack 🐵 cloudmonkey"
}

// Version of the CLI
func (c *Config) Version() string {
return "6.0.0-alpha1"
}

// PrintHeader prints startup message in CLI mode
func (c *Config) PrintHeader() {
fmt.Println(c.Name(), c.Version())
fmt.Println("Type \"help\" for details, \"sync\" to update API cache or press tab to list commands")
Expand Down
Loading

0 comments on commit 0eb9c6b

Please sign in to comment.