forked from aws/session-manager-plugin
/
inputhandler.go
122 lines (109 loc) · 3.01 KB
/
inputhandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// Package ssmclicommands contains all the commands with its implementation.
package ssmclicommands
import (
"errors"
"fmt"
"io"
"strings"
"github.com/matipan/ssm/src/ssmclicommands/utils"
)
const (
ArgumentLength = 2
)
// ParseCliCommand function parses command and returns options to validate.
func ParseCliCommand(args []string) (err error, options []string, command string, subcommand string, parameters map[string][]string) {
argCount := len(args)
pos := 1
// Options
options = make([]string, 0)
for _, val := range args[pos:] {
if !utils.IsFlag(val) {
break
}
options = append(options, utils.GetFlag(val))
pos++
}
// Command
if pos >= argCount {
err = errors.New("command is required")
return
}
command = strings.ToLower(args[pos])
pos++
//subcommand
if pos >= argCount {
return
}
subcommand = strings.ToLower(args[pos])
pos++
// Parameters
if pos >= argCount {
return
}
parameters = make(map[string][]string)
var parameterName string
for _, val := range args[2:] {
if utils.IsFlag(val) {
parameterName = utils.GetFlag(val)
if parameterName == "" {
// aws cli doesn't valid this
err = fmt.Errorf("input contains parameter with no name")
return
}
if _, exists := parameters[parameterName]; exists {
// aws cli doesn't valid this
err = fmt.Errorf("duplicate parameter %v", parameterName)
return
}
parameters[parameterName] = make([]string, 0)
} else {
parameters[parameterName] = append(parameters[parameterName], val)
}
}
return
}
// ValidateInput function validates the input and displays response accordingly.
func ValidateInput(args []string, out io.Writer) {
if len(args) < ArgumentLength {
utils.DisplayCommandUsage(out)
return
}
err, _, command, subcommand, parameters := ParseCliCommand(args)
if err != nil {
utils.DisplayCommandUsage(out)
fmt.Fprint(out, err.Error())
return
}
if cmd, exists := utils.SsmCliCommands[command]; exists {
if utils.IsHelp(subcommand, parameters) {
fmt.Fprintln(out, cmd.Help())
} else {
cmdErr, result := cmd.Execute(parameters)
if cmdErr != nil {
utils.DisplayCommandUsage(out)
fmt.Fprint(out, cmdErr.Error())
return
} else {
fmt.Fprint(out, result)
}
}
} else if command == utils.HelpFlag {
utils.DisplayHelp(out)
} else {
utils.DisplayCommandUsage(out)
fmt.Fprintf(out, "\nInvalid command %v. The following commands are supported:\n\n", command)
utils.DisplaySupportedCommands(out)
}
}