QBitRun/cmd/qbitrun/cli/cli.go

69 lines
1.8 KiB
Go
Raw Normal View History

2024-06-25 00:19:15 +07:00
package cli
import (
"fmt"
"os"
"path"
"github.com/spf13/cobra"
)
var (
// LookUpMode is the default behavior to look up workflow files.
//
// Valid values are:
// - "xdg": look up in XDG_CONFIG_HOME/qbitrun/workflows
// - "cwd": look up workflows dir in the current working directory
// - other: look up workflows dir in the same directory as the executable
LookUpMode string
Version string
)
var RootCmd = &cobra.Command{
Use: "qbitrun",
Short: "qbitrun is a CLI tool to handle qBittorrent pre/post processing tasks",
Long: "qbitrun is a CLI tool to handle qBittorrent pre/post processing tasks. It uses matchers defined in yaml files to determine which tasks to run on which torrents post download.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cmd.PersistentFlags().Changed("version") {
fmt.Println(Version)
os.Exit(0)
}
},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
func init() {
persistFlag := RootCmd.PersistentFlags()
var workflowDir string
switch LookUpMode {
case "xdg":
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
xdgConfigHome = path.Join(os.Getenv("HOME"), ".config")
}
workflowDir = path.Join(xdgConfigHome, "qbitrun", "workflows")
case "cwd":
cwd, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("failed to get current working directory: %w", err))
}
workflowDir = path.Join(cwd, "workflows")
default:
exe, err := os.Executable()
if err != nil {
panic(fmt.Errorf("failed to get executable path: %w", err))
}
workflowDir = path.Join(path.Dir(exe), "workflows")
}
persistFlag.StringP(
"workflow-dir",
"d",
workflowDir,
"Set the directory where the workflow yaml files are stored.")
persistFlag.BoolP("version", "v", false, "Print the version of qbitrun and exit")
}