github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/cmd/service.go (about)

     1  // Copyright (C) 2016-2017 vLife Systems Ltd <http://vlifesystems.com>
     2  // Licensed under an MIT licence.  Please see LICENSE.md for details.
     3  
     4  package cmd
     5  
     6  import (
     7  	"github.com/spf13/cobra"
     8  	"github.com/vlifesystems/rulehunter/logger"
     9  	"github.com/vlifesystems/rulehunter/quitter"
    10  )
    11  
    12  var ServiceCmd = &cobra.Command{
    13  	Use:   "service",
    14  	Short: "Control Rulehunter service",
    15  	Long:  `Control Rulehunter operating system service.`,
    16  }
    17  
    18  var ServiceInstallCmd = &cobra.Command{
    19  	Use:   "install",
    20  	Short: "Install Rulehunter as a service",
    21  	Long:  `Install the Rulehunter server as an operating system service.`,
    22  	RunE: func(cmd *cobra.Command, args []string) error {
    23  		l := logger.NewSvcLogger()
    24  		return runInstallService(l, flagConfigFilename)
    25  	},
    26  }
    27  
    28  var ServiceUninstallCmd = &cobra.Command{
    29  	Use:   "uninstall",
    30  	Short: "Uninstall Rulehunter service",
    31  	Long:  `Uninstall the Rulehunter operating system service.`,
    32  	RunE: func(cmd *cobra.Command, args []string) error {
    33  		l := logger.NewSvcLogger()
    34  		return runUninstallService(l, flagConfigFilename)
    35  	},
    36  }
    37  
    38  func init() {
    39  	ServiceCmd.PersistentFlags().StringVar(
    40  		&flagUser,
    41  		"user",
    42  		"",
    43  		"The name of a user to run the service under",
    44  	)
    45  	ServiceCmd.AddCommand(ServiceInstallCmd)
    46  	ServiceCmd.AddCommand(ServiceUninstallCmd)
    47  }
    48  
    49  func runInstallService(l logger.Logger, configFilename string) error {
    50  	q := quitter.New()
    51  	defer q.Quit()
    52  	s, err := InitSetup(l, q, configFilename)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	s.svc.Uninstall()
    57  	if err := s.svc.Install(); err != nil {
    58  		return err
    59  	}
    60  	return nil
    61  }
    62  
    63  func runUninstallService(l logger.Logger, configFilename string) error {
    64  	q := quitter.New()
    65  	defer q.Quit()
    66  	s, err := InitSetup(l, q, configFilename)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	return s.svc.Uninstall()
    71  }