github.com/pkalwak/bagins@v0.0.0-20210317172317-694ac5ce2f54/bagmaker/bagmaker.go (about)

     1  package main
     2  
     3  //
     4  //
     5  // This application can be compiled and deployed as a stand alone executable to
     6  // create very basic bags from the commandline.
     7  //
     8  //
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"github.com/pkalwak/bagins"
    14  	"strings"
    15  	"time"
    16  )
    17  
    18  var (
    19  	dir          string
    20  	name         string
    21  	payload      string
    22  	algo         string
    23  	tagmanifests string
    24  )
    25  
    26  func init() {
    27  	flag.StringVar(&dir, "dir", "", "Directory to create the bag.")
    28  	flag.StringVar(&name, "name", "", "Name for the bag root directory.")
    29  	flag.StringVar(&payload, "payload", "", "Directory of files to parse into the bag")
    30  	flag.StringVar(&algo, "algo", "md5", "Checksum algorithm to use.  md5, sha1, sha224, sha256, sha512, sha384")
    31  	flag.StringVar(&tagmanifests, "tagmanifests", "", "Set to true to create tag manifests. Default is false.")
    32  
    33  	flag.Parse()
    34  }
    35  
    36  func usage() {
    37  
    38  	usage := `
    39  Usage: ./bagmaker -dir <value> -name <value> -payload <value> [-algo <value>]
    40  
    41  Flags:
    42  
    43      -algo <value>
    44       Checksum algorithm to use.  md5, sha1, sha224, sha256,
    45       sha512, or sha384. Defaults to md5. Use commas (without
    46  	 spaces) to specify multiple algorithms. E.g. md5,sha256
    47  
    48      -dir <value>
    49       Directory to create the bag.
    50  
    51      -name <value>
    52       Name for the bag root directory.
    53  
    54      -payload <value>
    55       Directory of files to copy into the bag.
    56  
    57      -tagmanifests <value>
    58       Set to true to create tag manifests. Default is false.
    59  
    60       Example:
    61  
    62       Put all of /home/joe into a bag called joes_bag in the current working
    63       directory. Create payload manifests with md5 and sha256 checksums, and
    64       create tagmanifests as well (using the same checksum algorithms).
    65  
    66       bagmaker -payload /home/joe -name joes_bag -dir . -algo md5,sha256 -tagmanifests true
    67  
    68  `
    69  	fmt.Println(usage)
    70  }
    71  
    72  func main() {
    73  
    74  	if dir == "" {
    75  		usage()
    76  		return
    77  	}
    78  	if name == "" {
    79  		usage()
    80  		return
    81  	}
    82  	if payload == "" {
    83  		usage()
    84  		return
    85  	}
    86  
    87  	algoList := parseAlgorithms(algo)
    88  
    89  	createTagManifests := false
    90  	if tagmanifests == "true" {
    91  		createTagManifests = true
    92  	}
    93  
    94  	begin := time.Now()
    95  
    96  	bag, err := bagins.NewBag(dir, name, algoList, createTagManifests)
    97  	if err != nil {
    98  		fmt.Println("Bag Error:", err)
    99  		return
   100  	}
   101  
   102  	errs := bag.AddDir(payload)
   103  	for idx := range errs {
   104  		fmt.Println("AddDir Error:", errs[idx])
   105  		return
   106  	}
   107  
   108  	bag.Save()
   109  
   110  	elapsed := time.Since(begin)
   111  	fmt.Println("END: elapsed in", elapsed.Seconds(), "seconds.")
   112  	return
   113  }
   114  
   115  // Parses command line arguments to go into the
   116  
   117  // func parse_info(args []string) map[string]string {
   118  // 	info := make(map[string]string)
   119  // 	return info
   120  // }
   121  
   122  func parseAlgorithms(algo string) (algorithms []string) {
   123  	if algo == "" {
   124  		algorithms = []string{"md5"}
   125  	} else {
   126  		algorithms = strings.Split(algo, ",")
   127  	}
   128  	return algorithms
   129  }