diff --git a/.drone.yml b/.drone.yml index b377ea8..1e5495a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -13,13 +13,12 @@ steps: - pull_request - tag -# steps: -# - name: test-unit -# image: docker.io/volkerraschek/build-image:latest -# commands: -# - make test/unit -# when: -# event: -# - push -# - pull_request -# - tag \ No newline at end of file +- name: test-unit + image: docker.io/volkerraschek/build-image:latest + commands: + - make test/unit + when: + event: + - push + - pull_request + - tag \ No newline at end of file diff --git a/.gitignore b/.gitignore index c5e82d7..43e5476 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -bin \ No newline at end of file +bin +coverage.txt \ No newline at end of file diff --git a/Makefile b/Makefile index 1ecaee0..13779b9 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,15 @@ PHONY+=clean clean: -rm --force --recursive bin/ +# TEST +# ============================================================================== +PHONY+=test/unit +test/unit: + go test -v -race -coverprofile=coverage.txt -covermode=atomic -timeout 600s -count=1 ./... + +test/coverage: test/unit + go tool cover -html=coverage.txt + # PHONY # ============================================================================== # Declare the contents of the PHONY variable as phony. We keep that information diff --git a/go.mod b/go.mod index d5fda50..c95b914 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.15 require ( git.cryptic.systems/volker.raschek/go-logger v0.1.0 github.com/spf13/cobra v1.0.0 + github.com/stretchr/testify v1.6.1 ) diff --git a/go.sum b/go.sum index a52fb95..8d8cfb9 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -59,6 +60,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= @@ -87,6 +89,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= @@ -130,5 +133,6 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index 1f5e6ed..6a81205 100644 --- a/main.go +++ b/main.go @@ -127,14 +127,20 @@ Loop: } func removeSSHKeys(sshKeys []*sshKey, removeSSHKeys []*sshKey) []*sshKey { - for i := range removeSSHKeys { - for j := range sshKeys { - if sshKeys[j].Compare(removeSSHKeys[i]) { - sshKeys = append(sshKeys[:j], sshKeys[j+1:]...) + + s := make([]*sshKey, 0) + +Loop: + for i := range sshKeys { + for j := range removeSSHKeys { + if sshKeys[i].Compare(removeSSHKeys[j]) { + continue Loop } } + s = append(s, sshKeys[i]) } - return sshKeys + + return s } func rootCmd(cmd *cobra.Command, args []string) error { @@ -210,11 +216,7 @@ func writeSSHKeys(authorizedKeyFile string, sshKeys []*sshKey) error { } for i := range sshKeys { - if len(sshKeys[i].alias) > 0 { - fmt.Fprintf(f, "%v %v %v\n", sshKeys[i].algorithm, sshKeys[i].pubKey, sshKeys[i].alias) - } else { - fmt.Fprintf(f, "%v %v\n", sshKeys[i].algorithm, sshKeys[i].pubKey) - } + fmt.Fprintln(f, sshKeys[i].String()) } return nil @@ -240,6 +242,14 @@ func (s *sshKey) SetAlias(alias string) { } } +func (s *sshKey) String() string { + l := fmt.Sprintf("%v %v", s.algorithm, s.pubKey) + if len(s.alias) > 0 { + l = fmt.Sprintf("%v %v", l, s.alias) + } + return l +} + func (s *sshKey) Validate() error { entries := map[string]string{ "algorithm": s.algorithm, diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..92737e7 --- /dev/null +++ b/main_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestRemove(t *testing.T) { + require := require.New(t) + + s := []*sshKey{ + {algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"}, + {algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"}, + } + + r := []*sshKey{ + {algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"}, + } + + result := removeSSHKeys(s, r) + require.Len(result, 1) + + r = []*sshKey{ + {algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"}, + {algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"}, + } + + result = removeSSHKeys(s, r) + require.Len(result, 0) + + r = []*sshKey{ + {algorithm: "ssh-ed25519", pubKey: "sdfsdf"}, + } + + result = removeSSHKeys(s, r) + require.Len(result, 1) +} + +func TestSSHKeyString(t *testing.T) { + require := require.New(t) + + s := []*sshKey{ + {algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"}, + {algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"}, + {algorithm: "ssh-rsa", pubKey: "asdfasdsdfsfadsad"}, + } + + b := []string{ + "ssh-ed25519 sdfsdf hello@world", + "ssh-rsa asdfasdadsad world@hello", + "ssh-rsa asdfasdsdfsfadsad", + } + + for i := range s { + require.Equal(b[i], s[i].String()) + } +}