Skip to content

Commit

Permalink
Merge pull request #829 from gibmat/fix-ValueInSlice-removal
Browse files Browse the repository at this point in the history
Replace util.ValueInSlice with slices.Contains
  • Loading branch information
stgraber authored Mar 26, 2024
2 parents 93fff48 + eba1eb2 commit a49adf1
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 30 deletions.
6 changes: 3 additions & 3 deletions distrobuilder/main_incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"

client "github.com/lxc/incus/client"
"github.com/lxc/incus/shared/api"
incus "github.com/lxc/incus/shared/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (c *cmdIncus) commandBuild() *cobra.Command {
`, typeDescription, compressionDescription),
Args: cobra.RangeArgs(1, 2),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !incus.ValueInSlice(c.flagType, []string{"split", "unified"}) {
if !slices.Contains([]string{"split", "unified"}, c.flagType) {
return errors.New("--type needs to be one of ['split', 'unified']")
}

Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *cmdIncus) commandPack() *cobra.Command {
`, typeDescription, compressionDescription),
Args: cobra.RangeArgs(2, 3),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !incus.ValueInSlice(c.flagType, []string{"split", "unified"}) {
if !slices.Contains([]string{"split", "unified"}, c.flagType) {
return errors.New("--type needs to be one of ['split', 'unified']")
}

Expand Down
7 changes: 4 additions & 3 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -110,7 +111,7 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
} else {
supportedVersions := []string{"w11", "w10", "2k19", "2k12", "2k16", "2k22"}

if !incus.ValueInSlice(c.flagWindowsVersion, supportedVersions) {
if !slices.Contains(supportedVersions, c.flagWindowsVersion) {
return fmt.Errorf("Version must be one of %v", supportedVersions)
}
}
Expand All @@ -126,7 +127,7 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
} else {
supportedArchitectures := []string{"amd64", "ARM64"}

if !incus.ValueInSlice(c.flagWindowsArchitecture, supportedArchitectures) {
if !slices.Contains(supportedArchitectures, c.flagWindowsArchitecture) {
return fmt.Errorf("Architecture must be one of %v", supportedArchitectures)
}
}
Expand Down Expand Up @@ -554,7 +555,7 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error {
targetPath := filepath.Join(targetBasePath, filepath.Base(path))

// Copy driver files
if incus.ValueInSlice(ext, []string{".cat", ".dll", ".inf", ".sys"}) {
if slices.Contains([]string{".cat", ".dll", ".inf", ".sys"}, ext) {
logger.WithFields(logrus.Fields{"src": path, "dest": targetPath}).Debug("Copying file")

err := shared.Copy(path, targetPath)
Expand Down
3 changes: 2 additions & 1 deletion distrobuilder/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"

Expand All @@ -29,7 +30,7 @@ func newVM(ctx context.Context, imageFile, rootfsDir, fs string, size uint64) (*
fs = "ext4"
}

if !incus.ValueInSlice(fs, []string{"btrfs", "ext4"}) {
if !slices.Contains([]string{"btrfs", "ext4"}, fs) {
return nil, fmt.Errorf("Unsupported fs: %s", fs)
}

Expand Down
3 changes: 2 additions & 1 deletion generators/cloud-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"

"github.com/lxc/incus/shared/api"
Expand All @@ -29,7 +30,7 @@ func (g *cloudInit) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLX
return nil
}

if incus.ValueInSlice(info.Name(), []string{"cloud-init-local", "cloud-config", "cloud-init", "cloud-final"}) {
if slices.Contains([]string{"cloud-init-local", "cloud-config", "cloud-init", "cloud-final"}, info.Name()) {
err := os.Remove(path)
if err != nil {
return fmt.Errorf("Failed to remove file %q: %w", path, err)
Expand Down
7 changes: 3 additions & 4 deletions managers/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"os"
"path/filepath"
"runtime"

incus "github.com/lxc/incus/shared/util"
"slices"

"github.com/lxc/distrobuilder/shared"
)
Expand Down Expand Up @@ -100,7 +99,7 @@ func (m *pacman) setupTrustedKeys() error {

var keyring string

if incus.ValueInSlice(runtime.GOARCH, []string{"arm", "arm64"}) {
if slices.Contains([]string{"arm", "arm64"}, runtime.GOARCH) {
keyring = "archlinuxarm"
} else {
keyring = "archlinux"
Expand All @@ -124,7 +123,7 @@ func (m *pacman) setMirrorlist() error {

var mirror string

if incus.ValueInSlice(runtime.GOARCH, []string{"arm", "arm64"}) {
if slices.Contains([]string{"arm", "arm64"}, runtime.GOARCH) {
mirror = "Server = http://mirror.archlinuxarm.org/$arch/$repo"
} else {
mirror = "Server = http://mirrors.kernel.org/archlinux/$repo/os/$arch"
Expand Down
20 changes: 10 additions & 10 deletions shared/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"time"

"github.com/lxc/incus/shared/osarch"
incusArch "github.com/lxc/incus/shared/osarch"
"github.com/lxc/incus/shared/util"
)

// ImageTarget represents the image target.
Expand Down Expand Up @@ -386,7 +386,7 @@ func (d *Definition) Validate() error {
"nixos-http",
}

if !util.ValueInSlice(strings.TrimSpace(d.Source.Downloader), validDownloaders) {
if !slices.Contains(validDownloaders, strings.TrimSpace(d.Source.Downloader)) {
return fmt.Errorf("source.downloader must be one of %v", validDownloaders)
}

Expand All @@ -407,7 +407,7 @@ func (d *Definition) Validate() error {
"slackpkg",
}

if !util.ValueInSlice(strings.TrimSpace(d.Packages.Manager), validManagers) {
if !slices.Contains(validManagers, strings.TrimSpace(d.Packages.Manager)) {
return fmt.Errorf("packages.manager must be one of %v", validManagers)
}

Expand Down Expand Up @@ -453,7 +453,7 @@ func (d *Definition) Validate() error {
}

for _, file := range d.Files {
if !util.ValueInSlice(strings.TrimSpace(file.Generator), validGenerators) {
if !slices.Contains(validGenerators, strings.TrimSpace(file.Generator)) {
return fmt.Errorf("files.*.generator must be one of %v", validGenerators)
}
}
Expand All @@ -474,7 +474,7 @@ func (d *Definition) Validate() error {

architectureMap := strings.TrimSpace(d.Mappings.ArchitectureMap)
if architectureMap != "" {
if !util.ValueInSlice(architectureMap, validMappings) {
if !slices.Contains(validMappings, architectureMap) {
return fmt.Errorf("mappings.architecture_map must be one of %v", validMappings)
}
}
Expand All @@ -487,7 +487,7 @@ func (d *Definition) Validate() error {
}

for _, action := range d.Actions {
if !util.ValueInSlice(action.Trigger, validTriggers) {
if !slices.Contains(validTriggers, action.Trigger) {
return fmt.Errorf("actions.*.trigger must be one of %v", validTriggers)
}
}
Expand All @@ -498,7 +498,7 @@ func (d *Definition) Validate() error {
}

for _, set := range d.Packages.Sets {
if !util.ValueInSlice(set.Action, validPackageActions) {
if !slices.Contains(validPackageActions, set.Action) {
return fmt.Errorf("packages.*.set.*.action must be one of %v", validPackageActions)
}
}
Expand Down Expand Up @@ -643,15 +643,15 @@ func getFieldByTag(v reflect.Value, t reflect.Type, tag string) (reflect.Value,

// ApplyFilter returns true if the filter matches.
func ApplyFilter(filter Filter, release string, architecture string, variant string, targetType DefinitionFilterType, acceptedImageTargets ImageTarget) bool {
if len(filter.GetReleases()) > 0 && !util.ValueInSlice(release, filter.GetReleases()) {
if len(filter.GetReleases()) > 0 && !slices.Contains(filter.GetReleases(), release) {
return false
}

if len(filter.GetArchitectures()) > 0 && !util.ValueInSlice(architecture, filter.GetArchitectures()) {
if len(filter.GetArchitectures()) > 0 && !slices.Contains(filter.GetArchitectures(), architecture) {
return false
}

if len(filter.GetVariants()) > 0 && !util.ValueInSlice(variant, filter.GetVariants()) {
if len(filter.GetVariants()) > 0 && !slices.Contains(filter.GetVariants(), variant) {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"
"time"

"github.com/flosch/pongo2/v4"
"github.com/lxc/incus/shared/util"
"golang.org/x/sys/unix"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -135,7 +135,7 @@ func compressTarball(ctx context.Context, filename, compression string) (string,
}

// If supported, use as many threads as possible.
if util.ValueInSlice(compression, []string{"zstd", "xz", "lzma"}) {
if slices.Contains([]string{"zstd", "xz", "lzma"}, compression) {
args = append(args, "--threads=0")
}

Expand Down Expand Up @@ -385,7 +385,7 @@ func ParseSquashfsCompression(compression string) (string, *int, error) {
compression = "lzo"
}

if util.ValueInSlice(compression, []string{"gzip", "lzo", "lz4", "xz", "zstd", "lzma"}) {
if slices.Contains([]string{"gzip", "lzo", "lz4", "xz", "zstd", "lzma"}, compression) {
return compression, nil, nil
}

Expand Down
3 changes: 2 additions & 1 deletion sources/debootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strings"

incus "github.com/lxc/incus/shared/util"
Expand All @@ -24,7 +25,7 @@ func (s *debootstrap) Run() error {
release := strings.ToLower(s.definition.Image.Release)

// Enable merged /usr by default, and disable it for certain distros/releases
if distro == "ubuntu" && incus.ValueInSlice(release, []string{"xenial", "bionic", "noble"}) || distro == "debian" && incus.ValueInSlice(release, []string{"sid"}) || distro == "mint" && incus.ValueInSlice(release, []string{"tara", "tessa", "tina", "tricia", "ulyana"}) || distro == "devuan" {
if distro == "ubuntu" && slices.Contains([]string{"xenial", "bionic", "noble"}, release) || distro == "debian" && slices.Contains([]string{"sid"}, release) || distro == "mint" && slices.Contains([]string{"tara", "tessa", "tina", "tricia", "ulyana"}, release) || distro == "devuan" {
args = append(args, "--no-merged-usr")
} else {
args = append(args, "--merged-usr")
Expand Down
4 changes: 2 additions & 2 deletions sources/plamolinux-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"net/url"
"path"
"path/filepath"
"slices"
"strconv"
"strings"

incus "github.com/lxc/incus/shared/util"
"gopkg.in/antchfx/htmlquery.v1"

"github.com/lxc/distrobuilder/shared"
Expand Down Expand Up @@ -152,7 +152,7 @@ func (s *plamolinux) downloadFiles(def shared.DefinitionImage, URL string, ignor

if strings.HasSuffix(target, ".txz") || strings.HasSuffix(target, ".tzst") {
pkgName := strings.Split(target, "-")[0]
if incus.ValueInSlice(pkgName, ignoredPkgs) {
if slices.Contains(ignoredPkgs, pkgName) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions sources/slackware-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net/url"
"path"
"path/filepath"
"slices"
"strings"

incus "github.com/lxc/incus/shared/util"
"gopkg.in/antchfx/htmlquery.v1"

"github.com/lxc/distrobuilder/shared"
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *slackware) downloadFiles(def shared.DefinitionImage, URL string, requir
pkgName := strings.Split(target, "-")[0]
twoPkgName := strings.Split(target, "-")[0] + "-" + strings.Split(target, "-")[1]

if !((incus.ValueInSlice(pkgName, requiredPkgs)) || (incus.ValueInSlice(twoPkgName, requiredPkgs))) {
if !((slices.Contains(requiredPkgs, pkgName)) || (slices.Contains(requiredPkgs, twoPkgName))) {
continue
}

Expand Down

0 comments on commit a49adf1

Please sign in to comment.