Skip to content

Commit 9c5c573

Browse files
authored
Merge pull request #475 from oasisprotocol/andrej/feature/rofl-init-git
cmd/rofl/init: Init git repository if applicable
2 parents 61c9b5c + c3fdf40 commit 9c5c573

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

cmd/rofl/mgmt.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package rofl
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"io"
810
"os"
11+
"os/exec"
912
"path/filepath"
1013

1114
"github.com/spf13/cobra"
@@ -112,6 +115,40 @@ var (
112115
cobra.CheckErr(fmt.Errorf("failed to write manifest: %w", err))
113116
}
114117

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+
115152
fmt.Printf("Created manifest in '%s'.\n", manifest.SourceFileName())
116153
fmt.Printf("Run `oasis rofl create` to register your ROFL app and configure an app ID.\n")
117154
},

0 commit comments

Comments
 (0)