From 133ad1f50e5ea252594cce86477095a7137e56e3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 18 Jul 2019 16:15:26 +0200 Subject: [PATCH] Add show command --- pmctl/show.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 pmctl/show.go diff --git a/pmctl/show.go b/pmctl/show.go new file mode 100644 index 00000000..7e4b3396 --- /dev/null +++ b/pmctl/show.go @@ -0,0 +1,89 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(showCmd) + showCmd.AddCommand(showCore) + showCmd.AddCommand(showApp) + showCmd.AddCommand(showNotifier) +} + +var showCmd = &cobra.Command{ + Use: "show", + Short: "Show the command to run a Portmaster component yourself", +} + +var showCore = &cobra.Command{ + Use: "core", + Short: "Show command to run the Portmaster Core", + RunE: func(cmd *cobra.Command, args []string) error { + return show(cmd, &Options{ + Identifier: "core/portmaster-core", + }) + }, + FParseErrWhitelist: cobra.FParseErrWhitelist{ + // UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags + UnknownFlags: true, + }, +} + +var showApp = &cobra.Command{ + Use: "app", + Short: "Show command to run the Portmaster App", + RunE: func(cmd *cobra.Command, args []string) error { + return show(cmd, &Options{ + Identifier: "app/portmaster-app", + }) + }, + FParseErrWhitelist: cobra.FParseErrWhitelist{ + // UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags + UnknownFlags: true, + }, +} + +var showNotifier = &cobra.Command{ + Use: "notifier", + Short: "Show command to run the Portmaster Notifier", + RunE: func(cmd *cobra.Command, args []string) error { + return show(cmd, &Options{ + Identifier: "notifier/portmaster-notifier", + }) + }, + FParseErrWhitelist: cobra.FParseErrWhitelist{ + // UnknownFlags will ignore unknown flags errors and continue parsing rest of the flags + UnknownFlags: true, + }, +} + +func show(cmd *cobra.Command, opts *Options) error { + defer close(programEnded) + + // get original arguments + var args []string + if len(os.Args) < 4 { + return cmd.Help() + } + args = os.Args[3:] + + // adapt identifier + if onWindows { + opts.Identifier += ".exe" + } + + file, err := getFile(opts) + if err != nil { + return fmt.Errorf("could not get component: %s", err) + } + + fmt.Printf("%s command:\n", logPrefix) + fmt.Printf("%s %s\n", file.Path(), strings.Join(args, " ")) + + return nil +}