MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseFromStdin

Function parseFromStdin

cmd/gosqlx/cmd/parse.go:115–169  ·  view source on GitHub ↗

parseFromStdin handles parsing from stdin input

(cmd *cobra.Command)

Source from the content-addressed store, hash-verified

113
114// parseFromStdin handles parsing from stdin input
115func 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
171func init() {
172 rootCmd.AddCommand(parseCmd)

Callers 1

parseRunFunction · 0.85

Calls 11

ParseMethod · 0.95
DisplayMethod · 0.95
LoadDefaultFunction · 0.92
DefaultConfigFunction · 0.92
ReleaseASTFunction · 0.92
ParserOptionsFromConfigFunction · 0.85
ReadFromStdinFunction · 0.70
ValidateStdinInputFunction · 0.70
NewParserFunction · 0.70
ErrorfMethod · 0.65
VisitMethod · 0.65

Tested by

no test coverage detected