forked from jckuester/awsrm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
987 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.