Skip to content

Commit

Permalink
Merge pull request #462 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.119.0
  • Loading branch information
andyone authored Apr 29, 2024
2 parents 0cc25ed + be40c86 commit a75bcaa
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* + fsutil
* + hash
* + httputil
* ! initsystem
* - initsystem
* + jsonutil
* + knf
* + knf/united
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### 12.119.0

- `[initsystem]` Added [launchd](https://www.launchd.info) support
- `[support/services]` Added package for collecting services info

### 12.118.0

- `[terminal/input]` Added method `SetHistoryCapacity`
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.118.0"
const VERSION = "12.119.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
6 changes: 6 additions & 0 deletions initsystem/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func ExampleSystemd() {
}
}

func ExampleLaunchd() {
if Launchd() {
fmt.Println("Launchd init system is used")
}
}

func ExampleIsPresent() {
serviceName := "crond"

Expand Down
11 changes: 8 additions & 3 deletions initsystem/initsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (

// ////////////////////////////////////////////////////////////////////////////////// //

// SysV returns true if SysV is used on system
// SysV returns true if SysV is used on the system
func SysV() bool {
if sysvStatus != _STATUS_UNKNOWN {
return sysvStatus == _STATUS_PRESENT
Expand All @@ -59,7 +59,7 @@ func SysV() bool {
return sysvStatus == _STATUS_PRESENT
}

// Upstart returns true if Upstart is used on system
// Upstart returns true if Upstart is used on the system
func Upstart() bool {
if upstartStatus != _STATUS_UNKNOWN {
return upstartStatus == _STATUS_PRESENT
Expand All @@ -75,7 +75,7 @@ func Upstart() bool {
return upstartStatus == _STATUS_PRESENT
}

// Systemd returns true if Systemd is used on system
// Systemd returns true if Systemd is used on the system
func Systemd() bool {
if systemdStatus != _STATUS_UNKNOWN {
return systemdStatus == _STATUS_PRESENT
Expand All @@ -91,6 +91,11 @@ func Systemd() bool {
return systemdStatus == _STATUS_PRESENT
}

// Launchd returns true if Launchd is used on the system
func Launchd() bool {
return false
}

// IsPresent returns true if service is present in any init system
func IsPresent(name string) bool {
if hasSystemdService(name) {
Expand Down
82 changes: 68 additions & 14 deletions initsystem/initsystem_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,92 @@ package initsystem
// //
// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ SysV returns true if SysV is used on system
import (
"bytes"
"fmt"
"os/exec"
"strings"

"github.com/essentialkaos/ek/v12/strutil"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// SysV returns true if SysV is used on system
func SysV() bool {
panic("UNSUPPORTED")
return false
}

// Upstart returns true if Upstart is used on system
// Upstart returns true if Upstart is used on system
func Upstart() bool {
panic("UNSUPPORTED")
return false
}

// Systemd returns true if Systemd is used on system
// Systemd returns true if Systemd is used on system
func Systemd() bool {
panic("UNSUPPORTED")
return false
}

// ❗ IsPresent returns true if service is present in any init system
// Launchd returns true if Launchd is used on the system
func Launchd() bool {
return true
}

// IsPresent returns true if service is present in any init system
func IsPresent(name string) bool {
panic("UNSUPPORTED")
return false
isExist, _, _ := getLaunchdStatus(name)
return isExist
}

// IsWorks returns service state
// IsWorks returns service state
func IsWorks(name string) (bool, error) {
panic("UNSUPPORTED")
return false, nil
_, isWorks, err := getLaunchdStatus(name)
return isWorks, err
}

// IsEnabled returns true if auto start enabled for given service
// IsEnabled returns true if auto start enabled for given service
func IsEnabled(name string) (bool, error) {
panic("UNSUPPORTED")
return false, nil
}

// ////////////////////////////////////////////////////////////////////////////////// //

func getLaunchdStatus(name string) (bool, bool, error) {
output, err := exec.Command("launchctl", "list").Output()

if err != nil {
return false, false, fmt.Errorf("launchd returned error")
}

isExist, isWorks := parseLaunchdOutput(output, name)

return isExist, isWorks, nil
}

func parseLaunchdOutput(data []byte, name string) (bool, bool) {
buf := bytes.NewBuffer(data)

for {
line, err := buf.ReadString('\n')

if err != nil {
break
}

procName := strutil.ReadField(line, 2, false, '\t')

if !strings.HasPrefix(procName, name) {
continue
}

procPID := strutil.ReadField(line, 0, false, '\t')

if procPID == "-" {
return true, false
} else {
return true, true
}
}

return false, false
}
44 changes: 44 additions & 0 deletions initsystem/initsystem_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package initsystem

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"testing"

. "github.com/essentialkaos/check"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func Test(t *testing.T) { TestingT(t) }

type InitSuite struct{}

// ////////////////////////////////////////////////////////////////////////////////// //

var _ = Suite(&InitSuite{})

// ////////////////////////////////////////////////////////////////////////////////// //

func (s *InitSuite) TestLaunchdIsPresent(c *C) {
c.Assert(IsPresent("com.apple.unknown"), Equals, false)
c.Assert(IsPresent("com.apple.homed"), Equals, true)
c.Assert(IsPresent("com.apple.cloudphotod"), Equals, true)
}

func (s *InitSuite) TestLaunchdIsWorks(c *C) {
isWorks, err := IsWorks("com.apple.homed")

c.Assert(err, IsNil)
c.Assert(isWorks, Equals, true)

isWorks, err = IsWorks("com.apple.cloudphotod")

c.Assert(err, IsNil)
c.Assert(isWorks, Equals, false)
}
11 changes: 5 additions & 6 deletions initsystem/initsystem_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@ package initsystem
// ❗ SysV returns true if SysV is used on system
func SysV() bool {
panic("UNSUPPORTED")
return false
}

// ❗ Upstart returns true if Upstart is used on system
func Upstart() bool {
panic("UNSUPPORTED")
return false
}

// ❗ Systemd returns true if Systemd is used on system
func Systemd() bool {
panic("UNSUPPORTED")
return false
}

// ❗ Launchd returns true if Launchd is used on the system
func Launchd() bool {
panic("UNSUPPORTED")
}

// ❗ IsPresent returns true if service is present in any init system
func IsPresent(name string) bool {
panic("UNSUPPORTED")
return false
}

// ❗ IsWorks returns service state
func IsWorks(name string) (bool, error) {
panic("UNSUPPORTED")
return false, nil
}

// ❗ IsEnabled returns true if auto start enabled for given service
func IsEnabled(name string) (bool, error) {
panic("UNSUPPORTED")
return false, nil
}
46 changes: 46 additions & 0 deletions support/services/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build !windows
// +build !windows

// Package services provides methods for collecting information about system services
package services

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"github.com/essentialkaos/ek/v12/initsystem"
"github.com/essentialkaos/ek/v12/support"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Collect collect info about services
func Collect(services ...string) []support.Service {
var result []support.Service

for _, s := range services {
service := support.Service{Name: s, Status: support.STATUS_UNKNOWN}

if initsystem.IsPresent(s) {
service.IsPresent = true
service.IsEnabled, _ = initsystem.IsEnabled(s)

isWorks, err := initsystem.IsWorks(s)

switch {
case err == nil && isWorks:
service.Status = support.STATUS_WORKS
case err == nil && !isWorks:
service.Status = support.STATUS_STOPPED
}
}

result = append(result, service)
}

return result
}
18 changes: 18 additions & 0 deletions support/services/services_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package services provides methods for collecting information about system services
package services

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import "github.com/essentialkaos/ek/v12/support"

// ////////////////////////////////////////////////////////////////////////////////// //

// ❗ Collect collect info about services
func Collect(services ...string) []support.Service {
panic("UNSUPPORTED")
}
Loading

0 comments on commit a75bcaa

Please sign in to comment.