<commit_before>package service

import (
	"strconv"
	"strings"
)

const systemctlExec = "systemctl"

type systemctl struct {
}

func (s systemctl) startCmd(sName string) []string {
	return []string{systemctlExec, "start", sName}
}

func (s systemctl) statusCmd(sName string) []string {
	return []string{systemctlExec, "status", sName}
}

func (s systemctl) stopCmd(sName string) []string {
	return []string{systemctlExec, "stop", sName}
}

func (s systemctl) parseStatus(sData string, err error) (Status, error) {
	var st Status
	lines := strings.Split(sData, "\n")
	if err != nil {
		errString := err.Error()
		idx := strings.LastIndex(errString, " ")
		exitCode, _ := strconv.Atoi(errString[idx+1 : len(errString)])
		// SystemD status is 3 when service is stopped or does not exists
		if exitCode != 3 {
			return st, err
		}
		for _, line := range lines {
			line = strings.Trim(line, " ")
			// When services does not exists then "Loaded" contains "not-found"
			if strings.HasPrefix(line, "Loaded") && strings.Contains(line, "not-found") {
				return st, err
			}
		}
	}
	for _, line := range lines {
		line = strings.Trim(line, " ")
		if strings.HasPrefix(line, "Active") {
			st.Running = strings.Contains(line, "active (running)")
			if !st.Running {
				break
			}
		} else if strings.HasPrefix(line, "Main PID") {
			pid := line[10:len(line)]
			idx := strings.Index(pid, " ")
			pid = pid[0 : idx-1]
			st.PID, _ = strconv.Atoi(pid)
		}
	}
	return st, nil
}<commit_msg> Fix PID parse error for systemd<commit_after>package service

import (
	"strconv"
	"strings"
)

const systemctlExec = "systemctl"

type systemctl struct {
}

func (s systemctl) startCmd(sName string) []string {
	return []string{systemctlExec, "start", sName}
}

func (s systemctl) statusCmd(sName string) []string {
	return []string{systemctlExec, "status", sName}
}

func (s systemctl) stopCmd(sName string) []string {
	return []string{systemctlExec, "stop", sName}
}

func (s systemctl) parseStatus(sData string, err error) (Status, error) {
	var st Status
	lines := strings.Split(sData, "\n")
	if err != nil {
		errString := err.Error()
		idx := strings.LastIndex(errString, " ")
		exitCode, _ := strconv.Atoi(errString[idx+1 : len(errString)])
		// SystemD status is 3 when service is stopped or does not exists
		if exitCode != 3 {
			return st, err
		}
		for _, line := range lines {
			line = strings.Trim(line, " ")
			// When services does not exists then "Loaded" contains "not-found"
			if strings.HasPrefix(line, "Loaded") && strings.Contains(line, "not-found") {
				return st, err
			}
		}
	}
	for _, line := range lines {
		line = strings.Trim(line, " ")
		if strings.HasPrefix(line, "Active") {
			st.Running = strings.Contains(line, "active (running)")
			if !st.Running {
				break
			}
		} else if strings.HasPrefix(line, "Main PID") {
			pid := line[10:len(line)]
			idx := strings.Index(pid, " ")
			pid = pid[0:idx]
			st.PID, _ = strconv.Atoi(pid)
		}
	}
	return st, nil
}
{'fraction_non_alphanumeric': 0.1494793416190796, 'fraction_numerical': 0.004366812227074236, 'mean_word_length': 3.757188498402556, 'pattern_counts': {'":': 0, '<': 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': '3349163', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1044, 'n_words': 285}