Documentation ¶
Overview ¶
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
Index ¶
- Constants
- Variables
- func AddGroup(ctx context.Context, groupName string) error
- func AddUser(ctx context.Context, username, fullname, password string) error
- func AssociateUserToGroup(ctx context.Context, username, groupName string) error
- func BootTime() (bootime time.Time, err error)
- func ConvertError(err error) error
- func ConvertUserGroupError(err error) error
- func DefineSudoCommand(args ...string)
- func DefineUser(ctx context.Context, user *user.User, password string) (err error)
- func DeleteUser(ctx context.Context, user *user.User) (err error)
- func DissociateUserFromGroup(ctx context.Context, username, groupName string) error
- func ExpandFromEnvironment(s string, recursive bool) string
- func ExpandParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string
- func ExpandUnixParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string
- func ExpandWindowsParameter(s string, mappingFunc func(string) (string, bool), recursive bool) string
- func GetCurrentUser() (currentUser *user.User, err error)
- func GetDefaultHomeDirectory(username string) (string, error)
- func GetHomeDirectory(username string) (string, error)
- func GetUser(username string) (auser *user.User, err error)
- func HasGroup(groupName string) (found bool, err error)
- func HasUser(username string) (found bool, err error)
- func Hostname() (string, error)
- func IsAdmin(username string) (admin bool, err error)
- func IsCurrentUserAnAdmin() (admin bool, err error)
- func IsUserAdmin(user *user.User) (admin bool, err error)
- func IsWindows() bool
- func LineSeparator() string
- func NodeName() (nodename string, err error)
- func PlatformInformation() (information string, err error)
- func RemoveGroup(ctx context.Context, groupName string) error
- func RemoveUser(ctx context.Context, username string) error
- func RemoveWithPrivileges(ctx context.Context, path string) (err error)
- func SubstituteParameter(parameter ...string) string
- func SubstituteParameterUnix(parameter ...string) string
- func SubstituteParameterWindows(parameter ...string) string
- func SystemInformation() (information string, err error)
- func Uname() (string, error)
- func UnixLineSeparator() string
- func UpTime() (uptime time.Duration, err error)
- func WithPrivileges(cmd *command.CommandAsDifferentUser) (cmdWithPrivileges *command.CommandAsDifferentUser)
- type RAM
- type VirtualMemory
Constants ¶
const ( RootGroup = "root" SudoersGroup = "sudo" )
Variables ¶
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 AssociateUserToGroup ¶ added in v1.44.0
AssociateUserToGroup adds a user to a group.
func ConvertError ¶ added in v1.18.0
ConvertError converts a platform error into a commonerrors
func ConvertUserGroupError ¶ added in v1.44.0
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
DefineUser adds a new user to the platform
func DeleteUser ¶ added in v1.44.0
DeleteUser removes a user from the platform when the user is specified using a `user.User` structure.
func DissociateUserFromGroup ¶ added in v1.44.1
DissociateUserFromGroup removes a user from a group.
func ExpandFromEnvironment ¶ added in v1.52.0
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
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
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
GetCurrentUser returns information about the current platform's user and expands its home directory.
func GetDefaultHomeDirectory ¶ added in v1.52.0
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
GetHomeDirectory returns the home directory of a user.
func GetUser ¶ added in v1.51.0
GetUser returns information about a user and expands its home directory.
func IsCurrentUserAnAdmin ¶ added in v1.49.0
IsCurrentUserAnAdmin states whether the current user is a superuser or not.
func IsUserAdmin ¶ added in v1.49.0
IsUserAdmin states whether the user is a superuser or not. Similar to IsAdmin but may use more checks.
func PlatformInformation ¶
PlatformInformation returns the platform information (equivalent to uname -s).
func RemoveGroup ¶ added in v1.44.0
RemoveGroup removes a group from the platform
func RemoveUser ¶ added in v1.44.0
RemoveUser removes a user from the platform when only the username is known.
func RemoveWithPrivileges ¶ added in v1.50.0
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
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
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
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 ¶
SystemInformation returns the system information (equivalent to uname -a)
func UnixLineSeparator ¶ added in v1.1.0
func UnixLineSeparator() string
UnixLineSeparator returns the line separator on Unix platform.
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 }
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