You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mir/zip.go

100 lines
2.4 KiB
Go

3 years ago
package main
import (
"bytes"
3 years ago
"flag"
"fmt"
3 years ago
"io/ioutil"
"os"
"path/filepath"
"strings"
3 years ago
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/zip"
)
func modbasename(path string) string {
parts := strings.Split(path, "/")
return parts[len(parts)-1]
}
3 years ago
func zipcmd(args []string) {
var (
version string
outputPath string
)
flags := flag.NewFlagSet("zip", flag.ExitOnError)
flags.StringVar(&version, "version", "", "package version")
flags.StringVar(&outputPath, "o", "", "output file path")
3 years ago
flags.Parse(args)
3 years ago
if version == "" {
bail(1, "target release version is required")
}
pkgdir := flags.Arg(0)
if pkgdir == "" {
pkgdir = "."
}
3 years ago
modfilePath := filepath.Join(pkgdir, "go.mod")
b, err := ioutil.ReadFile(modfilePath)
if err != nil {
bail(1, "unable to read modfile: %v", err)
}
log_info.Printf("checking modfile at path %s", modfilePath)
3 years ago
f, err := modfile.Parse(modfilePath, b, nil)
if err != nil {
bail(1, "unable to parse modfile: %v", err)
}
3 years ago
modpath := f.Module.Mod.Path
log_info.Print("parsed modfile")
log_info.Printf("module path in modfile: %s", modpath)
log_info.Printf("module major version in modfile: %s", f.Module.Mod.Version)
log_info.Printf("target release version: %s", version)
// major version compatibility check
3 years ago
if err := module.Check(modpath, version); err != nil {
shutdown(err)
}
// default to basename@version.zip
if outputPath == "" {
outputPath = fmt.Sprintf("%s@%s.zip", modbasename(modpath), version)
}
// check that destination is available
log_info.Printf("destination: %s", outputPath)
switch _, err := os.Stat(outputPath); {
case err == nil:
bail(1, "a file at %s already exists", outputPath)
case os.IsNotExist(err):
break
default:
bail(1, "unable to check for file at %s: %v", outputPath, err)
}
// zip into memory
var buf bytes.Buffer
log_info.Printf("constructing zip in memory")
mv := module.Version{Path: modpath, Version: version}
if err := zip.CreateFromDir(&buf, mv, pkgdir); err != nil {
bail(1, "zip not created: %v", err)
3 years ago
}
fout, err := os.OpenFile(outputPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
bail(1, "unable to open output file at path %s: %v", outputPath, err)
3 years ago
}
defer fout.Close()
if _, err := buf.WriteTo(fout); err != nil {
bail(1, "unable to write output file at path %s: %v", outputPath, err)
}
log_info.Printf("wrote archive to %s", outputPath)
3 years ago
}