2018-12-06 17:26:46 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
|
|
|
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
|
|
|
// must be obtained first.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package option
|
|
|
|
|
|
|
|
import (
|
2018-12-06 19:48:12 +08:00
|
|
|
"fmt"
|
2018-12-06 18:56:16 +08:00
|
|
|
"os"
|
|
|
|
|
2018-12-06 17:26:46 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2018-12-06 19:48:12 +08:00
|
|
|
"github.com/goodrain/rainbond/util/windows"
|
2018-12-06 17:26:46 +08:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Config config
|
|
|
|
type Config struct {
|
2018-12-06 17:47:01 +08:00
|
|
|
Debug bool
|
|
|
|
RunShell string
|
|
|
|
ServiceName string
|
|
|
|
RunAsService bool
|
2018-12-06 18:56:16 +08:00
|
|
|
LogFile string
|
2018-12-06 17:26:46 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 19:48:12 +08:00
|
|
|
var removeService bool
|
|
|
|
|
2018-12-06 17:26:46 +08:00
|
|
|
//AddFlags config
|
|
|
|
func (c *Config) AddFlags(fs *pflag.FlagSet) {
|
|
|
|
fs.StringVar(&c.RunShell, "run", "", "Specify startup command")
|
|
|
|
fs.StringVar(&c.ServiceName, "service-name", "", "Specify windows service name")
|
2018-12-06 18:56:16 +08:00
|
|
|
fs.StringVar(&c.LogFile, "log-file", "c:\\windwosutil.log", "service log outputfile")
|
2018-12-06 17:47:01 +08:00
|
|
|
fs.BoolVar(&c.RunAsService, "run-as-service", true, "run as windows service")
|
2018-12-06 17:26:46 +08:00
|
|
|
fs.BoolVar(&c.Debug, "debug", false, "debug mode run ")
|
2018-12-06 19:48:12 +08:00
|
|
|
fs.BoolVar(&removeService, "remove-service", false, "remove windows service")
|
2018-12-06 17:26:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check check config
|
|
|
|
func (c *Config) Check() bool {
|
|
|
|
if c.ServiceName == "" {
|
|
|
|
logrus.Errorf("service name can not be empty")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.RunShell == "" {
|
|
|
|
logrus.Errorf("run shell can not be empty")
|
|
|
|
return false
|
|
|
|
}
|
2018-12-06 19:22:24 +08:00
|
|
|
logfile, err := os.OpenFile(c.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
|
2018-12-06 18:56:16 +08:00
|
|
|
if err != nil {
|
2018-12-06 19:09:22 +08:00
|
|
|
logrus.Fatalf("open log file %s failure %s", c.LogFile, err.Error())
|
2018-12-06 18:56:16 +08:00
|
|
|
}
|
|
|
|
logrus.SetOutput(logfile)
|
2018-12-06 19:48:12 +08:00
|
|
|
if removeService {
|
|
|
|
if err := windows.UnRegisterService(c.ServiceName); err != nil {
|
|
|
|
fmt.Printf("remove service %s failure %s", c.ServiceName, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-12-06 17:26:46 +08:00
|
|
|
return true
|
|
|
|
}
|