gitee.com/mirrors/gauge@v1.0.6/cmd/daemon.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"os"
    22  
    23  	"github.com/getgauge/gauge/api"
    24  	"github.com/getgauge/gauge/api/infoGatherer"
    25  	"github.com/getgauge/gauge/api/lang"
    26  	"github.com/getgauge/gauge/config"
    27  	"github.com/getgauge/gauge/manifest"
    28  	"github.com/getgauge/gauge/track"
    29  	"github.com/getgauge/gauge/util"
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  const (
    34  	isDaemon = "IS_DAEMON"
    35  )
    36  
    37  var (
    38  	daemonCmd = &cobra.Command{
    39  		Use:     "daemon [flags] <port> [args]",
    40  		Short:   "Run as a daemon",
    41  		Long:    `Run as a daemon.`,
    42  		Example: "  gauge daemon 1234",
    43  		Run: func(cmd *cobra.Command, args []string) {
    44  			os.Setenv(isDaemon, "true")
    45  			if err := config.SetProjectRoot(args); err != nil {
    46  				exit(err, cmd.UsageString())
    47  			}
    48  			loadEnvAndReinitLogger(cmd)
    49  			manifest, _ := manifest.ProjectManifest()
    50  			language := manifest.Language
    51  			if lsp {
    52  				go track.ScheduleDaemonTracking("lsp", language)
    53  				lang.Start(&infoGatherer.SpecInfoGatherer{SpecDirs: getSpecsDir(args)}, logLevel)
    54  				return
    55  			}
    56  			go track.ScheduleDaemonTracking("api", language)
    57  			port := ""
    58  			specs := util.GetSpecDirs()
    59  			if len(args) > 0 {
    60  				port = args[0]
    61  				specs = getSpecsDir(args[1:])
    62  			}
    63  			api.RunInBackground(port, specs)
    64  		},
    65  		PersistentPostRun: func(cmd *cobra.Command, args []string) { /* noop */ },
    66  		DisableAutoGenTag: true,
    67  	}
    68  	lsp bool
    69  )
    70  
    71  func init() {
    72  	GaugeCmd.AddCommand(daemonCmd)
    73  	daemonCmd.Flags().BoolVarP(&lsp, "lsp", "", false, "Start language server")
    74  	daemonCmd.Flags().MarkHidden("lsp")
    75  }