-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1499 from merico-dev/new-dtm
Feat: Add `dtm commit -m` Command Implementation
- Loading branch information
Showing
7 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/devstream-io/devstream/internal/log" | ||
"github.com/devstream-io/devstream/internal/pkg/commit" | ||
"github.com/devstream-io/devstream/internal/response" | ||
) | ||
|
||
// commitCmd represents the commit command | ||
var commitCmd = &cobra.Command{ | ||
Use: "commit", | ||
Short: "commit is used to execute git commit operations", | ||
Long: `commit is used to execute git commit operations | ||
e.g. | ||
1. dtm commit -m "commit message" | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
message := viper.GetString("message") | ||
if message == "" { | ||
log.Error("message is required") | ||
os.Exit(1) | ||
} | ||
err := commit.Commit(message) | ||
if err != nil { | ||
log.Errorf("commit error: %v", err) | ||
r := response.New(response.StatusError, response.MessageError, err.Error()) | ||
r.Print(OutputFormat) | ||
} else { | ||
r := response.New(response.StatusOK, response.MessageOK, "") | ||
r.Print(OutputFormat) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(commitCmd) | ||
commitCmd.Flags().StringP("message", "m", "", "commit message") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package commit | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/devstream-io/devstream/internal/log" | ||
) | ||
|
||
// Commit is used to execute git commit operations | ||
func Commit(message string) error { | ||
// Check if the git command exists | ||
gitPath, err := exec.LookPath("git") | ||
if err != nil { | ||
return fmt.Errorf("git command not found: %w", err) | ||
} | ||
|
||
cmd := exec.Command(gitPath, "commit", "-m", message) | ||
output, err := cmd.CombinedOutput() | ||
outputStr := strings.TrimSpace(string(output)) | ||
|
||
if err != nil { | ||
return fmt.Errorf("git commit failed: %w\nOutput: %s", err, outputStr) | ||
} | ||
|
||
log.Infof("Successfully committed the file") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package commit_test | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/devstream-io/devstream/internal/pkg/commit" | ||
) | ||
|
||
var _ = Describe("Commit", func() { | ||
var testRepoDir string | ||
|
||
BeforeEach(func() { | ||
// 1. Create a temporary directory | ||
var err error | ||
testRepoDir, err = os.MkdirTemp("", "test-repo-*") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// 2. Change the working directory to the temporary directory | ||
err = os.Chdir(testRepoDir) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// 3. Initialize a git repository | ||
cmd := exec.Command("git", "init") | ||
err = cmd.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// 4. Create a file and write some content to it | ||
file, err := os.Create(filepath.Join(testRepoDir, "test.txt")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = file.WriteString("Test content") | ||
Expect(err).NotTo(HaveOccurred()) | ||
file.Close() | ||
|
||
// 5. Add the file to the git index | ||
cmd = exec.Command("git", "add", "test.txt") | ||
err = cmd.Run() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
err := os.RemoveAll(testRepoDir) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should create a new commit with the given message", func() { | ||
message := "Test commit" | ||
err := Commit(message) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cmd := exec.Command("git", "log", "--oneline") | ||
output, err := cmd.CombinedOutput() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(string(output)).To(ContainSubstring(message)) | ||
}) | ||
|
||
It("should return an error when git is not installed", func() { | ||
origGitPath, err := exec.LookPath("git") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = os.Setenv("PATH", "") | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer func() { | ||
err = os.Setenv("PATH", origGitPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}() | ||
|
||
err = Commit("Test commit") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters