Skip to content

Commit

Permalink
Merge pull request #146 from 030/136-custom-zip-name
Browse files Browse the repository at this point in the history
[GH-136] Overwrite default zip file name.
  • Loading branch information
030 authored Jul 20, 2020
2 parents d6636d9 + dfeea57 commit 75828a3
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 141 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
None

## [4.1.3] - 2020-07-20
### Added
- Overwrite default zipFileName

## [4.1.2] - 2020-07-19
### Fixed
- Duplicated downloads issue resolved by excluding backup of groups
Expand Down Expand Up @@ -216,7 +220,8 @@ None
### Added
- Download all artifacts from a certain Nexus3 repository.

[Unreleased]: https://github.com/030/n3dr/compare/4.1.2...HEAD
[Unreleased]: https://github.com/030/n3dr/compare/4.1.3...HEAD
[4.1.3]: https://github.com/030/n3dr/compare/4.1.2...4.1.3
[4.1.2]: https://github.com/030/n3dr/compare/4.1.1...4.1.2
[4.1.1]: https://github.com/030/n3dr/compare/4.1.0...4.1.1
[4.1.0]: https://github.com/030/n3dr/compare/4.0.0...4.1.0
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ the `-x` option as well:
n3dr repositories -u admin -n http://localhost:8081 -b -x 'some/group42'
```
## Add all downloaded archives to a ZIP archive
### Add all downloaded archives to a ZIP archive
In order to add all archives to a zip archive, one has to use the --zip or -z flag.
If one would like to overwrite the default zip file name, then one has to use
the `-i` option. Note: the extension '.zip' is obliged.
### Upload all artifacts to a certain repository
It is possible to upload all JARs that reside in a folder by
Expand Down
29 changes: 2 additions & 27 deletions cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/spf13/viper"

"github.com/asaskevich/govalidator"
"github.com/mholt/archiver"
log "github.com/sirupsen/logrus"
"github.com/svenfuchs/jq"
"github.com/thedevsaddam/gojsonq"
Expand All @@ -33,16 +32,6 @@ const (
tmpDir = "/tmp/n3dr"
)

// Nexus3 contains the attributes that are used by several functions
type Nexus3 struct {
URL string
User string
Pass string
Repository string
APIVersion string
ZIP bool
}

func TempDownloadDir() (string, error) {
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
return "", nil
Expand Down Expand Up @@ -147,7 +136,7 @@ func createArtifact(d string, f string, content string, md5sum string) error {

md5sumLocal := ""
if fileExists(filename) {
md5sumLocal, err = HashFileMD5(filename)
md5sumLocal, err = hashFileMD5(filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -290,21 +279,8 @@ func (n Nexus3) StoreArtifactsOnDisk(dir, regex string) error {
return nil
}

// CreateZip adds all artifacts to a ZIP archive
func (n Nexus3) CreateZip(dir string) error {
name := "n3dr-backup-" + time.Now().Format("01-02-2006T15-04-05") + ".zip"
if n.ZIP {
err := archiver.Archive([]string{dir}, name)
if err != nil {
return err
}
}
log.Infof("Zipfile: '%v' created", name)
return nil
}

// HashFileMD5 returns MD5 checksum of a file
func HashFileMD5(filePath string) (string, error) {
func hashFileMD5(filePath string) (string, error) {
var returnMD5String string
file, err := os.Open(filePath)
if err != nil {
Expand All @@ -318,7 +294,6 @@ func HashFileMD5(filePath string) (string, error) {
hashInBytes := hash.Sum(nil)[:16]
returnMD5String = hex.EncodeToString(hashInBytes)
return returnMD5String, nil

}

func fileExists(filename string) bool {
Expand Down
18 changes: 0 additions & 18 deletions cli/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,6 @@ func TestDownloadArtifact(t *testing.T) {
}
}

func TestHashFileMD5(t *testing.T) {
file := testDirHome + testDirDownload + "/file1/file1/1.0.0/file1-1.0.0.jar"
_, actualError := HashFileMD5(file)
expectedError := "open " + testDirHome + testDirDownload + "/file1/file1/1.0.0/file1-1.0.0.jar: no such file or directory"

if actualError.Error() != expectedError {
t.Errorf(errMsgTxt, expectedError, actualError)
}

file = testFileJar100
expectedResult := "ad60407c083b4ecc372614b8fcd9f305"
result, _ := HashFileMD5(file)

if result != expectedResult {
t.Errorf(errMsgTxt, expectedResult, result)
}
}

func TestFileExists(t *testing.T) {
file := "file1/file1/1.0.0/file1-1.0.0.jar"
result := fileExists(file)
Expand Down
28 changes: 28 additions & 0 deletions cli/req.go → cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import (
"io/ioutil"
"net/http"
"strconv"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/mholt/archiver"

log "github.com/sirupsen/logrus"
)

// Nexus3 contains the attributes that are used by several functions
type Nexus3 struct {
URL string
User string
Pass string
Repository string
APIVersion string
ZIP bool
ZipName string
}

func (n Nexus3) validate() {
if n.User == "" {
log.Debug("Empty user. Verify whether the the subcommand is specified or anonymous mode is used")
Expand Down Expand Up @@ -78,3 +91,18 @@ func (n Nexus3) responseBodyString(resp *http.Response) ([]byte, string, error)

return bodyBytes, bodyString, nil
}

// CreateZip adds all artifacts to a ZIP archive
func (n Nexus3) CreateZip(dir string) error {
if n.ZIP {
if n.ZipName == "" {
n.ZipName = "n3dr-backup-" + time.Now().Format("01-02-2006T15-04-05") + ".zip"
}
err := archiver.Archive([]string{dir}, n.ZipName)
if err != nil {
return err
}
log.Infof("Zipfile: '%v' created", n.ZipName)
}
return nil
}
48 changes: 48 additions & 0 deletions cli/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"testing"

"github.com/stretchr/testify/assert"
)

const (
testDirHome = "/tmp/n3drtest"
)

func TestHashFileMD5(t *testing.T) {
file := testDirHome + testDirDownload + "/file1/file1/1.0.0/file1-1.0.0.jar"
_, actualError := hashFileMD5(file)
expectedError := "open " + testDirHome + testDirDownload + "/file1/file1/1.0.0/file1-1.0.0.jar: no such file or directory"

if actualError.Error() != expectedError {
t.Errorf(errMsgTxt, expectedError, actualError)
}

file = testFileJar100
expectedResult := "ad60407c083b4ecc372614b8fcd9f305"
result, _ := hashFileMD5(file)

if result != expectedResult {
t.Errorf(errMsgTxt, expectedResult, result)
}
}
func TestCreateZip(t *testing.T) {
assert.Nil(t, n.CreateZip(testDirHome))
n.ZIP = true
assert.Nil(t, n.CreateZip(testDirHome))
n.ZipName = "notAZip"
assert.EqualError(t, n.CreateZip(testDirHome), "format unrecognized by filename: notAZip")
n.ZIP = false
}

func TestRequest(t *testing.T) {
n.User = ""
n.validate()
n.Pass = ""
n.validate()
_, _, err := n.request("incorrectUrl")
assert.EqualError(t, err, "Get \"incorrectUrl\": Get \"incorrectUrl\": unsupported protocol scheme \"\"")
n.User = "admin"
n.Pass = "admin123"
}
9 changes: 0 additions & 9 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

const (
testDirHome = "/tmp/n3drtest"
testDirDownload = "/download"
testDirUpload = "/testFiles"
testNexusAuthError = "ResponseCode: '401' and Message '401 Unauthorized' for URL: http://localhost:9999/service/rest/v1/repositories"
Expand All @@ -40,14 +39,6 @@ var n = Nexus3{
APIVersion: "v1",
}

var nErrAuth = Nexus3{
URL: "http://localhost:9999",
User: "admin",
Pass: "incorrectPass",
Repository: "maven-releases",
APIVersion: "v1",
}

func setup() {
// Start docker nexus
cmd := exec.Command("bash", "-c", "docker run -d -p 9999:8081 --name nexus sonatype/nexus3:3.16.1")
Expand Down
18 changes: 8 additions & 10 deletions cli/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ func TestRepositoryNamesJSON(t *testing.T) {
}
}

func TestCountRepositories(t *testing.T) {
func TestHappyFlow(t *testing.T) {
assert.Nil(t, n.CountRepositories())
assert.EqualError(t, nErrAuth.CountRepositories(), testNexusAuthError)
}

func TestRepositoryNames(t *testing.T) {
assert.Nil(t, n.RepositoryNames())
assert.EqualError(t, nErrAuth.RepositoryNames(), testNexusAuthError)
}

func TestDownloads(t *testing.T) {
assert.Nil(t, n.Downloads(".*"))
assert.EqualError(t, nErrAuth.Downloads(".*"), testNexusAuthError)
}
func TestUnhappyFlow(t *testing.T) {
n.Pass = "incorrectPass"
assert.EqualError(t, n.CountRepositories(), testNexusAuthError)
assert.EqualError(t, n.RepositoryNames(), testNexusAuthError)
assert.EqualError(t, n.Downloads(".*"), testNexusAuthError)
n.Pass = "admin123"
}
16 changes: 1 addition & 15 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down Expand Up @@ -42,7 +28,7 @@ reside in a certain Nexus3 repository`,
log.Fatal(err)
}

n := cli.Nexus3{URL: n3drURL, User: n3drUser, Pass: viper.GetString("n3drPass"), Repository: n3drRepo, APIVersion: apiVersion, ZIP: zip}
n := cli.Nexus3{URL: n3drURL, User: n3drUser, Pass: viper.GetString("n3drPass"), Repository: n3drRepo, APIVersion: apiVersion, ZIP: zip, ZipName: zipName}
if err := n.StoreArtifactsOnDisk(dir, regex); err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 1 addition & 15 deletions cmd/repositories.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down Expand Up @@ -49,7 +35,7 @@ download artifacts from all repositories`,
os.Exit(0)
}
pw := viper.GetString("n3drPass")
n := cli.Nexus3{URL: n3drURL, User: n3drUser, Pass: pw, APIVersion: apiVersion, ZIP: zip}
n := cli.Nexus3{URL: n3drURL, User: n3drUser, Pass: pw, APIVersion: apiVersion, ZIP: zip, ZipName: zipName}
if names {
if err := n.RepositoryNames(); err != nil {
log.Fatal(err)
Expand Down
19 changes: 3 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand All @@ -27,8 +13,8 @@ import (
)

var (
apiVersion, cfgFile, n3drRepo, n3drURL, n3drUser, Version string
debug, zip, insecureSkipVerify bool
apiVersion, cfgFile, n3drRepo, n3drURL, n3drUser, Version, zipName string
debug, zip, insecureSkipVerify bool
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -57,6 +43,7 @@ func init() {

rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug logging")
rootCmd.PersistentFlags().BoolVarP(&zip, "zip", "z", false, "add downloaded artifacts to a ZIP archive")
rootCmd.PersistentFlags().StringVarP(&zipName, "zipName", "i", "", "the name of the zip file")
rootCmd.PersistentFlags().BoolVar(&insecureSkipVerify, "insecureSkipVerify", false, "Skip repository certificate check")

rootCmd.PersistentFlags().StringP("n3drPass", "p", "", "nexus3 password")
Expand Down
14 changes: 0 additions & 14 deletions cmd/upload.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
Loading

0 comments on commit 75828a3

Please sign in to comment.