Skip to content

Commit

Permalink
support egctl x translate -f - (#1065)
Browse files Browse the repository at this point in the history
* support `egctl x translate -f -`

Fixes: #1057

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* lint

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

---------

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg committed Feb 21, 2023
1 parent 2c57f3d commit b97859d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/latest/user/egctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ In the below example, we will translate the Kubernetes resources (including the
resources.

```
cat <<EOF >> gateway-api-config.yaml
cat <<EOF | egctl x translate --from gateway-api --to xds -f -
apiVersion: gateway.networking.k8s.io/v1beta1
kind: GatewayClass
metadata:
Expand Down Expand Up @@ -76,10 +76,10 @@ spec:
type: PathPrefix
value: /
EOF
```

```
egctl x translate --from gateway-api --to xds -f gateway-api-config.yaml
xDS
Expand Down
35 changes: 33 additions & 2 deletions internal/cmd/egctl/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package egctl

import (
"bufio"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -91,14 +92,44 @@ func getValidOutputTypesStr() string {
return fmt.Sprintf("Valid types are %v.", validOutputTypes())
}

func translate(w io.Writer, inFile, inType, outType string) error {
func getInputBytes(inFile string) ([]byte, error) {

// Get input from stdin
if inFile == "-" {
scanner := bufio.NewScanner(os.Stdin)
var input string
for {
if !scanner.Scan() {
break
}
input += scanner.Text() + "\n"
}
return []byte(input), nil
}
// Get input from file
return os.ReadFile(inFile)
}

func validate(inFile, inType, outType string) error {
if !isValidInputType(inType) {
return fmt.Errorf("%s is not a valid input type. %s", inType, getValidInputTypesStr())
}
if !isValidOutputType(outType) {
return fmt.Errorf("%s is not a valid output type. %s", outType, getValidOutputTypesStr())
}
inBytes, err := os.ReadFile(inFile)
if inFile == "" {
return fmt.Errorf("--file must be specified")
}

return nil
}

func translate(w io.Writer, inFile, inType, outType string) error {
if err := validate(inFile, inType, outType); err != nil {
return err
}

inBytes, err := getInputBytes(inFile)
if err != nil {
return fmt.Errorf("unable to read input file: %w", err)
}
Expand Down

0 comments on commit b97859d

Please sign in to comment.