platform

package
v1.62.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	RootGroup    = "root"
	SudoersGroup = "sudo"
)

Variables

View Source
var (

	// UnixVariableNameRegexString defines the schema for variable names on Unix.
	// See https://www.gnu.org/software/bash/manual/bash.html#index-name and https://mywiki.wooledge.org/BashFAQ/006
	UnixVariableNameRegexString = "^[a-zA-Z_][a-zA-Z_0-9]*$"
	// WindowsVariableNameRegexString defines the schema for variable names on Windows.
	// See https://ss64.com/nt/syntax-variables.html
	WindowsVariableNameRegexString = "^[A-Za-z#$'()*+,.?@\\[\\]_`{}~][A-Za-z0-9#$'()*+,.?@\\[\\]_`{}~\\s]*$"

	// IsWindowsVariableName defines a validation rule for variable names on Windows for use with github.com/go-ozzo/ozzo-validation
	IsWindowsVariableName = validation.NewStringRuleWithError(isWindowsVarName, errVariableNameInvalid)
	// IsUnixVariableName defines a validation rule for variable names on Unix for use with github.com/go-ozzo/ozzo-validation
	IsUnixVariableName = validation.NewStringRuleWithError(isUnixVarName, errVariableNameInvalid)
	// IsVariableName defines a validation rule for variable names for use with github.com/go-ozzo/ozzo-validation
	IsVariableName = validation.NewStringRuleWithError(isVarName, errVariableNameInvalid)
)

Functions

func AddGroup added in v1.44.0

func AddGroup(ctx context.Context, groupName string) error

AddGroup creates a group if not already existing.

func AddUser added in v1.44.0

func AddUser(ctx context.Context, username, fullname, password string) error

AddUser adds a new user to the platform

func AssociateUserToGroup added in v1.44.0

func AssociateUserToGroup(ctx context.Context, username, groupName string) error

AssociateUserToGroup adds a user to a group.

func BootTime

func BootTime() (bootime time.Time, err error)

BootTime returns system uptime.

func ConvertError added in v1.18.0

func ConvertError(err error) error

ConvertError converts a platform error into a commonerrors

func ConvertUserGroupError added in v1.44.0

func ConvertUserGroupError(err error) error

ConvertUserGroupError converts errors related to users in common errors.

func DefineSudoCommand added in v1.44.0

func DefineSudoCommand(args ...string)

DefineSudoCommand defines the command to run to be `root` or a user with enough privileges (superuser). e.g.

  • args="sudo" to run commands as `root`
  • args="su", "tom" if `tom` has enough privileges to run the command

func DefineUser added in v1.44.0

func DefineUser(ctx context.Context, user *user.User, password string) (err error)

DefineUser adds a new user to the platform

func DeleteUser added in v1.44.0

func DeleteUser(ctx context.Context, user *user.User) (err error)

DeleteUser removes a user from the platform when the user is specified using a `user.User` structure.

func DissociateUserFromGroup added in v1.44.1

func DissociateUserFromGroup(ctx context.Context, username, groupName string) error

DissociateUserFromGroup removes a user from a group.

func ExpandFromEnvironment added in v1.52.0

func ExpandFromEnvironment(s string, recursive bool) string

ExpandFromEnvironment expands a string containing variables with values from the environment. On unix, it is equivalent to os.ExpandEnv but differs on Windows due to the following issues: - https://learn.microsoft.com/en-gb/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsa?redirectedfrom=MSDN - https://github.com/golang/go/issues/43763 - https://github.com/golang/go/issues/24848

func ExpandParameter added in v1.52.0

func ExpandParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string

ExpandParameter expands a variable expressed in a string `s` with its value returned by the mapping function. If the mapping function returns a string with variables, it will expand them too if recursive is set to true.

func ExpandUnixParameter added in v1.52.0

func ExpandUnixParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string

ExpandUnixParameter expands a ${param} or $param in `s` based on the mapping function See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html os.Expand is used under the bonnet and so, only basic parameter substitution is performed. TODO if os.Expand is not good enough, consider using other libraries such as https://github.com/ganbarodigital/go_shellexpand or https://github.com/mvdan/sh

func ExpandWindowsParameter added in v1.52.0

func ExpandWindowsParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string

ExpandWindowsParameter expands a %param% in `s` based on the mapping function See https://learn.microsoft.com/en-us/previous-versions/troubleshoot/winautomation/product-documentation/best-practices/variables/percentage-character-usage-in-notations https://devblogs.microsoft.com/oldnewthing/20060823-00/?p=29993 https://github.com/golang/go/issues/24848 WARNING: currently the function only works with one parameter substitution in `s`.

func GetCurrentUser added in v1.51.0

func GetCurrentUser() (currentUser *user.User, err error)

GetCurrentUser returns information about the current platform's user and expands its home directory.

func GetDefaultHomeDirectory added in v1.52.0

func GetDefaultHomeDirectory(username string) (string, error)

GetDefaultHomeDirectory returns the default home directory for a user based on the convention listed in https://en.wikipedia.org/wiki/Home_directory#Default_home_directory_per_operating_system

func GetHomeDirectory added in v1.52.0

func GetHomeDirectory(username string) (string, error)

GetHomeDirectory returns the home directory of a user.

func GetUser added in v1.51.0

func GetUser(username string) (auser *user.User, err error)

GetUser returns information about a user and expands its home directory.

func HasGroup added in v1.44.0

func HasGroup(groupName string) (found bool, err error)

HasGroup checks whether a group exists

func HasUser added in v1.44.0

func HasUser(username string) (found bool, err error)

HasUser checks whether a user exists

func Hostname

func Hostname() (string, error)

Hostname returns the hostname.

func IsAdmin added in v1.49.0

func IsAdmin(username string) (admin bool, err error)

IsAdmin states whether the user is a superuser or not.

func IsCurrentUserAnAdmin added in v1.49.0

func IsCurrentUserAnAdmin() (admin bool, err error)

IsCurrentUserAnAdmin states whether the current user is a superuser or not.

func IsUserAdmin added in v1.49.0

func IsUserAdmin(user *user.User) (admin bool, err error)

IsUserAdmin states whether the user is a superuser or not. Similar to IsAdmin but may use more checks.

func IsWindows

func IsWindows() bool

IsWindows checks whether we are running on Windows or not.

func LineSeparator

func LineSeparator() string

LineSeparator returns the line separator.

func NodeName

func NodeName() (nodename string, err error)

NodeName returns the system node name (equivalent to uname -n).

func PlatformInformation

func PlatformInformation() (information string, err error)

PlatformInformation returns the platform information (equivalent to uname -s).

func RemoveGroup added in v1.44.0

func RemoveGroup(ctx context.Context, groupName string) error

RemoveGroup removes a group from the platform

func RemoveUser added in v1.44.0

func RemoveUser(ctx context.Context, username string) error

RemoveUser removes a user from the platform when only the username is known.

func RemoveWithPrivileges added in v1.50.0

func RemoveWithPrivileges(ctx context.Context, path string) (err error)

RemoveWithPrivileges removes a directory even if it is not owned by user (equivalent to sudo rm -rf). It expects the current user to be a superuser.

func SubstituteParameter added in v1.53.0

func SubstituteParameter(parameter ...string) string

SubstituteParameter performs parameter substitution on all platforms. - the first element is the parameter to substitute - if find and replace is also wanted, pass the pattern and the replacement as following arguments in that order.

func SubstituteParameterUnix added in v1.53.0

func SubstituteParameterUnix(parameter ...string) string

SubstituteParameterUnix performs Unix parameter substitution: See https://tldp.org/LDP/abs/html/parameter-substitution.html - the first element is the parameter to substitute - if find and replace is also wanted, pass the pattern and the replacement as following arguments in that order.

func SubstituteParameterWindows added in v1.53.0

func SubstituteParameterWindows(parameter ...string) string

SubstituteParameterWindows performs Windows parameter substitution: See https://ss64.com/nt/syntax-replace.html - the first element is the parameter to substitute - if find and replace is also wanted, pass the pattern and the replacement as following arguments in that order.

func SystemInformation

func SystemInformation() (information string, err error)

SystemInformation returns the system information (equivalent to uname -a)

func Uname

func Uname() (string, error)

func UnixLineSeparator added in v1.1.0

func UnixLineSeparator() string

UnixLineSeparator returns the line separator on Unix platform.

func UpTime

func UpTime() (uptime time.Duration, err error)

UpTime returns system uptime.

func WithPrivileges added in v1.49.0

func WithPrivileges(cmd *command.CommandAsDifferentUser) (cmdWithPrivileges *command.CommandAsDifferentUser)

WithPrivileges redefines a command so that it is run with elevated privileges. For instance, on Linux, if the current user has enough privileges, the command will be run as is. Otherwise, `sudo` will be used if defined as the sudo (See `DefineSudoCommand`). Similar scenario will happen on Windows, although the elevated command is defined using `DefineSudoCommand`.

Types

type RAM

type RAM interface {
	// GetTotal returns total amount of RAM on this system
	GetTotal() uint64
	// GetAvailable returns RAM available for programs to allocate
	GetAvailable() uint64
	// GetUsed returns RAM used by programs
	GetUsed() uint64
	// GetUsedPercent returns Percentage of RAM used by programs
	GetUsedPercent() float64
	// GetFree returns kernel's notion of free memory
	GetFree() uint64
}

func GetRAM

func GetRAM() (ram RAM, err error)

type VirtualMemory

type VirtualMemory struct {
	Total       uint64
	Available   uint64
	Used        uint64
	UsedPercent float64
	Free        uint64
}

func (*VirtualMemory) GetAvailable

func (m *VirtualMemory) GetAvailable() uint64

func (*VirtualMemory) GetFree

func (m *VirtualMemory) GetFree() uint64

func (*VirtualMemory) GetTotal

func (m *VirtualMemory) GetTotal() uint64

func (*VirtualMemory) GetUsed

func (m *VirtualMemory) GetUsed() uint64

func (*VirtualMemory) GetUsedPercent

func (m *VirtualMemory) GetUsedPercent() float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL