<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"code.google.com/p/winsvc/debug"
	"code.google.com/p/winsvc/eventlog"
	"code.google.com/p/winsvc/mgr"
	"code.google.com/p/winsvc/svc"
	"github.com/StackExchange/slog"
)

var win_service_command = flag.String("winsvc", "", "For Windows Service, can be install, remove, start, stop")

func init() {
	mains = append(mains, win_service_main)
}

func win_service_main() {
	const svcName = "scollector"

	isIntSess, err := svc.IsAnInteractiveSession()
	if err != nil {
		slog.Fatalf("failed to determine if we are running in an interactive session: %v", err)
	}
	if !isIntSess {
		go runService(svcName, false)
		return
	}

	if *win_service_command == "" {
		return
	}
	switch *win_service_command {
	case "install":
		err = installService(svcName, "Stack Exchange's Metric Collection Agent")
	case "remove":
		err = removeService(svcName)
	case "start":
		err = startService(svcName)
	case "stop":
		err = controlService(svcName, svc.Stop, svc.Stopped)
	}
	if err != nil {
		slog.Fatalf("failed to %s %s: %v", *win_service_command, svcName, err)
	}
	os.Exit(0)
}

func exePath() (string, error) {
	return "C:\\scollector.exe", nil
}

func installService(name, desc string) error {
	exepath, err := exePath()
	if err != nil {
		return err
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", name)<commit_msg>cmd/scollector: Allow scollector service to run non-interactively<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"code.google.com/p/winsvc/debug"
	"code.google.com/p/winsvc/eventlog"
	"code.google.com/p/winsvc/mgr"
	"code.google.com/p/winsvc/svc"
	"github.com/StackExchange/slog"
)

var win_service_command = flag.String("winsvc", "", "For Windows Service, can be install, remove, start, stop")

func init() {
	mains = append(mains, win_service_main)
}

func win_service_main() {
	const svcName = "scollector"
	var err error
	switch *win_service_command {
	case "install":
		err = installService(svcName, "Stack Exchange's Metric Collection Agent")
	case "remove":
		err = removeService(svcName)
	case "start":
		err = startService(svcName)
	case "stop":
		err = controlService(svcName, svc.Stop, svc.Stopped)
	default:
		isIntSess, err := svc.IsAnInteractiveSession()
		if err != nil {
			slog.Fatalf("failed to determine if we are running in an interactive session: %v", err)
		}
		if !isIntSess {
			go runService(svcName, false)
			return
		}
	}
	if err != nil {
		slog.Fatalf("failed to %s %s: %v", *win_service_command, svcName, err)
	}
	os.Exit(0)
}

func exePath() (string, error) {
	return "C:\\scollector.exe", nil
}

func installService(name, desc string) error {
	exepath, err := exePath()
	if err != nil {
		return err
	}
	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err == nil {
		s.Close()
		return fmt.Errorf("service %s already exists", name)
{'fraction_non_alphanumeric': 0.14411764705882352, 'fraction_numerical': 0.00065359477124183, 'mean_word_length': 4.118729096989966, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5543201', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1194, 'n_words': 290}