Skip to content

Commit 8f6dbf8

Browse files
committed
feat(apply): support reading OAS from stdin with -f - (YAML or JSON). Update help text.
1 parent 2a1c958 commit 8f6dbf8

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

internal/cli/api.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Examples:
324324
RunE: runAPIApply,
325325
}
326326

327-
cmd.Flags().StringP("file", "f", "", "Path to Tyk-enhanced OpenAPI specification file (required)")
327+
cmd.Flags().StringP("file", "f", "", "Path to Tyk-enhanced OpenAPI specification file (use '-' for stdin) (required)")
328328
cmd.Flags().String("version-name", "", "Version name (defaults to info.version or v1)")
329329
cmd.Flags().Bool("set-default", true, "Set this version as the default")
330330

@@ -939,26 +939,41 @@ func runAPIApply(cmd *cobra.Command, args []string) error {
939939
return fmt.Errorf("configuration not found")
940940
}
941941

942-
// Validate and read the OAS file
943-
if !filepath.IsAbs(filePath) {
944-
absPath, err := filepath.Abs(filePath)
945-
if err != nil {
946-
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to resolve file path: %v", err)}
947-
}
948-
filePath = absPath
949-
}
942+
var oasData map[string]interface{}
943+
if filePath == "-" {
944+
// Read from stdin; support JSON or YAML (YAML parser also accepts JSON)
945+
data, err := io.ReadAll(os.Stdin)
946+
if err != nil {
947+
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to read stdin: %v", err)}
948+
}
949+
if len(data) == 0 {
950+
return &ExitError{Code: 2, Message: "no input provided on stdin"}
951+
}
952+
if err := yaml.Unmarshal(data, &oasData); err != nil {
953+
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to parse input as YAML/JSON: %v", err)}
954+
}
955+
} else {
956+
// Validate and read the OAS file
957+
if !filepath.IsAbs(filePath) {
958+
absPath, err := filepath.Abs(filePath)
959+
if err != nil {
960+
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to resolve file path: %v", err)}
961+
}
962+
filePath = absPath
963+
}
950964

951-
// Check if file exists
952-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
953-
return &ExitError{Code: 2, Message: fmt.Sprintf("file not found: %s", filePath)}
954-
}
965+
// Check if file exists
966+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
967+
return &ExitError{Code: 2, Message: fmt.Sprintf("file not found: %s", filePath)}
968+
}
955969

956-
// Load and parse the OAS file
957-
fileInfo, err := filehandler.LoadFile(filePath)
958-
if err != nil {
959-
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to load OAS file: %v", err)}
960-
}
961-
oasData := fileInfo.Content
970+
// Load and parse the OAS file
971+
fileInfo, err := filehandler.LoadFile(filePath)
972+
if err != nil {
973+
return &ExitError{Code: 2, Message: fmt.Sprintf("failed to load OAS file: %v", err)}
974+
}
975+
oasData = fileInfo.Content
976+
}
962977

963978
// Enhanced validation: Check if it's a Tyk-enhanced OAS file
964979
if !oas.HasTykExtensions(oasData) {

0 commit comments

Comments
 (0)