87 lines
2.9 KiB
Go
87 lines
2.9 KiB
Go
|
/*
|
||
|
SysUtils - a library to assist with various system-related functions
|
||
|
Copyright (C) 2020 Brent Saner
|
||
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
package exec_extra
|
||
|
|
||
|
import (
|
||
|
"os/exec"
|
||
|
)
|
||
|
|
||
|
func ExecCmdReturn(cmd *exec.Cmd) (exitStatus int, err error) {
|
||
|
// https://stackoverflow.com/a/55055100/733214
|
||
|
err = cmd.Run()
|
||
|
exitErr, _ := err.(*exec.ExitError)
|
||
|
exitStatus = exitErr.ExitCode()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
GetCmdFromStruct takes (a pointer to) a struct and returns a slice of
|
||
|
strings compatible with os/exec.Cmd.
|
||
|
|
||
|
The tag name used can be changed by setting the StructTagCmdArgs variable in this module;
|
||
|
the default is `cmdarg`.
|
||
|
|
||
|
If the tag value is "-", the field will be skipped. Any other tag value(s) are ignored.
|
||
|
|
||
|
Tag value format:
|
||
|
<tag>:"<option>=<value>[,<option>[=<value>],<option>[=<value>]...]"
|
||
|
e.g.
|
||
|
cmdarg:"short=l,long=list"
|
||
|
cmdarg:"short=l"
|
||
|
cmdarg:"long=list"
|
||
|
|
||
|
If the tag value is "-", or <VAR NAME> is not provided, the field will be explicitly skipped.
|
||
|
(This is the default behavior for struct fields not tagged with `cmdarg`.)
|
||
|
|
||
|
If a cmdarg tag is specified but has no `short` or `long` option value, the field will be skipped entirely.
|
||
|
If a field's value is nil, it will be skipped.
|
||
|
Otherwise if a field's value is the zero-value, it will be skipped.
|
||
|
|
||
|
Recognized options:
|
||
|
|
||
|
* short - A short flag for the argument
|
||
|
|
||
|
e.g.:
|
||
|
|
||
|
struct{
|
||
|
// If this is an empty string, it will be replaced with the value of $CWD.
|
||
|
CurrentDir string `envpop:"CWD"`
|
||
|
// This would only populate with $USER if the pointer is nil.
|
||
|
UserName *string `envpop:"USER"`
|
||
|
// This will *always* replace the field's value with the value of $DISPLAY,
|
||
|
// even if not an empty string.
|
||
|
// Note the `force` option.
|
||
|
Display string `envpop:"DISPLAY,force"`
|
||
|
// Likewise, even if not nil, this field's value would be replaced with the value of $SHELL.
|
||
|
Shell *string `envpop:"SHELL,force"`
|
||
|
// This field will be untouched if non-nil, otherwise it will be a pointer to an empty string
|
||
|
// if FOOBAR is undefined.
|
||
|
NonExistentVar *string `envpop:"FOOBAR,allow_empty"`
|
||
|
}
|
||
|
|
||
|
If s is nil, nothing will be done and err will be errs.ErrNilPtr.
|
||
|
If s is not a pointer to a struct, nothing will be done and err will be errs.ErrBadType.
|
||
|
*/
|
||
|
func GetCmdFromStruct[T any](s T, opts ...cmdArgOpt) (cmdSlice []string, err error) {
|
||
|
|
||
|
// TODO
|
||
|
|
||
|
return
|
||
|
}
|