Skip to content

Commit 97d0aea

Browse files
author
Chris Gilmer
authored
Merge pull request #63 from trussworks/cg_standards
Update repo to latest CLI standards
2 parents 6c92d29 + bc4db11 commit 97d0aea

10 files changed

+137
-30
lines changed

.circleci/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ jobs:
1414
- pre-commit-dot-cache-{{ checksum ".pre-commit-config.yaml" }}
1515
- go-mod-sources-v1-{{ checksum "go.sum" }}
1616
- run:
17-
name: Run pre-commit and Go tests
17+
name: Run pre-commit
18+
command: pre-commit run -a
19+
- run:
20+
name: Run Go tests
1821
command: make test
1922
- save_cache:
2023
key: pre-commit-dot-cache-{{ checksum ".pre-commit-config.yaml" }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist
1+
bin/
2+
dist/

.golangci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
linters-settings:
2+
govet:
3+
check-shadowing: true
4+
goimports:
5+
# put imports beginning with prefix after 3rd-party packages;
6+
# it's a comma-separated list of prefixes
7+
local-prefixes: github.com/trussworks/find-guardduty-user
8+
9+
linters:
10+
enable:
11+
- deadcode
12+
- errcheck
13+
- gofmt
14+
- goimports
15+
- golint
16+
- gosec
17+
- govet
18+
- ineffassign
19+
- staticcheck
20+
- structcheck
21+
- typecheck
22+
- varcheck
23+
disable:
24+
- gosimple #deprecated https://github.com/golangci/golangci-lint/issues/357
25+
- unused #deprecated https://github.com/dominikh/go-tools/tree/master/cmd/unused
26+
fast: false
27+
28+
issues:
29+
# golangci-lint excludes by default some checks they consider "annoying"
30+
# A better practice is for each repo to choose which ones to disable
31+
exclude-use-default: false
32+
fix: true
33+
34+
run:
35+
modules-download-mode: readonly

.goreleaser.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
env:
2+
- GO111MODULE=on
13
before:
24
hooks:
35
- go mod download
@@ -18,14 +20,14 @@ brews:
1820
name: trussworks-infra
1921
2022
archives:
21-
- replacements:
22-
darwin: Darwin
23-
linux: Linux
24-
windows: Windows
25-
386: i386
26-
amd64: x86_64
23+
-
24+
replacements:
25+
darwin: Darwin
26+
linux: Linux
27+
amd64: x86_64
2728
checksum:
2829
name_template: 'checksums.txt'
30+
algorithm: sha256
2931
snapshot:
3032
name_template: "{{ .Tag }}-next"
3133
changelog:

.pre-commit-config.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ repos:
77
- repo: git://github.com/pre-commit/pre-commit-hooks
88
rev: v3.1.0
99
hooks:
10-
- id: check-json
1110
- id: check-merge-conflict
1211
- id: check-yaml
1312
- id: detect-private-key
14-
- id: pretty-format-json
15-
args:
16-
- --autofix
1713
- id: trailing-whitespace
1814

1915
- repo: git://github.com/igorshubovych/markdownlint-cli
2016
rev: v0.23.1
2117
hooks:
2218
- id: markdownlint
19+
20+
- repo: git://github.com/trussworks/pre-commit-hooks
21+
rev: v0.0.4
22+
hooks:
23+
- id: circleci-validate
24+
- id: goreleaser-check

Makefile

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
.PHONY: ensure_pre_commit
2-
ensure_pre_commit: .git/hooks/pre-commit ## Ensure pre-commit is installed
3-
.git/hooks/pre-commit: /usr/local/bin/pre-commit
4-
pre-commit install
5-
pre-commit install-hooks
1+
# goreleaser removes the `v` prefix when building and this does too
2+
VERSION = 0.0.1
63

7-
.PHONY: pre_commit_tests
8-
pre_commit_tests: ensure_pre_commit ## Run pre-commit tests
9-
pre-commit run --all-files --show-diff-on-failure
4+
ifdef CIRCLECI
5+
UNAME_S := $(shell uname -s)
6+
ifeq ($(UNAME_S),Linux)
7+
LDFLAGS=-linkmode external -extldflags -static
8+
endif
9+
endif
10+
11+
.PHONY: help
12+
help: ## Print the help documentation
13+
@grep -E '^[/a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
14+
15+
bin/setup-new-aws-user: ## Build setup-new-aws-user
16+
go build -ldflags "$(LDFLAGS) -X main.version=${VERSION}" -o bin/setup-new-aws-user ./cmd/
1017

1118
.PHONY: test
12-
test: pre_commit_tests
19+
test:
1320
go test ./cmd/...
1421

1522
.PHONY: clean
1623
clean:
1724
rm -f .*.stamp
25+
rm -rf ./bin
26+
rm -rf ./dist
27+
28+
.PHONY: goreleaser_check
29+
goreleaser_check: ## Goreleaser check configuration
30+
goreleaser check
31+
32+
.PHONY: goreleaser_build
33+
goreleaser_build: ## Goreleaser build configuration
34+
goreleaser build --snapshot --rm-dist
35+
36+
.PHONY: goreleaser_test
37+
goreleaser_test: ## Goreleaser test configuration
38+
goreleaser --snapshot --skip-publish --rm-dist
39+
40+
default: help

cmd/main.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
const maxNumAccessKeys = 2
2626
const maxMFATokenPromptAttempts = 5
2727

28+
// version is the published version of the utility
29+
var version string
30+
2831
var validate *validator.Validate
2932

3033
// MFATokenPair holds two MFA tokens for enabling virtual
@@ -41,6 +44,7 @@ type cliOptions struct {
4144
IAMUser string `required:"true" long:"iam-user" description:"The IAM user name"`
4245
Role string `required:"true" long:"role" description:"The user role type"`
4346
Output string `long:"output" default:"json" description:"The AWS CLI output format"`
47+
Version bool `long:"version" description:"Print the version and exit"`
4448
}
4549

4650
// User holds information for the AWS user being configured by this script
@@ -446,7 +450,7 @@ func generateQrCode(payload string, tempFile *os.File) error {
446450

447451
// Write the QR PNG to the Temp File
448452
if _, err := tempFile.Write(qr); err != nil {
449-
tempFile.Close()
453+
_ = tempFile.Close()
450454
return err
451455
}
452456
return nil
@@ -485,13 +489,29 @@ func getPartition(region string) (string, error) {
485489
func main() {
486490
// parse command line flags
487491
var options cliOptions
488-
parser := flags.NewParser(&options, flags.Default)
492+
parser := flags.NewParser(&options, flags.HelpFlag|flags.PassDoubleDash)
489493

490-
_, err := parser.Parse()
491-
if err != nil {
492-
log.Fatal(err)
494+
// Parse the command line flags
495+
_, errParse := parser.Parse()
496+
497+
// Print the version and exit
498+
if options.Version {
499+
// Disable output not related to version before printing
500+
log.SetFlags(0)
501+
if len(version) == 0 {
502+
log.Println("development")
503+
} else {
504+
log.Println(version)
505+
}
506+
os.Exit(0)
507+
}
508+
509+
// Manage parse errors after checking for version
510+
if errParse != nil {
511+
log.Fatal(errParse)
493512
}
494513

514+
// Validator used to validate input options for MFA
495515
validate = validator.New()
496516

497517
// initialize things
@@ -522,7 +542,12 @@ func main() {
522542
log.Fatal(err)
523543
}
524544
// Cleanup after ourselves
525-
defer os.Remove(tempfile.Name())
545+
defer func() {
546+
errRemove := os.Remove(tempfile.Name())
547+
if errRemove != nil {
548+
log.Fatal(errRemove)
549+
}
550+
}()
526551

527552
config, err := vault.LoadConfigFromEnv()
528553
if err != nil {

cmd/main_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ func newConfigFile(t *testing.T, b []byte) string {
2727

2828
func TestExistingAWSProfile(t *testing.T) {
2929
f := newConfigFile(t, defaultConfig)
30-
defer os.Remove(f)
30+
defer func() {
31+
errRemove := os.Remove(f)
32+
assert.NoError(t, errRemove)
33+
}()
3134
config, _ := vault.LoadConfig(f)
3235
baseProfile := vault.Profile{
3336
Name: "test",
@@ -52,7 +55,10 @@ func TestExistingAWSProfile(t *testing.T) {
5255

5356
func TestUpdateAWSConfigFile(t *testing.T) {
5457
f := newConfigFile(t, defaultConfig)
55-
defer os.Remove(f)
58+
defer func() {
59+
errRemove := os.Remove(f)
60+
assert.NoError(t, errRemove)
61+
}()
5662
baseProfile := vault.Profile{
5763
Name: "test-base",
5864
Region: "us-west-2",
@@ -81,7 +87,10 @@ func TestUpdateAWSConfigFile(t *testing.T) {
8187
func TestGenerateQrCode(t *testing.T) {
8288
tempFile, err := ioutil.TempFile("", "temp-qr.*.png")
8389
assert.NoError(t, err)
84-
defer os.Remove(tempFile.Name())
90+
defer func() {
91+
errRemove := os.Remove(tempFile.Name())
92+
assert.NoError(t, errRemove)
93+
}()
8594

8695
err = generateQrCode("otpauth://totp/super@top?secret=secret", tempFile)
8796
assert.NoError(t, err)

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ require (
2020
gopkg.in/go-playground/validator.v9 v9.31.0
2121
gopkg.in/ini.v1 v1.57.0
2222
)
23+
24+
// Update to ignore compiler warnings on macOS catalina
25+
// https://github.com/keybase/go-keychain/pull/55
26+
// https://github.com/99designs/aws-vault/pull/427
27+
replace github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/99designs/aws-vault v1.0.1-0.20191030013236-08380e6561cc h1:LvkmVHxD4e0kBiGmyMIu+IsK9ZIa1Ky9ESI2xmM9IcQ=
22
github.com/99designs/aws-vault v1.0.1-0.20191030013236-08380e6561cc/go.mod h1:uMZxYSIbSixdBvLRO46R1OOeUY+J0crm/3mRsO2v+XU=
3+
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
4+
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
35
github.com/99designs/keyring v1.1.3 h1:mEV3iyZWjkxQ7R8ia8GcG97vCX5zQQ7n4o8R2BylwQY=
46
github.com/99designs/keyring v1.1.3/go.mod h1:657DQuMrBZRtuL/voxVyiyb6zpMehlm5vLB9Qwrv904=
57
github.com/99designs/keyring v1.1.5 h1:wLv7QyzYpFIyMSwOADq1CLTF9KbjbBfcnfmOGJ64aO4=

0 commit comments

Comments
 (0)