Skip to content

Commit

Permalink
Merge pull request #25 from bwhaley/fix-linting-errors
Browse files Browse the repository at this point in the history
Fix linting errors
  • Loading branch information
bwhaley authored Nov 9, 2020
2 parents edb3c81 + 9f170be commit 786ea92
Show file tree
Hide file tree
Showing 12 changed files with 559 additions and 45 deletions.
1 change: 1 addition & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:

env:
SSMSH_VERSION: 1.4.3
AWS_DEFAULT_REGION: us-east-1

jobs:
lint:
Expand Down
3 changes: 1 addition & 2 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ func checkRecursion(paths []string) ([]string, bool) {

// parsePath determines whether a path includes a region
func parsePath(path string) (parameterPath parameterstore.ParameterPath) {
var pathParts []string
pathParts = strings.Split(path, ":")
pathParts := strings.Split(path, ":")
switch len(pathParts) {
case 1:
parameterPath.Name = pathParts[0]
Expand Down
2 changes: 1 addition & 1 deletion commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func list(path string, recurse bool) ([]string, error) {
signal.Notify(sigs, syscall.SIGINT)

quit := make(chan bool)
lr := make(chan parameterstore.ListResult, 0)
lr := make(chan parameterstore.ListResult)
go func() {
parameterPath := parsePath(path)
ps.List(parameterPath, recurse, lr, quit)
Expand Down
5 changes: 4 additions & 1 deletion commands/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ type NoChangeNotificationAttributes struct {

func policy(c *ishell.Context) {
if len(c.Args) == 1 {
printPolicy(c.Args[0])
err := printPolicy(c.Args[0])
if err != nil {
shell.Printf("Error: %s\n", err)
}
} else if len(c.Args) > 1 {
err := createPolicy(c.Args[0], c.Args[1:])
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions commands/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ func validatePolicies(s string) (err error) {
}
}
policyBytes, err := json.Marshal(policySet)
if err != nil {
return err
}
putParamInput.Policies = aws.String(string(policyBytes))
putParamInput.Tier = aws.String(AdvancedTier)
return nil
Expand Down
5 changes: 4 additions & 1 deletion commands/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func region(c *ishell.Context) {
}
} else if len(c.Args) == 1 {
ps.Region = c.Args[0]
ps.NewParameterStore()
err := ps.NewParameterStore(true)
if err != nil {
shell.Printf("Error: %s", err)
}
}
}
1 change: 0 additions & 1 deletion commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ func rm(c *ishell.Context) {
} else {
shell.Println(rmUsage, err)
}
return
}
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ require (
github.com/aws/aws-sdk-go v1.19.20-0.20190426182805-4dbd475cffe6
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/golangci/golangci-lint v1.32.2 // indirect
github.com/mattn/go-shellwords v1.0.5
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20190426135247-a129542de9ae // indirect
gopkg.in/gcfg.v1 v1.2.3
gopkg.in/warnings.v0 v0.1.2 // indirect
)
485 changes: 485 additions & 0 deletions go.sum

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions parameterstore/parameterstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ func (ps *ParameterStore) SetDefaults(cfg config.Config) {
}

// NewParameterStore initializes a ParameterStore with default values
func (ps *ParameterStore) NewParameterStore() error {
func (ps *ParameterStore) NewParameterStore(checkCredentials bool) error {
ps.Cwd = Delimiter

ps.Clients = make(map[string]ssmiface.SSMAPI)
ps.Clients[ps.Region] = ssm.New(saws.NewSession(ps.Region, ps.Profile))

// Check for a non-existent parameter to validate credentials & permissions
_, err := ps.Get([]string{Delimiter}, ps.Region)
if err != nil {
return err
if checkCredentials {
// Check for a non-existent parameter to validate credentials & permissions
_, err := ps.Get([]string{Delimiter}, ps.Region)
if err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -218,7 +220,6 @@ func (ps *ParameterStore) recursiveDelete(path ParameterPath) (err error) {

// deleteByRegion groups parameters by region before calling delete()
func (ps *ParameterStore) deleteByRegion(params []ParameterPath) (err error) {
const maxParams = 10
paramsByRegion := make(map[string][]string)
for _, p := range params {
paramsByRegion[p.Region] = append(paramsByRegion[p.Region], p.Name)
Expand Down Expand Up @@ -500,10 +501,7 @@ func (ps *ParameterStore) isParameter(param ParameterPath) bool {
Name: aws.String(param.Name),
}
_, err := ps.Clients[param.Region].GetParameter(p)
if err != nil {
return false
}
return true
return err == nil
}

// isPath checks for the existence of at least one key under path
Expand Down
65 changes: 44 additions & 21 deletions parameterstore/parameterstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ var HouseStark = []*ssm.Parameter{
RobStark,
}

var HouseStarkNext = []*ssm.Parameter{
JonSnow,
}

var HouseTargaryen = []*ssm.Parameter{
DaenerysTargaryen,
}
Expand Down Expand Up @@ -116,7 +112,10 @@ func (m mockedSSM) PutParameter(in *ssm.PutParameterInput) (*ssm.PutParameterOut
func TestPut(t *testing.T) {
var expectedVersion int64 = 1
var p parameterstore.ParameterStore
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal(err)
}
p.Cwd = parameterstore.Delimiter
p.Clients[p.Region] = mockedSSM{
PutParameterResp: ssm.PutParameterOutput{
Expand Down Expand Up @@ -151,7 +150,10 @@ func TestMoveParameter(t *testing.T) {
}
var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal(err)
}
p.Cwd = parameterstore.Delimiter
p.Clients[p.Region] = mockedSSM{
GetParameterResp: []ssm.GetParameterOutput{
Expand Down Expand Up @@ -189,7 +191,7 @@ func TestMoveParameter(t *testing.T) {
},
},
}
err := p.Move(srcParam, dstParam)
err = p.Move(srcParam, dstParam)
if err != nil {
t.Fatal("Error moving parameter", err)
}
Expand Down Expand Up @@ -217,7 +219,7 @@ func TestMoveParameter(t *testing.T) {
}
_, err = p.Get([]string{dstParam.Name}, p.Region)
if err != nil {
msg := fmt.Errorf("Expected to find %s but didn't!", dstParam.Name)
msg := fmt.Errorf("Expected to find %s but didn't", dstParam.Name)
t.Fatal(msg)
}
}
Expand All @@ -234,7 +236,10 @@ func TestCopyPath(t *testing.T) {

var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal(err)
}
p.Cwd = parameterstore.Delimiter
bothHouses := append(HouseStark, HouseTargaryen...)
p.Clients[p.Region] = mockedSSM{
Expand Down Expand Up @@ -263,7 +268,7 @@ func TestCopyPath(t *testing.T) {
NextToken: aws.String(""),
},
}
err := p.Copy(srcPath, dstPath, true)
err = p.Copy(srcPath, dstPath, true)
if err != nil {
t.Fatal("Error copying parameter path: ", err)
}
Expand Down Expand Up @@ -292,7 +297,10 @@ func TestCopyParameter(t *testing.T) {
}
var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal(err)
}
p.Cwd = parameterstore.Delimiter
p.Clients[p.Region] = mockedSSM{
GetParameterResp: []ssm.GetParameterOutput{
Expand Down Expand Up @@ -330,7 +338,7 @@ func TestCopyParameter(t *testing.T) {
},
},
}
err := p.Copy(srcParam, dstParam, false)
err = p.Copy(srcParam, dstParam, false)
if err != nil {
t.Fatal("Error copying parameter", err)
}
Expand Down Expand Up @@ -376,13 +384,16 @@ func TestCwd(t *testing.T) {

var p parameterstore.ParameterStore
for _, c := range cases {
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal("unexpected error", err)
}
p.Region = "region"
p.Cwd = parameterstore.Delimiter
p.Clients[p.Region] = mockedSSM{
GetParametersByPathResp: c.GetParametersByPathResp,
}
err := p.SetCwd(parameterstore.ParameterPath{Name: c.Path, Region: "region"})
err = p.SetCwd(parameterstore.ParameterPath{Name: c.Path, Region: "region"})
if err != nil {
t.Fatal("unexpected error", err)
}
Expand All @@ -392,13 +403,16 @@ func TestCwd(t *testing.T) {
}
}

p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal("unexpected error", err)
}
p.Cwd = parameterstore.Delimiter
testDir := parameterstore.ParameterPath{
Name: "/nodir",
Region: "region",
}
err := p.SetCwd(testDir)
err = p.SetCwd(testDir)
if err == nil {
msg := fmt.Errorf("Expected error for dir %s, got cwd %s ", testDir, p.Cwd)
t.Fatal(msg)
Expand Down Expand Up @@ -432,11 +446,14 @@ func TestDelete(t *testing.T) {

var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal("unexpected error", err)
}
p.Clients[p.Region] = mockedSSM{
DeleteParametersResp: deleteParametersOutput,
}
err := p.Remove(testParams, false)
err = p.Remove(testParams, false)
if err == nil {
msg := fmt.Errorf("Expected error for param %s, got %v ", testParams[2], err)
t.Fatal(msg)
Expand All @@ -463,7 +480,10 @@ func TestGetHistory(t *testing.T) {
}
var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal("unexpected error", err)
}
p.Clients[p.Region] = mockedSSM{
GetParameterHistoryResp: getHistoryOutput,
}
Expand Down Expand Up @@ -571,15 +591,18 @@ func TestList(t *testing.T) {
for _, c := range cases {
var p parameterstore.ParameterStore
p.Region = "region"
p.NewParameterStore()
err := p.NewParameterStore(false)
if err != nil {
t.Fatal("unexpected error", err)
}
p.Clients[p.Region] = mockedSSM{
GetParametersByPathResp: c.GetParametersByPathResp,
GetParametersByPathNext: c.GetParametersByPathNext,
GetParametersResp: c.GetParametersResp,
}
p.Cwd = parameterstore.Delimiter

ch := make(chan parameterstore.ListResult, 0)
ch := make(chan parameterstore.ListResult)
quit := make(chan bool)
go func() {
p.List(c.Query, c.Recurse, ch, quit)
Expand Down
9 changes: 7 additions & 2 deletions ssmsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
shell := ishell.New()
var ps parameterstore.ParameterStore
ps.SetDefaults(cfg)
err = ps.NewParameterStore()
err = ps.NewParameterStore(true)
if err != nil {
shell.Println("Error initializing session. Is your authentication correct?", err)
os.Exit(1)
Expand All @@ -48,7 +48,12 @@ func main() {
} else if *file != "" {
processFile(shell, *file)
} else if len(flag.Args()) > 1 {
shell.Process(flag.Args()...)
err := shell.Process(flag.Args()...)
if err != nil {
shell.Println("Error executing shell process:", err)
shell.Println("This might be a bug. Please open an issue at github.com/bwhaley/ssmsh.\n")
os.Exit(1)
}
} else {
shell.Run()
shell.Close()
Expand Down

0 comments on commit 786ea92

Please sign in to comment.