Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Jan 23, 2025
1 parent 1a4dfe5 commit 80f4976
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions cmd/airgap/bundleartifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ import (
"errors"
"fmt"
"io"
"iter"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"syscall"

"github.com/k0sproject/k0s/internal/pkg/file"
Expand Down Expand Up @@ -89,7 +86,7 @@ if no names are given on the command line.`,

var refs []reference.Named
if len(args) > 0 {
refs, err = parseArtifactRefsFromSeq(slices.Values(args))
refs, err = parseArtifactRefs(args)
} else {
refs, err = parseArtifactRefsFromReader(cmd.InOrStdin())
}
Expand All @@ -105,33 +102,33 @@ if no names are given on the command line.`,
},
}

cmd.Flags().StringVarP(&outPath, "output", "o", "", "output file path (writes to standard output if omitted)")
cmd.Flags().Var((*insecureRegistryFlag)(&bundler.InsecureRegistries), "insecure-registries", "one of "+strings.Join(insecureRegistryFlagValues[:], ", "))
cmd.Flags().Var((*platformFlag)(&platform), "platform", "the platform to export")
cmd.Flags().StringArrayVar(&bundler.RegistriesConfigPaths, "registries-config", nil, "paths to the authentication files for OCI registries (uses the standard Docker config if omitted)")
flags := cmd.Flags()
flags.StringVarP(&outPath, "output", "o", "", "output file path (writes to standard output if omitted)")
flags.Var((*insecureRegistryFlag)(&bundler.InsecureRegistries), "insecure-registries", "one of no, skip-tls-verify or plain-http")
flags.Var((*platformFlag)(&platform), "platform", "the platform to export")
flags.StringArrayVar(&bundler.RegistriesConfigPaths, "registries-config", nil, "paths to the authentication files for OCI registries (uses the standard Docker config if omitted)")

return cmd
}

func parseArtifactRefsFromReader(in io.Reader) ([]reference.Named, error) {
words := bufio.NewScanner(in)
words.Split(bufio.ScanWords)
refs, err := parseArtifactRefsFromSeq(func(yield func(string) bool) {
for words.Scan() {
if !yield(words.Text()) {
return
}
}
})
if err := errors.Join(err, words.Err()); err != nil {

var refs []string
for words.Scan() {
refs = append(refs, words.Text())
}
if err := words.Err(); err != nil {
return nil, err
}

return refs, nil
return parseArtifactRefs(refs)
}

func parseArtifactRefsFromSeq(refs iter.Seq[string]) (collected []reference.Named, _ error) {
for ref := range refs {
func parseArtifactRefs(refs []string) ([]reference.Named, error) {
var collected []reference.Named
for _, ref := range refs {
parsed, err := reference.ParseNormalizedNamed(ref)
if err != nil {
return nil, fmt.Errorf("while parsing %s: %w", ref, err)
Expand All @@ -143,32 +140,39 @@ func parseArtifactRefsFromSeq(refs iter.Seq[string]) (collected []reference.Name

type insecureRegistryFlag airgap.InsecureOCIRegistryKind

var insecureRegistryFlagValues = [...]string{
airgap.NoInsecureOCIRegistry: "no",
airgap.SkipTLSVerifyOCIRegistry: "skip-tls-verify",
airgap.PlainHTTPOCIRegistry: "plain-http",
}

func (insecureRegistryFlag) Type() string {
return "string"
}

func (i insecureRegistryFlag) String() string {
if i := int(i); i < len(insecureRegistryFlagValues) {
return insecureRegistryFlagValues[i]
} else {
return strconv.Itoa(i)
switch (airgap.InsecureOCIRegistryKind)(i) {
case airgap.NoInsecureOCIRegistry:
return "no"
case airgap.SkipTLSVerifyOCIRegistry:
return "skip-tls-verify"
case airgap.PlainHTTPOCIRegistry:
return "plain-http"
default:
return strconv.Itoa(int(i))
}
}

func (i *insecureRegistryFlag) Set(value string) error {
idx := slices.Index(insecureRegistryFlagValues[:], value)
if idx >= 0 {
*(*airgap.InsecureOCIRegistryKind)(i) = airgap.InsecureOCIRegistryKind(idx)
return nil
var kind airgap.InsecureOCIRegistryKind

switch value {
case "no":
kind = airgap.NoInsecureOCIRegistry
case "skip-tls-verify":
kind = airgap.SkipTLSVerifyOCIRegistry
case "plain-http":
kind = airgap.PlainHTTPOCIRegistry
default:
return errors.New("must be one of no, skip-tls-verify or plain-http")
}

return errors.New("must be one of " + strings.Join(insecureRegistryFlagValues[:], ", "))
*(*airgap.InsecureOCIRegistryKind)(i) = kind
return nil
}

type platformFlag imagespecv1.Platform
Expand Down

0 comments on commit 80f4976

Please sign in to comment.