parseFromStdin handles parsing from stdin input
(cmd *cobra.Command)
| 113 | |
| 114 | // parseFromStdin handles parsing from stdin input |
| 115 | func parseFromStdin(cmd *cobra.Command) error { |
| 116 | // Read from stdin |
| 117 | content, err := ReadFromStdin() |
| 118 | if err != nil { |
| 119 | return fmt.Errorf("failed to read from stdin: %w", err) |
| 120 | } |
| 121 | |
| 122 | // Validate stdin content |
| 123 | if err := ValidateStdinInput(content); err != nil { |
| 124 | return fmt.Errorf("stdin validation failed: %w", err) |
| 125 | } |
| 126 | |
| 127 | // Load configuration |
| 128 | cfg, err := config.LoadDefault() |
| 129 | if err != nil { |
| 130 | cfg = config.DefaultConfig() |
| 131 | } |
| 132 | |
| 133 | // Track which flags were explicitly set |
| 134 | flagsChanged := make(map[string]bool) |
| 135 | cmd.Flags().Visit(func(f *pflag.Flag) { |
| 136 | flagsChanged[f.Name] = true |
| 137 | }) |
| 138 | if cmd.Parent() != nil && cmd.Parent().PersistentFlags() != nil { |
| 139 | cmd.Parent().PersistentFlags().Visit(func(f *pflag.Flag) { |
| 140 | flagsChanged[f.Name] = true |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | // Create parser options |
| 145 | opts := ParserOptionsFromConfig(cfg, flagsChanged, ParserFlags{ |
| 146 | ShowAST: parseShowAST, |
| 147 | ShowTokens: parseShowTokens, |
| 148 | TreeView: parseTreeView, |
| 149 | Format: format, |
| 150 | Verbose: verbose, |
| 151 | }) |
| 152 | |
| 153 | // Create parser |
| 154 | parser := NewParser(cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) |
| 155 | |
| 156 | // Parse the stdin content (Parse accepts string input directly) |
| 157 | result, err := parser.Parse(string(content)) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | |
| 162 | // CRITICAL: Always release AST if it was created |
| 163 | if result.AST != nil { |
| 164 | defer ast.ReleaseAST(result.AST) |
| 165 | } |
| 166 | |
| 167 | // Display the result |
| 168 | return parser.Display(result) |
| 169 | } |
| 170 | |
| 171 | func init() { |
| 172 | rootCmd.AddCommand(parseCmd) |
no test coverage detected