Skip to content

Commit

Permalink
feat(cmd): added init command
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj Nandan Sharma authored and Raj Nandan Sharma committed Apr 30, 2024
1 parent 24f2b42 commit 8697941
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
Thumbs.db

.conventionalcommits/
.okgit/
dist/
bin/
.okgit/
45 changes: 45 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"io/ioutil"
"path/filepath"
"strings"

"github.com/rajnandan1/okgit/utils"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
Use: "init",
Short: "Add .okgit/ to the .gitignore file",
Run: func(cmd *cobra.Command, args []string) {
gitignorePath := filepath.Join(".", ".gitignore")
okgitPath := ".okgit/"

// Read the contents of the .gitignore file
data, err := ioutil.ReadFile(gitignorePath)
if err != nil {
utils.LogFatal(err)
}

// Append .okgit/ to the .gitignore file if it's not already present
contents := string(data)
if !strings.Contains(contents, okgitPath) {
contents += okgitPath + "\n"

// Write the updated contents back to the .gitignore file
err = ioutil.WriteFile(gitignorePath, []byte(contents), 0644)
if err != nil {
utils.LogFatal(err)
}

utils.LogOutput(".okgit/ added to .gitignore file")
} else {
utils.LogOutput(".okgit/ already present in .gitignore file")
}
},
}

func init() {
rootCmd.AddCommand(initCmd)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Version: "1.0.9",
Version: "1.0.10",
}

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

0 comments on commit 8697941

Please sign in to comment.