Commit bc69d851 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Clean up sharing of status codes

parent af40facc
package main package main
import ( import (
"../../internal/zipartifacts"
"archive/zip" "archive/zip"
"flag" "flag"
"fmt" "fmt"
...@@ -9,7 +10,6 @@ import ( ...@@ -9,7 +10,6 @@ import (
"os" "os"
) )
const notFound = 2
const progName = "gitlab-zip-cat" const progName = "gitlab-zip-cat"
var Version = "unknown" var Version = "unknown"
...@@ -79,6 +79,6 @@ func fatalError(err error) { ...@@ -79,6 +79,6 @@ func fatalError(err error) {
} }
func exitNotFound() { func exitNotFound() {
fmt.Printf("%d\n", -notFound) // for the content-length reader fmt.Printf("%d\n", -zipartifacts.StatusEntryNotFound) // for the content-length reader
os.Exit(notFound) os.Exit(zipartifacts.StatusEntryNotFound)
} }
package main package main
import ( import (
"../../internal/zipmetadata" "../../internal/zipartifacts"
"flag" "flag"
"fmt" "fmt"
"os" "os"
...@@ -26,10 +26,10 @@ func main() { ...@@ -26,10 +26,10 @@ func main() {
fmt.Fprintf(os.Stderr, "Usage: %s FILE.ZIP", progName) fmt.Fprintf(os.Stderr, "Usage: %s FILE.ZIP", progName)
os.Exit(1) 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) fmt.Fprintf(os.Stderr, "%s: %v\n", progName, err)
if err == os.ErrInvalid { if err == os.ErrInvalid {
os.Exit(zipmetadata.StatusNotZip) os.Exit(zipartifacts.StatusNotZip)
} }
os.Exit(1) os.Exit(1)
} }
......
...@@ -3,6 +3,7 @@ package artifacts ...@@ -3,6 +3,7 @@ package artifacts
import ( import (
"../api" "../api"
"../helper" "../helper"
"../zipartifacts"
"bufio" "bufio"
"encoding/base64" "encoding/base64"
"errors" "errors"
...@@ -17,9 +18,7 @@ import ( ...@@ -17,9 +18,7 @@ import (
"syscall" "syscall"
) )
const exitStatusNotFound = 2 var notFoundString = fmt.Sprintf("%d", -zipartifacts.StatusEntryNotFound)
var notFoundString = fmt.Sprintf("%d", -exitStatusNotFound)
func decodeFileEntry(entry string) (string, error) { func decodeFileEntry(entry string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(entry) decoded, err := base64.StdEncoding.DecodeString(entry)
...@@ -72,7 +71,7 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou ...@@ -72,7 +71,7 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou
} }
if err := catFile.Wait(); err != nil { 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 return os.ErrNotExist
} }
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"../api" "../api"
"../helper" "../helper"
"../upload" "../upload"
"../zipmetadata" "../zipartifacts"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -49,7 +49,7 @@ func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer ...@@ -49,7 +49,7 @@ func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer
} }
defer helper.CleanUpProcessGroup(zipMd) defer helper.CleanUpProcessGroup(zipMd)
if err := zipMd.Wait(); err != nil { 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 nil
} }
return err return err
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"../helper" "../helper"
"../proxy" "../proxy"
"../testhelper" "../testhelper"
"../zipmetadata" "../zipartifacts"
"archive/zip" "archive/zip"
"bytes" "bytes"
"compress/gzip" "compress/gzip"
...@@ -72,7 +72,7 @@ func testArtifactsUploadServer(t *testing.T, tempPath string) *httptest.Server { ...@@ -72,7 +72,7 @@ func testArtifactsUploadServer(t *testing.T, tempPath string) *httptest.Server {
w.WriteHeader(404) w.WriteHeader(404)
return return
} }
if !bytes.HasPrefix(metadata, []byte(zipmetadata.HeaderPrefix+zipmetadata.Header)) { if !bytes.HasPrefix(metadata, []byte(zipartifacts.MetadataHeaderPrefix+zipartifacts.MetadataHeader)) {
w.WriteHeader(400) w.WriteHeader(400)
return 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 ( import (
"archive/zip" "archive/zip"
...@@ -19,9 +19,8 @@ type metadata struct { ...@@ -19,9 +19,8 @@ type metadata struct {
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
} }
const HeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly const MetadataHeaderPrefix = "\x00\x00\x00&" // length of string below, encoded properly
const Header = "GitLab Build Artifacts Metadata 0.0.2\n" const MetadataHeader = "GitLab Build Artifacts Metadata 0.0.2\n"
const StatusNotZip = 2
func newMetadata(file *zip.File) metadata { func newMetadata(file *zip.File) metadata {
return metadata{ return metadata{
...@@ -57,7 +56,7 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error { ...@@ -57,7 +56,7 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
} }
func generateZipMetadata(output io.Writer, archive *zip.Reader) error { func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
err := writeString(output, Header) err := writeString(output, MetadataHeader)
if err != nil { if err != nil {
return err 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