Skip to content

Commit 8697941

Browse files
Raj Nandan SharmaRaj Nandan Sharma
Raj Nandan Sharma
authored and
Raj Nandan Sharma
committed
feat(cmd): added init command
1 parent 24f2b42 commit 8697941

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
Thumbs.db
2626

2727
.conventionalcommits/
28-
.okgit/
2928
dist/
3029
bin/
30+
.okgit/

cmd/init.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"io/ioutil"
5+
"path/filepath"
6+
"strings"
7+
8+
"github.com/rajnandan1/okgit/utils"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var initCmd = &cobra.Command{
13+
Use: "init",
14+
Short: "Add .okgit/ to the .gitignore file",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
gitignorePath := filepath.Join(".", ".gitignore")
17+
okgitPath := ".okgit/"
18+
19+
// Read the contents of the .gitignore file
20+
data, err := ioutil.ReadFile(gitignorePath)
21+
if err != nil {
22+
utils.LogFatal(err)
23+
}
24+
25+
// Append .okgit/ to the .gitignore file if it's not already present
26+
contents := string(data)
27+
if !strings.Contains(contents, okgitPath) {
28+
contents += okgitPath + "\n"
29+
30+
// Write the updated contents back to the .gitignore file
31+
err = ioutil.WriteFile(gitignorePath, []byte(contents), 0644)
32+
if err != nil {
33+
utils.LogFatal(err)
34+
}
35+
36+
utils.LogOutput(".okgit/ added to .gitignore file")
37+
} else {
38+
utils.LogOutput(".okgit/ already present in .gitignore file")
39+
}
40+
},
41+
}
42+
43+
func init() {
44+
rootCmd.AddCommand(initCmd)
45+
}

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{
1717
// Uncomment the following line if your bare application
1818
// has an action associated with it:
1919
// Run: func(cmd *cobra.Command, args []string) { },
20-
Version: "1.0.9",
20+
Version: "1.0.10",
2121
}
2222

2323
// Execute adds all child commands to the root command and sets flags appropriately.

0 commit comments

Comments
 (0)