Skip to content

Commit

Permalink
Merge pull request #464 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.121.0
  • Loading branch information
andyone authored May 2, 2024
2 parents d4fd4ce + 943922c commit 98163ea
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 15 deletions.
1 change: 1 addition & 0 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* + hash
* + httputil
* - initsystem
* ! initsystem/sdnotify
* + jsonutil
* + knf
* + knf/united
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### 12.121.0

- `[initsystem/sdnotify]` Added new package for sending messages to systemd
- `[support/deps]` Updated for compatibility with the latest version of [depsy](https://kaos.sh/depsy)
- `[terminal/tty]` Improved check for systemd

### 12.120.0

- `[knf]` Added methods `Alias` and `Config.Alias`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ go get -u github.com/essentialkaos/ek/v12
* [`hash`](https://kaos.sh/g/ek.v12/hash) — Package hash contains different hash algorithms and utilities
* [`httputil`](https://kaos.sh/g/ek.v12/httputil) — Package provides methods for working with HTTP request/responses
* [`initsystem`](https://kaos.sh/g/ek.v12/initsystem) — Package provides methods for working with different init systems (sysv, upstart, systemd)
* [`initsystem`](https://kaos.sh/g/ek.v12/initsystem/sdnotify) — Package provides methods methods for sending [notifications to systemd](https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#Well-known%20assignments)
* [`jsonutil`](https://kaos.sh/g/ek.v12/jsonutil) — Package provides methods for working with JSON data
* [`knf`](https://kaos.sh/g/ek.v12/knf) — Package provides methods for working with configuration files in [KNF format](https://kaos.sh/knf-spec)
* [`knf/united`](https://kaos.sh/g/ek.v12/knf/united) — Package provides united configuration (_knf + options + environment variables_)
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.120.0"
const VERSION = "12.121.0"

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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/depsy v1.1.0
github.com/essentialkaos/depsy v1.3.0
github.com/essentialkaos/go-linenoise/v3 v3.6.0
golang.org/x/crypto v0.22.0
golang.org/x/sys v0.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk=
github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0=
github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI=
github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8=
github.com/essentialkaos/depsy v1.3.0 h1:CN7bRgBU2jGTHSkg/Sh38eDUn7cvmaTp2sxFt2HpFeU=
github.com/essentialkaos/depsy v1.3.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8=
github.com/essentialkaos/go-linenoise/v3 v3.6.0 h1:deLcrodtLIkcHjNyW/MoQpjznXPVqvwlspxk7s/5YeY=
github.com/essentialkaos/go-linenoise/v3 v3.6.0/go.mod h1:Fi6kLdZdURkXHpRkIiX2nFGORNv81CXTZ2Mn72i/cn0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
19 changes: 19 additions & 0 deletions initsystem/sdnotify/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sdnotify

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

func ExampleConnect() {
err := Connect()

if err != nil {
panic(err.Error())
}

Status("Loading data %d%%", 50)
Ready()
}
89 changes: 89 additions & 0 deletions initsystem/sdnotify/sdnotify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package sdnotify provides methods for sending notifications to systemd
package sdnotify

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

import (
"fmt"
"net"
"os"
)

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

var (
ErrNoSocket = fmt.Errorf("NOTIFY_SOCKET is empty")
ErrNotConnected = fmt.Errorf("Not connected to socket")
)

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

var conn net.Conn

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

// Connect connects systemd to socket
func Connect() error {
var err error

socket := os.Getenv("NOTIFY_SOCKET")

if err != nil {
return ErrNoSocket
}

conn, err = net.Dial("unixgram", socket)

if err != nil {
return fmt.Errorf("Can't connect to socket: %w", err)
}

return nil
}

// Notify sends provided message to systemd
func Notify(msg string) error {
if conn == nil {
return ErrNotConnected
}

_, err := fmt.Fprint(conn, msg)

return err
}

// Ready sends READY message to systemd
func Ready() error {
return Notify("READY=1")
}

// Reloading sends RELOADING message to systemd
func Reloading() error {
return Notify("RELOADING=1")
}

// Stopping sends STOPPING message to systemd
func Stopping() error {
return Notify("STOPPING=1")
}

// MainPID sends MAINPID message with PID to systemd
func MainPID(pid int) error {
return Notify(fmt.Sprintf("MAINPID=%d", pid))
}

// ExtendTimeout sends EXTEND_TIMEOUT_USEC message to systemd
func ExtendTimeout(sec float64) error {
usec := uint(sec * 1_000_000)
return Notify(fmt.Sprintf("EXTEND_TIMEOUT_USEC=%d", usec))
}

// Status sends status message to systemd
func Status(format string, a ...any) error {
return Notify(fmt.Sprintf(format, a...))
}
54 changes: 54 additions & 0 deletions initsystem/sdnotify/sdnotify_stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build windows || darwin
// +build windows darwin

// Package sdnotify provides methods for sending notifications to systemd
package sdnotify

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

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

// ❗ Connect connects systemd to socket
func Connect() error {
panic("UNSUPPORTED")
}

// ❗ Notify sends provided message to systemd
func Notify(msg string) error {
panic("UNSUPPORTED")
}

// ❗ Ready sends READY message to systemd
func Ready() error {
panic("UNSUPPORTED")
}

// ❗ Reloading sends RELOADING message to systemd
func Reloading() error {
panic("UNSUPPORTED")
}

// ❗ Stopping sends STOPPING message to systemd
func Stopping() error {
panic("UNSUPPORTED")
}

// ❗ MainPID sends MAINPID message with PID to systemd
func MainPID(pid int) error {
panic("UNSUPPORTED")
}

// ❗ ExtendTimeout sends EXTEND_TIMEOUT_USEC message to systemd
func ExtendTimeout(sec float64) error {
panic("UNSUPPORTED")
}

// ❗ Status sends status message to systemd
func Status(format string, a ...any) error {
panic("UNSUPPORTED")
}
2 changes: 1 addition & 1 deletion support/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Extract(gomod []byte) []support.Dep {
for _, dep := range depsy.Extract(gomod, false) {
result = append(result, support.Dep{
Version: dep.Version,
Path: dep.Path,
Path: dep.PrettyPath(),
Extra: dep.Extra,
})
}
Expand Down
5 changes: 3 additions & 2 deletions support/support_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,10 @@ func (i *Info) printDependencies() {
fmtutil.Separator(false, "DEPENDENCIES")

for _, dep := range i.Deps {
if dep.Extra == "" {
switch dep.Extra {
case "":
fmtc.Printf(" {s}%8s{!} %s\n", dep.Version, dep.Path)
} else {
default:
fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, dep.Extra)
}
}
Expand Down
3 changes: 1 addition & 2 deletions terminal/tty/tty_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func IsFakeTTY() bool {

// IsSystemd returns true if process started by systemd
func IsSystemd() bool {
return os.Getenv("INVOCATION_ID") != "" ||
os.Getenv("SYSTEMCTL_IGNORE_DEPENDENCIES") != ""
return os.Getppid() == 1
}

// ////////////////////////////////////////////////////////////////////////////////// //
7 changes: 1 addition & 6 deletions terminal/tty/tty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func (s *TTYSuite) TestIsTTY(c *C) {
}

func (s *TTYSuite) TestIsSystemd(c *C) {
os.Setenv("INVOCATION_ID", "")
os.Setenv("SYSTEMCTL_IGNORE_DEPENDENCIES", "")
c.Assert(IsSystemd(), Equals, false)
os.Setenv("INVOCATION_ID", "5d0149bfa2c34b79bccb13074001eb20")
c.Assert(IsSystemd(), Equals, true)
os.Setenv("INVOCATION_ID", "")
IsSystemd()
}

func (s *TTYSuite) TestIsTMUX(c *C) {
Expand Down

0 comments on commit 98163ea

Please sign in to comment.