Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jckuester committed Aug 13, 2020
1 parent 4577b92 commit 177d089
Show file tree
Hide file tree
Showing 9 changed files with 987 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2020 Jan-Christoph Kuester

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/jckuester/awsrm

go 1.14

require (
github.com/apex/log v1.8.0
github.com/fatih/color v1.9.0
github.com/jckuester/awsls v0.4.1-0.20200809110610-a39a0788d3d5
github.com/jckuester/terradozer v0.1.2
github.com/spf13/pflag v1.0.3
)
593 changes: 593 additions & 0 deletions go.sum

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions internal/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package internal

import (
"fmt"
"io"

"github.com/apex/log"
)

// UserConfirmedDeletion asks the user to confirm before destroying any resources.
func UserConfirmedDeletion(r io.Reader, force bool) bool {
if force {
LogTitle("user will not be asked for confirmation (force mode)")
return true
}

log.Info("Are you sure you want to delete these resources (cannot be undone)? Only YES will be accepted.")
fmt.Print(fmt.Sprintf("%23v", "Enter a value: "))

var response string

_, err := fmt.Fscanln(r, &response)
if err != nil {
log.Fatal(err.Error())
}

if response == "YES" {
return true
}

return false
}
39 changes: 39 additions & 0 deletions internal/confirm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal_test

import (
"strings"
"testing"

"github.com/jckuester/awsrm/internal"
"github.com/stretchr/testify/assert"
)

func TestUserConfirmedDeletion(t *testing.T) {
tests := []struct {
name string
force bool
userInput string
expectedConfirmation bool
}{
{
name: "confirmed with YES",
userInput: "YES",
expectedConfirmation: true,
},
{
name: "confirmed with yes",
userInput: "yes",
},
{
name: "force mode",
force: true,
expectedConfirmation: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actualConfirmation := internal.UserConfirmedDeletion(strings.NewReader(tc.userInput), tc.force)
assert.Equal(t, tc.expectedConfirmation, actualConfirmation)
})
}
}
29 changes: 29 additions & 0 deletions internal/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package internal

import (
"fmt"
"strings"

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/fatih/color"
)

// DefaultInitialPadding is the default padding in the log library.
const DefaultInitialPadding = 3

// ExtraPadding is the double of the DefaultInitialPadding.
const ExtraPadding = DefaultInitialPadding * 2

// LogTitle pretty prints a given title.
func LogTitle(title string) {
cli.Default.Padding = DefaultInitialPadding

log.Info(color.New(color.Bold).Sprint(strings.ToUpper(title)))

cli.Default.Padding = ExtraPadding
}

func Pad(s string) string {
return fmt.Sprintf("%-50v", s)
}
29 changes: 29 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package internal

import (
"fmt"
"runtime"
)

//nolint:gochecknoglobals
var (
version = "dev"
commit = "?"
date = "?"
)

func BuildVersionString() string {
var result = fmt.Sprintf("version: %s", version)

if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}

if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}

result = fmt.Sprintf("%s\nusing: %s", result, runtime.Version())

return result
}
15 changes: 15 additions & 0 deletions internal/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package internal_test

import (
"runtime"
"testing"

"github.com/jckuester/awsrm/internal"
"github.com/stretchr/testify/assert"
)

func TestBuildVersionString(t *testing.T) {
actualVersionString := internal.BuildVersionString()

assert.Equal(t, actualVersionString, "version: dev\ncommit: ?\nbuilt at: ?\nusing: "+runtime.Version())
}
Loading

0 comments on commit 177d089

Please sign in to comment.