From 398122a68990d13a1f8fe6316566ff3c01a252e4 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Thu, 26 Oct 2023 15:57:46 +0200 Subject: [PATCH] add(cli): taskwarrior context wrapper command --- cmd/task.go | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/cmd/task.go b/cmd/task.go index 71281c3..d2beded 100644 --- a/cmd/task.go +++ b/cmd/task.go @@ -1,6 +1,15 @@ package cmd -import "github.com/spf13/cobra" +import ( + "fmt" + "os" + "os/exec" + "strings" + + "gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea" + "gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitw" + "github.com/spf13/cobra" +) func init() { rootCmd.AddCommand(taskCmd) @@ -10,12 +19,39 @@ var taskCmd = &cobra.Command{ Use: "task", Short: "Taskwarrior wrapper with automatic context for current git project", Long: `Taskwarrior wrapper with automatic context for current git project. -This allows the usage of common taskwarrior commands with appling the context for the current git project, +This allows the usage of common taskwarrior commands with applying the context for the current git project, which filters all the tasks automatically for the current project. The context will be disabled as soon as the provided command has been executed. -There is no need for the user to disable / enable context specific configurations.`, +There is no need for the user to disable / enable context specific configurations. The user will need to configure the context beforehand themselfs.`, Run: func(cmd *cobra.Command, args []string) { - // TODO: run taskwarrior context enabling (maybe even creating the context) - // TODO: run taskwarrior command with provided args - // TODO: run taskwarrior context disableing + var execCmd *exec.Cmd + server := gitea.NewGitea() + repository, err := gitw.Discover() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + repository, err = server.VerifyRepository(repository) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + execCmd = exec.Command("task", "context", repository.Name) + if err := execCmd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Failed to execute task command: `task context %s`\n", repository.Name) + os.Exit(-1) + } + execCmd = exec.Command("task", args...) + execCmd.Stdout = os.Stdout + execCmd.Stdin = os.Stdin + execCmd.Stderr = os.Stderr + if err := execCmd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Failed to execute task command: `task %s`\n", strings.Join(args, " ")) + defer os.Exit(-1) // do not exit yet because the context still needs to be removed + } + execCmd = exec.Command("task", "context", "none") + if err := execCmd.Run(); err != nil { + fmt.Fprintln(os.Stderr, "Failed to execute task command: `task context none`") + os.Exit(-1) + } }, }