|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/urfave/cli/v3" |
| 10 | +) |
| 11 | + |
| 12 | +type configChangedEvent struct{} |
| 13 | + |
| 14 | +func waitTillConfigChanges(ctx context.Context, cmd *cli.Command, cc *apiCommandContext) error { |
| 15 | + openapiSpecPath := cc.workspaceConfig.OpenAPISpec |
| 16 | + if cmd.IsSet("openapi-spec") { |
| 17 | + openapiSpecPath = cmd.String("openapi-spec") |
| 18 | + } |
| 19 | + stainlessConfigPath := cc.workspaceConfig.StainlessConfig |
| 20 | + if cmd.IsSet("stainless-config") { |
| 21 | + stainlessConfigPath = cmd.String("stainless-config") |
| 22 | + } |
| 23 | + |
| 24 | + // Get initial file modification times |
| 25 | + openapiSpecInfo, err := os.Stat(openapiSpecPath) |
| 26 | + if err != nil { |
| 27 | + return fmt.Errorf("failed to get file info for %s: %v", openapiSpecPath, err) |
| 28 | + } |
| 29 | + openapiSpecModTime := openapiSpecInfo.ModTime() |
| 30 | + |
| 31 | + stainlessConfigInfo, err := os.Stat(stainlessConfigPath) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to get file info for %s: %v", stainlessConfigPath, err) |
| 34 | + } |
| 35 | + stainlessConfigModTime := stainlessConfigInfo.ModTime() |
| 36 | + |
| 37 | + // Poll for file changes every 250ms |
| 38 | + ticker := time.NewTicker(250 * time.Millisecond) |
| 39 | + defer ticker.Stop() |
| 40 | + |
| 41 | + for { |
| 42 | + select { |
| 43 | + case <-ticker.C: |
| 44 | + // Check OpenAPI spec file |
| 45 | + if info, err := os.Stat(openapiSpecPath); err == nil { |
| 46 | + if info.ModTime().After(openapiSpecModTime) { |
| 47 | + return nil |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Check Stainless config file |
| 52 | + if info, err := os.Stat(stainlessConfigPath); err == nil { |
| 53 | + if info.ModTime().After(stainlessConfigModTime) { |
| 54 | + return nil |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + case <-ctx.Done(): |
| 59 | + return ctx.Err() |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments