Skip to content

Commit

Permalink
error refactoring. Close #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Fritz committed Feb 22, 2022
1 parent 0966cb9 commit 66d1735
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 103 deletions.
114 changes: 114 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
all:
go build
test:
go test -v
rm -rf testdata/bag_golden
prod:
go build -ldflags "-w -s"

.PHONY: test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Wikipedia: [https://en.wikipedia.org/wiki/BagIt](https://en.wikipedia.org/wiki/B
IETF: [https://tools.ietf.org/html/rfc8493](https://tools.ietf.org/html/rfc8493)


[![Build Status](https://travis-ci.org/steffenfritz/bagit.svg?branch=master)](https://travis-ci.org/steffenfritz/bagit)
[![Build status](https://ci.appveyor.com/api/projects/status/vscholjbph8umbd3?svg=true)](https://ci.appveyor.com/project/steffenfritz/bagit)
[![codecov](https://codecov.io/gh/steffenfritz/bagit/branch/master/graph/badge.svg)](https://codecov.io/gh/steffenfritz/bagit)
[![Go Report Card](https://goreportcard.com/badge/github.com/steffenfritz/bagit)](https://goreportcard.com/report/github.com/steffenfritz/bagit)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=steffenfritz_bagit&metric=alert_status)](https://sonarcloud.io/dashboard?id=steffenfritz_bagit)

Version: 0.4.0
Version: 0.5.0

# Usage examples

Expand Down
31 changes: 15 additions & 16 deletions cmd/gobagit/gobagit.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Copyright (c) 2019, Steffen Fritz
.\" Copyright (c) 2019-2022, Steffen Fritz
.\"
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
.\" This is free documentation; you can redistribute it and/or
Expand All @@ -21,7 +21,7 @@
.\" <http://www.gnu.org/licenses/>.
.\" %%%LICENSE_END

.TH gobagit 1 "July 2019" "version 0.4.0"
.TH gobagit 1 "March 2022" "version 0.5.0"
.SH NAME
gobagit
.SH SYNOPSIS
Expand All @@ -37,39 +37,38 @@ Bags are ideal for digital content normally kept as a collection of files. They

.SH FLAGS

.BR \-create\fR
.BR \--create,\ -C\fR
Create bag. Expects path to source directory

.BR \-fetch\fR
.BR \--fetch,\ -F\fR
Adds optional fetch file to bag. Expects path to fetch.txt file and flag manifetch

.BR \-hash\fR
.BR \--hash,\ -H\fR
Hash algorithm used for manifest file when creating a bag

.BR \-header\fR
.BR \--header,\ -J\fR
Additional headers for bag-info.txt. Expects path to json file

.BR \-manifetch\fR
.BR \--manifetch,\ -M\fR
Path to manifest file for optional fetch.txt file. Mandatory if fetch switch is used

.BR \-output\fR
.BR \--output,\ -O\fR
Output directory for bag. Used with create flag

.BR \-tagmanifest\fR
.BR \--tagmanifest,\ -t\fR
Hash algorithm used for tag manifest file

.BR \-tar\fR
.BR \--tar,\ -T\fR
Create a tar archive when creating a bag

.BR \-v\fR
Verbose output

.BR \-validate\fR
.BR \--validate,\ -V\fR
Validate bag. Expects path to bag

.BR \-version\fR
Print version
.BR \--verbose,\ -v\fR
Verbose output

.BR \--version\fR
Print version


.SH AUTHOR
Expand Down
37 changes: 21 additions & 16 deletions cmd/gobagit/main.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package main

import (
"flag"
"log"
"os"
"time"

flag "github.com/spf13/pflag"
"github.com/steffenfritz/bagit"
)

const version = "0.4.0"
var Version string
var Build string

var starttime = time.Now().Format("2006-01-02T150405")

func main() {
b := bagit.New()

vers := flag.Bool("version", false, "Print version")
validate := flag.String("validate", "", "Validate bag. Expects path to bag")
b.SrcDir = flag.String("create", "", "Create bag. Expects path to source directory")
b.OutDir = flag.String("output", "bag_"+starttime, "Output directory for bag. Used with create flag")
tarit := flag.Bool("tar", false, "Create a tar archive when creating a bag")
b.HashAlg = flag.String("hash", "sha512", "Hash algorithm used for manifest file when creating a bag [sha1, sha256, sha512, md5]")
verbose := flag.Bool("v", false, "Verbose output")
b.AddHeader = flag.String("header", "", "Additional headers for bag-info.txt. Expects path to json file")
b.FetchFile = flag.String("fetch", "", "Adds optional fetch file to bag. Expects path to fetch.txt file and switch manifetch")
b.FetchManifest = flag.String("manifetch", "", "Path to manifest file for optional fetch.txt file. Mandatory if fetch switch is used")
b.TagManifest = flag.String("tagmanifest", "", "Hash algorithm used for tag manifest file [sha1, sha256, sha512, md5]")
vers := flag.BoolP("version", "", false, "Print version")
validate := flag.StringP("validate", "V", "", "Validate bag. Expects path to bag")
b.SrcDir = flag.StringP("create", "C", "", "Create bag. Expects path to source directory")
b.OutDir = flag.StringP("output", "O", "bag_"+starttime, "Output directory for bag. Used with create flag")
tarit := flag.BoolP("tar", "T", false, "Create a tar archive when creating a bag")
b.HashAlg = flag.StringP("hash", "H", "sha512", "Hash algorithm used for manifest file when creating a bag [sha1, sha256, sha512, md5]")
verbose := flag.BoolP("verbose", "v", false, "Verbose output")
b.AddHeader = flag.StringP("header", "J", "", "Additional headers for bag-info.txt. Expects path to json file")
b.FetchFile = flag.StringP("fetch", "F", "", "Adds optional fetch file to bag. Expects path to fetch.txt file and switch manifetch")
b.FetchManifest = flag.StringP("manifetch", "M", "", "Path to manifest file for optional fetch.txt file. Mandatory if fetch switch is used")
b.TagManifest = flag.StringP("tagmanifest", "t", "", "Hash algorithm used for tag manifest file [sha1, sha256, sha512, md5]")

flag.Parse()

if *vers {
log.Println("Version: " + version)
log.Println("Version: " + Version + " Build: " + Build)

return
}
Expand Down Expand Up @@ -98,10 +99,14 @@ func main() {

b.Oxum.Bytes = int64(fetchoxumbytes)
b.Oxum.Filecount = fetchoxumfiles
b.Create(*verbose)
err = b.Create(*verbose)
if err != nil {
log.Fatalf("ERROR: %s", err.Error())
}

if *tarit {
b.Tarit(*b.OutDir, *b.OutDir+".tar.gz")
err = b.Tarit(*b.OutDir, *b.OutDir+".tar.gz")
log.Fatalf("ERROR: %s", err.Error())
}

return
Expand Down
27 changes: 22 additions & 5 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@ package bagit
import (
"fmt"
"io"
"log"
"os"
)

func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
e(err)
if err != nil {
log.Fatalf("ERROR: %s", err.Error())
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)
e(err)
defer source.Close()
if err != nil {
log.Fatalf("ERROR: %s", err.Error())
}
defer func(source *os.File) {
err := source.Close()
if err != nil {
log.Printf("WARNING: %s", err.Error())
}
}(source)

destination, err := os.Create(dst)
e(err)
defer destination.Close()
if err != nil {
log.Fatalf("ERROR: %s", err.Error())
}
defer func(destination *os.File) {
err := destination.Close()
if err != nil {
log.Printf("WARNING: %s", err.Error())
}
}(destination)

nBytes, err := io.Copy(destination, source)
return nBytes, err
Expand Down
Loading

0 comments on commit 66d1735

Please sign in to comment.