Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func newPrintCommand() *cobra.Command {
"Output format. One of: (yaml, json, kyaml).")

cmd.Flags().StringVar(&pr.inputFile, "input-file", "",
`Path to the manifest file. When set, the tool will read ingresses from the file instead of reading from the cluster. Supported files are yaml and json.`)
`Path to the manifest file. Use "-" to read from stdin. When set, the tool will read ingresses from the file instead of reading from the cluster. Supported files are yaml and json.`)

cmd.Flags().StringVarP(&pr.namespace, "namespace", "n", "",
`If present, the namespace scope for this CLI request.`)
Expand Down
34 changes: 31 additions & 3 deletions pkg/i2gw/providers/common/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"sync"

apiv1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand All @@ -34,6 +35,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
stdin []byte
stdinOnce sync.Once
stdinErr error
)

func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingressClasses sets.Set[string]) (map[types.NamespacedName]*networkingv1.Ingress, error) {
var ingressList networkingv1.IngressList
err := client.List(ctx, &ingressList)
Expand All @@ -52,11 +59,32 @@ func ReadIngressesFromCluster(ctx context.Context, client client.Client, ingress
return ingresses, nil
}

func ReadIngressesFromFile(filename, namespace string, ingressClasses sets.Set[string]) (map[types.NamespacedName]*networkingv1.Ingress, error) {
// readFileOrStdin reads content from a file or stdin based on the filename.
// If filename is "-", it reads from stdin, otherwise from the specified file.
// Stdin content is stored in memory after the first read to allow multiple calls.
func readFileOrStdin(filename string) ([]byte, error) {
if filename == "-" {
stdinOnce.Do(func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really a big deal, but probably you could simply check if the len of stdIn is > 0 :)

but lgtm

stdin, stdinErr = io.ReadAll(os.Stdin)
if stdinErr != nil {
stdinErr = fmt.Errorf("failed to read from stdin: %w", stdinErr)
}
})
return stdin, stdinErr
}

stream, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file %v: %w", filename, err)
}
return stream, nil
}

func ReadIngressesFromFile(filename, namespace string, ingressClasses sets.Set[string]) (map[types.NamespacedName]*networkingv1.Ingress, error) {
stream, err := readFileOrStdin(filename)
if err != nil {
return nil, err
}

unstructuredObjects, err := ExtractObjectsFromReader(bytes.NewReader(stream), namespace)
if err != nil {
Expand Down Expand Up @@ -98,9 +126,9 @@ func ReadServicesFromCluster(ctx context.Context, client client.Client) (map[typ
}

func ReadServicesFromFile(filename, namespace string) (map[types.NamespacedName]*apiv1.Service, error) {
stream, err := os.ReadFile(filename)
stream, err := readFileOrStdin(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file %v: %w", filename, err)
return nil, err
}

unstructuredObjects, err := ExtractObjectsFromReader(bytes.NewReader(stream), namespace)
Expand Down