Skip to content

Commit

Permalink
Merge pull request #502 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.4.0
  • Loading branch information
andyone authored Sep 12, 2024
2 parents e1e17bb + af6b014 commit eb9e7a4
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 12 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

strategy:
matrix:
go: [ '1.21.x', '1.22.x' ]
go: [ '1.22.x', '1.23.x' ]

steps:
- name: Set up Go
Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:

strategy:
matrix:
go: [ '1.21.x', '1.22.x' ]
go: [ '1.22.x', '1.23.x' ]

steps:
- name: Set up Go
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:

strategy:
matrix:
go: [ '1.21.x', '1.22.x' ]
go: [ '1.22.x', '1.23.x' ]

steps:
- name: Set up Go
Expand All @@ -165,6 +165,7 @@ jobs:
SendCoverage:
name: Send Coverage
runs-on: ubuntu-latest
if: success() || failure()

needs: [Linux, macOS]

Expand Down
1 change: 1 addition & 0 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ L + netutil
L - system/process
* - system/procname
L + system/sensors
* + system/sysctl
* + terminal
* ! terminal/tty
* - terminal/input
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

### [13.4.0](https://kaos.sh/ek/13.4.0)

- `[system/sysctl]` Added new package for reading kernel parameters
- `[strutil]` Added method `SqueezeRepeats`

### [13.3.5](https://kaos.sh/ek/13.3.5)

- `[timeutil]` Improved formatting of days and seconds in `MiniDuration`
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<br/>

Auxiliary packages for Go.
Auxiliary packages for Go 1.22+.

### Platform support

Expand Down Expand Up @@ -78,12 +78,13 @@ Currently we support Linux and macOS (_except some packages_). All packages have
- [`spinner`](https://kaos.sh/g/ek.v13/spinner) — Package provides methods for creating spinner animation for long-running tasks
- [`strutil`](https://kaos.sh/g/ek.v13/strutil) — Package provides methods for working with strings
- [`support`](https://kaos.sh/g/ek.v13/support) — Package provides methods for collecting and printing support information about system
- [`system`](https://kaos.sh/g/ek.v13/system) — Package provides methods for working with system data (_system metrics/users_)
- [`system/container`](https://kaos.sh/g/ek.v13/system/container) — Package provides methods for checking container engine info
- [`system/exec`](https://kaos.sh/g/ek.v13/system/exec) — Package provides methods for executing commands
- [`system/process`](https://kaos.sh/g/ek.v13/system/process) — Package provides methods for gathering information about active processes
- [`system/procname`](https://kaos.sh/g/ek.v13/system/procname) — Package provides methods for changing process name in the process tree
- [`system/sensors`](https://kaos.sh/g/ek.v13/system/sensors) — Package provide methods for collecting sensors information
- [`system`](https://kaos.sh/g/ek.v13/system) — Package provides methods for working with system data (_system metrics/users_)
- [`system/sysctl`](https://kaos.sh/g/ek.v13/system/sysctl) — Package provide methods for reading kernel parameters
- [`terminal`](https://kaos.sh/g/ek.v13/terminal) — Package provides methods for printing messages to terminal
- [`terminal/input`](https://kaos.sh/g/ek.v13/terminal/input) — Package provides methods for working with user input
- [`terminal/tty`](https://kaos.sh/g/ek.v13/terminal/tty) — Package provides methods for working with TTY
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/depsy v1.3.0
github.com/essentialkaos/go-linenoise/v3 v3.6.0
golang.org/x/crypto v0.26.0
golang.org/x/sys v0.24.0
golang.org/x/crypto v0.27.0
golang.org/x/sys v0.25.0
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
8 changes: 8 additions & 0 deletions strutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,11 @@ func ExampleIndexByteSkip() {
// 10
// 10
}

func ExampleSqueezeRepeats() {
s := `"john"" "bob" """mary""`
fmt.Println(SqueezeRepeats(s, `" `))

// Output:
// "john" "bob" "mary"
}
24 changes: 24 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ func IndexByteSkip(s string, c byte, skip int) int {
return -1
}

// SqueezeRepeats replaces each sequence of a repeated character that is listed in
// the specified set
func SqueezeRepeats(s string, set string) string {
if s == "" || set == "" {
return s
}

for _, r := range set {
l, rs := len(s), string(r)

for {
s = strings.ReplaceAll(s, rs+rs, rs)

if len(s) == l {
break
}

l = len(s)
}
}

return s
}

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

func appendField(data []string, item string) []string {
Expand Down
6 changes: 6 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ func (s *StrUtilSuite) TestIndexByteSkip(c *C) {
c.Assert(IndexByteSkip("/home/john/projects/test.log", '/', -1), Equals, 10)
}

func (s *StrUtilSuite) TestSqueezeRepeats(c *C) {
c.Assert(SqueezeRepeats("", ""), Equals, "")
c.Assert(SqueezeRepeats("test", ""), Equals, "test")
c.Assert(SqueezeRepeats("1 ---- 2 - 3 -- 4", " -"), Equals, "1 - 2 - 3 - 4")
}

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

func (s *StrUtilSuite) BenchmarkSubstr(c *C) {
Expand Down
44 changes: 44 additions & 0 deletions system/sysctl/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sysctl

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

import (
"fmt"
)

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

func ExampleGet() {
paramValue, err := Get("kernel.random.boot_id")

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

fmt.Printf("Boot ID: %s\n", paramValue)
}

func ExampleGetI() {
paramValue, err := GetI("kernel.pty.max")

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

fmt.Printf("PTY Max: %d\n", paramValue)
}

func ExampleGetI64() {
paramValue, err := GetI64("fs.file-max")

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

fmt.Printf("File max: %d\n", paramValue)
}
76 changes: 76 additions & 0 deletions system/sysctl/sysctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//go:build linux || darwin
// +build linux darwin

// Package sysctl provides methods for reading kernel parameters
package sysctl

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

import (
"fmt"
"strconv"
"strings"
)

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

// binary is sysctl binary name
var binary = "sysctl"

// procFS is path to procfs
var procFS = "/proc/sys"

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

// Get returns kernel parameter value as a string
func Get(param string) (string, error) {
switch {
case param == "":
return "", fmt.Errorf("Kernel parameter name cannot be empty")
case !strings.Contains(param, ".") || strings.ContainsAny(param, " /\n\t"):
return "", fmt.Errorf("Invalid parameter name %q", param)
}

return getKernelParam(param)
}

// GetI returns kernel parameter value as an int
func GetI(param string) (int, error) {
p, err := Get(param)

if err != nil {
return 0, err
}

i, err := strconv.Atoi(p)

if err != nil {
return 0, fmt.Errorf("Can't parse %q parameter as int: %w", param, err)
}

return i, nil
}

// GetI64 returns kernel parameter value as an int64
func GetI64(param string) (int64, error) {
p, err := Get(param)

if err != nil {
return 0, err
}

i, err := strconv.ParseInt(p, 10, 64)

if err != nil {
return 0, fmt.Errorf("Can't parse %q parameter as int64: %w", param, err)
}

return i, nil
}

// ////////////////////////////////////////////////////////////////////////////////// //
21 changes: 21 additions & 0 deletions system/sysctl/sysctl_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sysctl

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

import (
"os/exec"
"strings"
)

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

// getKernelParam reads kernel parameter from procfs
func getKernelParam(param string) (string, error) {
output, err := exec.Command(binary, "-n", param).Output()
return strings.Trim(string(output), "\n\r"), err
}
31 changes: 31 additions & 0 deletions system/sysctl/sysctl_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sysctl

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

import (
"fmt"
"os"
"strings"

"github.com/essentialkaos/ek/v13/path"
)

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

// getKernelParam reads kernel parameter from procfs
func getKernelParam(param string) (string, error) {
p, err := os.ReadFile(path.Clean(path.Join(
procFS, strings.ReplaceAll(param, ".", "/"),
)))

if err != nil {
return "", fmt.Errorf("Can't read parameter %q: %w", param, err)
}

return strings.Trim(string(p), "\n\r"), nil
}
27 changes: 27 additions & 0 deletions system/sysctl/sysctl_stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !linux && !darwin
// +build !linux,!darwin

// Package sysctl provides methods for reading kernel parameters
package sysctl

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

// Get returns kernel parameter value as a string
func Get(param string) (string, error) {
return "", nil
}

// GetI returns kernel parameter value as an int
func GetI(param string) (int, error) {
return 0, nil
}

// GetI64 returns kernel parameter value as an int64
func GetI64(param string) (int64, error) {
return 0, nil
}
Loading

0 comments on commit eb9e7a4

Please sign in to comment.