From 266c38fd98fd0853addf0950090990b3752937cd Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Tue, 5 May 2020 17:34:11 -0400 Subject: [PATCH] stage subcommands --- wintun-testing/go.mod | 1 + wintun-testing/go.sum | 3 +++ wintun-testing/ziti-tunnel/cmd/root.go | 27 +++++++++++++++++++ .../ziti-tunnel/cmd/root_addidentity.go | 20 ++++++++++++++ wintun-testing/ziti-tunnel/cmd/root_debug.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_disable_id.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_enable_id.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_removeidentity.go | 20 ++++++++++++++ wintun-testing/ziti-tunnel/cmd/root_svc.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_svc_install.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_svc_remove.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_svc_start.go | 19 +++++++++++++ .../ziti-tunnel/cmd/root_svc_status.go | 20 ++++++++++++++ .../ziti-tunnel/cmd/root_svc_stop.go | 19 +++++++++++++ wintun-testing/ziti-tunnel/cmd/version.go | 20 ++++++++++++++ wintun-testing/ziti-tunnel/main.go | 12 ++++++++- 16 files changed, 280 insertions(+), 1 deletion(-) create mode 100644 wintun-testing/ziti-tunnel/cmd/root.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_addidentity.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_debug.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_disable_id.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_enable_id.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_removeidentity.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc_install.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc_remove.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc_start.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc_status.go create mode 100644 wintun-testing/ziti-tunnel/cmd/root_svc_stop.go create mode 100644 wintun-testing/ziti-tunnel/cmd/version.go diff --git a/wintun-testing/go.mod b/wintun-testing/go.mod index 70695225..73c27e15 100644 --- a/wintun-testing/go.mod +++ b/wintun-testing/go.mod @@ -14,6 +14,7 @@ require ( github.com/netfoundry/ziti-foundation v0.9.8 github.com/netfoundry/ziti-sdk-golang v0.11.16 github.com/sirupsen/logrus v1.5.0 + github.com/spf13/cobra v0.0.5 github.com/tebeka/strftime v0.1.4 // indirect github.com/tv42/topic v0.0.0-20130729201830-aa72cbe81b48 golang.org/x/sys v0.0.0-20200406113430-c6e801f48ba2 diff --git a/wintun-testing/go.sum b/wintun-testing/go.sum index 26f50c29..33b578ae 100644 --- a/wintun-testing/go.sum +++ b/wintun-testing/go.sum @@ -32,6 +32,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4= @@ -93,9 +94,11 @@ github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s github.com/speps/go-hashids v2.0.0+incompatible/go.mod h1:P7hqPzMdnZOfyIk+xrlG1QaSMw+gCBdHKsBDnhpaZvc= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/wintun-testing/ziti-tunnel/cmd/root.go b/wintun-testing/ziti-tunnel/cmd/root.go new file mode 100644 index 00000000..c95fef8b --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "os" + "path/filepath" +) +var exeName, _ = os.Executable() +var rootCmd = &cobra.Command{ + Use: filepath.Base(exeName) , + Short: "Runs the ziti-tunnel as a service", + Long: `This program provides access to a ziti-based network. + When executed without parameters it is expected that this application is running as a service`, + Run: func(cmd *cobra.Command, args []string) { + // Do Stuff Here + fmt.Println("i am in command") + }, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } + +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_addidentity.go b/wintun-testing/ziti-tunnel/cmd/root_addidentity.go new file mode 100644 index 00000000..28460aec --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_addidentity.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(addIdCmd) +} + +var addIdCmd = &cobra.Command{ + Use: "addId", + Short: "Adds the provided jwt file to the running service", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Adds the provided jwt file to the running service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_debug.go b/wintun-testing/ziti-tunnel/cmd/root_debug.go new file mode 100644 index 00000000..ec9ff0db --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_debug.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(debugCmd) +} + +var debugCmd = &cobra.Command{ + Use: "debug", + Short: "Runs the program interactively instead of expecting to be run as a service. Used in debugging", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Runs the program interactively instead of expecting to be run as a service. Used in debugging") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_disable_id.go b/wintun-testing/ziti-tunnel/cmd/root_disable_id.go new file mode 100644 index 00000000..94c8b46a --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_disable_id.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(disableIdCmd) +} + +var disableIdCmd = &cobra.Command{ + Use: "disable", + Short: "Disables any identity in the running service using the provided fingerprint", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Disables any identity in the running service using the provided fingerprint") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_enable_id.go b/wintun-testing/ziti-tunnel/cmd/root_enable_id.go new file mode 100644 index 00000000..ebdbeee2 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_enable_id.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(enableIdCmd) +} + +var enableIdCmd = &cobra.Command{ + Use: "enable", + Short: "Enables any identity in the running service using the provided fingerprint", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Enables any identity in the running service using the provided fingerprint") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_removeidentity.go b/wintun-testing/ziti-tunnel/cmd/root_removeidentity.go new file mode 100644 index 00000000..92444aa7 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_removeidentity.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(removeIdCmd) +} + +var removeIdCmd = &cobra.Command{ + Use: "removeId", + Short: "Removes any identity from the running service using the provided fingerprint", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Removes any identity from the running service using the provided fingerprint") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc.go b/wintun-testing/ziti-tunnel/cmd/root_svc.go new file mode 100644 index 00000000..decfbd6f --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(serviceCmd) +} + +var serviceCmd = &cobra.Command{ + Use: "service", + Short: "Expects to run the program as a service", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("print or return the version here") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc_install.go b/wintun-testing/ziti-tunnel/cmd/root_svc_install.go new file mode 100644 index 00000000..ed838e84 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc_install.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + serviceCmd.AddCommand(installCmd) +} + +var installCmd = &cobra.Command{ + Use: "install", + Short: "Installs the executable as a service.", + Long: `When installing as a service using this function make sure not to move the executable. The path to the executing program will be used in the service`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("install this thing as a service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc_remove.go b/wintun-testing/ziti-tunnel/cmd/root_svc_remove.go new file mode 100644 index 00000000..4a0a64a2 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc_remove.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + serviceCmd.AddCommand(removeCmd) +} + +var removeCmd = &cobra.Command{ + Use: "remove", + Short: "Removes the executable as a service.", + Long: `When removing the service make sure the path and service name have not changed`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("remove this thing as a service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc_start.go b/wintun-testing/ziti-tunnel/cmd/root_svc_start.go new file mode 100644 index 00000000..4de6e9c1 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc_start.go @@ -0,0 +1,19 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + serviceCmd.AddCommand(startCmd) +} + +var startCmd = &cobra.Command{ + Use: "start", + Short: "Tries to start the service", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Tries to start the service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc_status.go b/wintun-testing/ziti-tunnel/cmd/root_svc_status.go new file mode 100644 index 00000000..6bcac35a --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc_status.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + serviceCmd.AddCommand(statusCmd) +} + +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Prints the status of the service", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Prints the status of the service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/root_svc_stop.go b/wintun-testing/ziti-tunnel/cmd/root_svc_stop.go new file mode 100644 index 00000000..ac400f52 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/root_svc_stop.go @@ -0,0 +1,19 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + serviceCmd.AddCommand(stopCmd) +} + +var stopCmd = &cobra.Command{ + Use: "stop", + Short: "Tries to stop the service", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("try to stop the service") + }, +} diff --git a/wintun-testing/ziti-tunnel/cmd/version.go b/wintun-testing/ziti-tunnel/cmd/version.go new file mode 100644 index 00000000..3e0ef436 --- /dev/null +++ b/wintun-testing/ziti-tunnel/cmd/version.go @@ -0,0 +1,20 @@ +package cmd + +import ( +"fmt" + +"github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version of the current executable", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("print or return the version here") + }, +} diff --git a/wintun-testing/ziti-tunnel/main.go b/wintun-testing/ziti-tunnel/main.go index 2dd05b87..983ead8f 100644 --- a/wintun-testing/ziti-tunnel/main.go +++ b/wintun-testing/ziti-tunnel/main.go @@ -7,6 +7,7 @@ import ( "golang.org/x/sys/windows/svc/eventlog" "os" "strings" + "wintun-testing/ziti-tunnel/cmd" "wintun-testing/ziti-tunnel/globals" "golang.org/x/sys/windows/svc" @@ -18,7 +19,16 @@ var log = globals.Logger() func main() { globals.InitLogger("debug") - // passing no arguments is an indicator that this is excpecting to be run 'as a service'. + if len(os.Args) < 2 { + // if no args supplied - expect to run as service + os.Args = append(os.Args, "service") + } + cmd.Execute() + if true { + os.Exit(0) + } + + // passing no arguments is an indicator that this is expecting to be run 'as a service'. // using arg count instead of svc.IsAnInteractiveSession() as svc.IsAnInteractiveSession() // seems to return false even when run in an interactive shell as via `psexec -i -s cmd.exe` hasArgs := len(os.Args) > 1