|
1 | 1 | package rofl |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "encoding/json" |
| 7 | + "errors" |
6 | 8 | "fmt" |
7 | 9 | "io" |
8 | 10 | "os" |
| 11 | + "os/exec" |
9 | 12 | "path/filepath" |
10 | 13 |
|
11 | 14 | "github.com/spf13/cobra" |
@@ -112,6 +115,40 @@ var ( |
112 | 115 | cobra.CheckErr(fmt.Errorf("failed to write manifest: %w", err)) |
113 | 116 | } |
114 | 117 |
|
| 118 | + // Initialize git repository if not already present. |
| 119 | + _, err = os.Stat(".git") |
| 120 | + if err != nil && errors.Is(err, os.ErrNotExist) { |
| 121 | + // Git directory doesn't exist, try to initialize. |
| 122 | + gitInitCmd := exec.Command("git", "init") |
| 123 | + if err = gitInitCmd.Run(); err != nil { |
| 124 | + fmt.Printf("Git repository not initialized: %s.\n", err) |
| 125 | + } else { |
| 126 | + fmt.Printf("Git repository initialized.\n") |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Initialize .gitignore. |
| 131 | + needToAddOrcIgnore := true |
| 132 | + gitignore, err := os.Open(".gitignore") |
| 133 | + if err == nil { |
| 134 | + s := bufio.NewScanner(gitignore) |
| 135 | + for s.Scan() { |
| 136 | + line := s.Text() |
| 137 | + if line == "*.orc" { |
| 138 | + needToAddOrcIgnore = false |
| 139 | + break |
| 140 | + } |
| 141 | + } |
| 142 | + _ = gitignore.Close() |
| 143 | + } |
| 144 | + if needToAddOrcIgnore { |
| 145 | + gitignore, err := os.OpenFile(".gitignore", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) |
| 146 | + if err == nil { |
| 147 | + _, _ = gitignore.Write([]byte("*.orc\n")) |
| 148 | + _ = gitignore.Close() |
| 149 | + } |
| 150 | + } |
| 151 | + |
115 | 152 | fmt.Printf("Created manifest in '%s'.\n", manifest.SourceFileName()) |
116 | 153 | fmt.Printf("Run `oasis rofl create` to register your ROFL app and configure an app ID.\n") |
117 | 154 | }, |
|
0 commit comments