Commit bc69d851 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Clean up sharing of status codes

parent af40facc
package main
import (
"../../internal/zipartifacts"
"archive/zip"
"flag"
"fmt"
......@@ -9,7 +10,6 @@ import (
"os"
)
const notFound = 2
const progName = "gitlab-zip-cat"
var Version = "unknown"
......@@ -79,6 +79,6 @@ func fatalError(err error) {
}
func exitNotFound() {
fmt.Printf("%d\n", -notFound) // for the content-length reader
os.Exit(notFound)
fmt.Printf("%d\n", -zipartifacts.StatusEntryNotFound) // for the content-length reader
os.Exit(zipartifacts.StatusEntryNotFound)
}
package main
import (
"../../internal/zipmetadata"
"../../internal/zipartifacts"
"flag"
"fmt"
"os"
......@@ -26,10 +26,10 @@ func main() {
fmt.Fprintf(os.Stderr, "Usage: %s FILE.ZIP", progName)
os.Exit(1)
}
if err := zipmetadata.GenerateZipMetadataFromFile(os.Args[1], os.Stdout); err != nil {
if err := zipartifacts.GenerateZipMetadataFromFile(os.Args[1], os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", progName, err)
if err == os.ErrInvalid {
os.Exit(zipmetadata.StatusNotZip)
os.Exit(zipartifacts.StatusNotZip)
}
os.Exit(1)
}
......
......@@ -3,6 +3,7 @@ package artifacts
import (
"../api"
"../helper"
"../zipartifacts"
"bufio"
"encoding/base64"
"errors"
......@@ -17,9 +18,7 @@ import (
"syscall"
)
const exitStatusNotFound = 2
var notFoundString = fmt.Sprintf("%d", -exitStatusNotFound)
var notFoundString = fmt.Sprintf("%d", -zipartifacts.StatusEntryNotFound)
func decodeFileEntry(entry string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(entry)
......@@ -72,7 +71,7 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou
}
if err := catFile.Wait(); err != nil {
if st, ok := helper.ExitStatus(err); ok && st == exitStatusNotFound {
if st, ok := helper.ExitStatus(err); ok && st == zipartifacts.StatusEntryNotFound {
return os.ErrNotExist
}
......
......@@ -4,7 +4,7 @@ import (
"../api"
"../helper"
"../upload"
"../zipmetadata"
"../zipartifacts"
"errors"
"fmt"
"io/ioutil"
......@@ -49,7 +49,7 @@ func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer
}
defer helper.CleanUpProcessGroup(zipMd)
if err := zipMd.Wait(); err != nil {
if st, ok := helper.ExitStatus(err); ok && st == zipmetadata.StatusNotZip {
if st, ok := helper.ExitStatus(err); ok && st == zipartifacts.StatusNotZip {
return nil
}
return err
......
......@@ -5,7 +5,7 @@ import (
"../helper"
"../proxy"
"../testhelper"
"../zipmetadata"
"../zipartifacts"
"archive/zip"
"bytes"
"compress/gzip"
......@@ -72,7 +72,7 @@ func testArtifactsUploadServer(t *testing.T, tempPath string) *httptest.Server {
w.WriteHeader(404)
return
}
if !bytes.HasPrefix(metadata, []byte(zipmetadata.HeaderPrefix+zipmetadata.Header)) {
if !bytes.HasPrefix(metadata, []byte(zipartifacts.MetadataHeaderPrefix+zipartifacts.MetadataHeader)) {
w.WriteHeader(400)
return
}
......
package zipartifacts
// These are exit codes used by subprocesses in cmd/gitlab-zip-xxx
const (
StatusNotZip = 10 + iota
StatusEntryNotFound
)
package zipmetadata
package zipartifacts
import (
"archive/zip"
......@@ -19,9 +19,8 @@ type metadata struct {
Comment string `json:"comment,omitempty"`
}
const HeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly
const Header = "GitLab Build Artifacts Metadata 0.0.2\n"
const StatusNotZip = 2
const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly
const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n"
func newMetadata(file *zip.File) metadata {
return metadata{
......@@ -57,7 +56,7 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
}
func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
err := writeString(output, Header)
err := writeString(output, MetadataHeader)
if err != nil {
return err
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment