Files
act_runner/act/container/volume_spec.go
silverwind 4158e05dae refactor: drop logrus and other direct dependencies (#1319)
After https://gitea.com/gitea/runner/pulls/1304, logrus stayed only to forward logs from docker/cli and moby/go-archive. The runner used a small part of those: volume parsing, the build context tar, the `ssh://` connection and registry credentials. Own code that matches them replaces them, so logrus leaves the module graph. docker/cli stays for its logrus-free `opts` and `connhelper/ssh` packages.

Other dependencies that the standard library or existing ones cover go too: mergo, httprouter (replaced by `http.ServeMux`), retry-go, go-isatty, go-selinux, container-device-interface, gotest.tools and go-cmp (replaced by testify). The binary shrinks by 5%.

Behaviour differences:

1. An unreadable file in a build context is skipped with a warning, where go-archive left a truncated tar.
2. Build contexts no longer carry `security.capability` xattrs.
3. The cache and artifact servers answer HEAD on GET routes, and no longer redirect trailing slashes or answer OPTIONS.

*Written by Claude.*

---------

Co-authored-by: bircni <bircni@icloud.com>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1319
Reviewed-by: bircni <bircni@icloud.com>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <2021+silverwind@noreply.gitea.com>
2026-10-18 18:57:08 +00:00

86 lines
2.0 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package container
import (
"errors"
"fmt"
"slices"
"strings"
"unicode"
"unicode/utf8"
"github.com/moby/moby/api/types/mount"
)
type VolumeSpec struct {
Type mount.Type
Source string
Target string
ReadOnly bool
NoCopy bool
}
// ParseVolume parses spec like `docker run -v` does, without knowing the target platform.
func ParseVolume(spec string) (VolumeSpec, error) {
switch len(spec) {
case 0:
return VolumeSpec{}, errors.New("invalid empty volume spec")
case 1, 2:
return VolumeSpec{Type: mount.TypeVolume, Target: spec}, nil
}
spec = string([]rune(spec)) // each invalid UTF-8 byte becomes U+FFFD, as in the JSON the daemon receives
var parts []string
for rest := spec; ; {
start := 0
if hasDriveLetter(rest) {
start = strings.IndexByte(rest, ':') + 1
}
end := strings.IndexByte(rest[start:], ':')
if end < 0 {
parts = append(parts, rest)
break
}
parts = append(parts, rest[:start+end])
rest = rest[start+end+1:]
}
if len(parts) > 3 {
return VolumeSpec{}, fmt.Errorf("invalid spec: %s: too many colons", spec)
}
if slices.Contains(parts, "") {
return VolumeSpec{}, fmt.Errorf("invalid spec: %s: empty section between colons", spec)
}
volume := VolumeSpec{Type: mount.TypeVolume, Target: parts[0]}
if len(parts) > 1 {
volume.Source, volume.Target = parts[0], parts[1]
if isHostPath(volume.Source) {
volume.Type = mount.TypeBind
}
}
if len(parts) == 3 {
for option := range strings.SplitSeq(parts[2], ",") {
switch option {
case "ro":
volume.ReadOnly = true
case "rw":
volume.ReadOnly = false
case "nocopy":
volume.NoCopy = true
}
}
}
return volume, nil
}
func hasDriveLetter(s string) bool {
letter, size := utf8.DecodeRuneInString(s)
return size < len(s) && s[size] == ':' && unicode.IsLetter(letter)
}
func isHostPath(source string) bool {
return strings.ContainsAny(source[:1], "./~") || strings.HasPrefix(source, `\\`) || hasDriveLetter(source)
}