File: main.go

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (106 lines) | stat: -rw-r--r-- 2,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Package main provides a utility for extracting and displaying files from a ZIP archive.
package main

import (
	"archive/zip"
	"context"
	"errors"
	"flag"
	"fmt"
	"io"
	"os"

	"gitlab.com/gitlab-org/labkit/mask"

	"gitlab.com/gitlab-org/gitlab/workhorse/internal/zipartifacts"
)

const progName = "gitlab-zip-cat"

// Version holds the version of the program, which is set during the build process.
var Version = "unknown"

var printVersion = flag.Bool("version", false, "Print version and exit")

func main() {
	flag.Parse()

	if *printVersion {
		fmt.Printf("%s %s\n", progName, Version)
		os.Exit(0)
	}

	contextErr, statusErr := run()
	if contextErr != nil && statusErr == nil {
		fmt.Fprintln(os.Stderr, statusErr)
		os.Exit(1)
	}

	if contextErr != nil && statusErr != nil {
		fatalError(contextErr, statusErr)
	}
}

func run() (error, error) {
	archivePath := os.Getenv("ARCHIVE_PATH")
	encodedFileName := os.Getenv("ENCODED_FILE_NAME")

	if len(os.Args) != 1 || archivePath == "" || encodedFileName == "" {
		return fmt.Errorf("usage: %s\nEnv: ARCHIVE_PATH=https://path.to/archive.zip or /path/to/archive.zip\nEnv: ENCODED_FILE_NAME=base64-encoded-file-name", progName), nil
	}

	scrubbedArchivePath := mask.URL(archivePath)

	fileName, err := zipartifacts.DecodeFileEntry(encodedFileName)
	if err != nil {
		return fmt.Errorf("decode entry %q", encodedFileName), err
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	archive, err := zipartifacts.OpenArchive(ctx, archivePath)
	if err != nil {
		return errors.New("open archive"), err
	}

	file := findFileInZip(fileName, archive)
	if file == nil {
		return fmt.Errorf("find %q in %q: not found", fileName, scrubbedArchivePath), zipartifacts.ErrorCode[zipartifacts.CodeEntryNotFound]
	}
	// Start decompressing the file
	reader, err := file.Open()
	if err != nil {
		return fmt.Errorf("open %q in %q", fileName, scrubbedArchivePath), err
	}
	defer reader.Close() //nolint:errcheck

	if _, err := fmt.Printf("%d\n", file.UncompressedSize64); err != nil {
		return fmt.Errorf("write file size invalid"), err
	}

	if _, err := io.Copy(os.Stdout, reader); err != nil {
		return fmt.Errorf("write %q from %q to stdout", fileName, scrubbedArchivePath), err
	}
	return nil, nil
}

func findFileInZip(fileName string, archive *zip.Reader) *zip.File {
	for _, file := range archive.File {
		if file.Name == fileName {
			return file
		}
	}
	return nil
}

func fatalError(contextErr error, statusErr error) {
	code := zipartifacts.ExitCodeByError(statusErr)

	fmt.Fprintf(os.Stderr, "%s error: %v - %v, code: %d\n", progName, statusErr, contextErr, code)

	if code > 0 {
		os.Exit(code)
	}
	os.Exit(1)
}