2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-16 08:43:02 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-07 04:32:42 -07:00
|
|
|
"io/ioutil"
|
2019-03-13 04:14:30 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-10-01 11:57:02 -07:00
|
|
|
"path/filepath"
|
2019-03-13 04:14:30 -07:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/distatus/battery"
|
2020-09-15 04:44:53 -07:00
|
|
|
ps "github.com/mitchellh/go-ps"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type environmentInfo interface {
|
|
|
|
getenv(key string) string
|
2020-10-02 12:50:13 -07:00
|
|
|
getcwd() string
|
|
|
|
homeDir() string
|
2020-10-01 11:57:02 -07:00
|
|
|
hasFiles(pattern string) bool
|
2020-10-07 04:32:42 -07:00
|
|
|
hasFolder(folder string) bool
|
|
|
|
getFileContent(file string) string
|
2019-03-13 04:14:30 -07:00
|
|
|
getPathSeperator() string
|
2020-10-12 03:53:54 -07:00
|
|
|
getCurrentUser() string
|
2019-03-13 04:14:30 -07:00
|
|
|
isRunningAsRoot() bool
|
|
|
|
getHostName() (string, error)
|
|
|
|
getRuntimeGOOS() string
|
|
|
|
hasCommand(command string) bool
|
2020-10-16 08:43:02 -07:00
|
|
|
runCommand(command string, args ...string) (string, error)
|
2019-03-13 04:14:30 -07:00
|
|
|
runShellCommand(shell string, command string) string
|
|
|
|
lastErrorCode() int
|
|
|
|
getArgs() *args
|
|
|
|
getBatteryInfo() (*battery.Battery, error)
|
2020-09-24 10:11:56 -07:00
|
|
|
getShellName() string
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type environment struct {
|
|
|
|
args *args
|
2020-10-12 00:04:37 -07:00
|
|
|
cwd string
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2020-10-16 08:43:02 -07:00
|
|
|
type commandError struct {
|
|
|
|
exitCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *commandError) Error() string {
|
|
|
|
return fmt.Sprintf("%d", e.exitCode)
|
|
|
|
}
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
func (env *environment) getenv(key string) string {
|
|
|
|
return os.Getenv(key)
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:50:13 -07:00
|
|
|
func (env *environment) getcwd() string {
|
2020-10-12 00:04:37 -07:00
|
|
|
if env.cwd != "" {
|
|
|
|
return env.cwd
|
|
|
|
}
|
2020-10-08 10:23:20 -07:00
|
|
|
correctPath := func(pwd string) string {
|
|
|
|
// on Windows, and being case sentisitive and not consistent and all, this gives silly issues
|
|
|
|
return strings.Replace(pwd, "c:", "C:", 1)
|
|
|
|
}
|
2020-10-10 10:16:58 -07:00
|
|
|
if env.args != nil && *env.args.PWD != "" {
|
2020-10-12 00:04:37 -07:00
|
|
|
env.cwd = correctPath(*env.args.PWD)
|
|
|
|
return env.cwd
|
2020-10-08 10:23:20 -07:00
|
|
|
}
|
2020-10-02 12:50:13 -07:00
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2020-10-12 00:04:37 -07:00
|
|
|
env.cwd = correctPath(dir)
|
|
|
|
return env.cwd
|
2020-10-02 12:50:13 -07:00
|
|
|
}
|
|
|
|
|
2020-10-01 11:57:02 -07:00
|
|
|
func (env *environment) hasFiles(pattern string) bool {
|
2020-10-02 12:50:13 -07:00
|
|
|
cwd := env.getcwd()
|
2020-10-01 11:57:02 -07:00
|
|
|
pattern = cwd + env.getPathSeperator() + pattern
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(matches) > 0
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:32:42 -07:00
|
|
|
func (env *environment) hasFolder(folder string) bool {
|
|
|
|
_, err := os.Stat(folder)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) getFileContent(file string) string {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(content)
|
|
|
|
}
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
func (env *environment) getPathSeperator() string {
|
|
|
|
return string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
|
2020-10-12 03:53:54 -07:00
|
|
|
func (env *environment) getCurrentUser() string {
|
|
|
|
user := os.Getenv("USER")
|
|
|
|
if user == "" {
|
|
|
|
user = os.Getenv("USERNAME")
|
|
|
|
}
|
|
|
|
return user
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) getHostName() (string, error) {
|
|
|
|
hostName, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cleanHostName(hostName), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) getRuntimeGOOS() string {
|
|
|
|
return runtime.GOOS
|
|
|
|
}
|
|
|
|
|
2020-10-16 08:43:02 -07:00
|
|
|
func (env *environment) runCommand(command string, args ...string) (string, error) {
|
2019-03-13 04:14:30 -07:00
|
|
|
out, err := exec.Command(command, args...).Output()
|
2020-10-16 08:43:02 -07:00
|
|
|
|
|
|
|
var exerr *exec.ExitError
|
|
|
|
if errors.As(err, &exerr) {
|
|
|
|
return "", &commandError{exitCode: exerr.ExitCode()}
|
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
if err != nil {
|
2020-10-16 08:43:02 -07:00
|
|
|
return "", err
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-10-16 08:43:02 -07:00
|
|
|
|
|
|
|
return strings.TrimSpace(string(out)), nil
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) runShellCommand(shell string, command string) string {
|
|
|
|
out, err := exec.Command(shell, "-c", command).Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) hasCommand(command string) bool {
|
|
|
|
_, err := exec.LookPath(command)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) lastErrorCode() int {
|
|
|
|
return *env.args.ErrorCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) getArgs() *args {
|
|
|
|
return env.args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
|
|
|
return battery.Get(0)
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:11:56 -07:00
|
|
|
func (env *environment) getShellName() string {
|
2020-09-15 04:44:53 -07:00
|
|
|
pid := os.Getppid()
|
2020-09-24 10:11:56 -07:00
|
|
|
p, err := ps.FindProcess(pid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
shell := strings.Replace(p.Executable(), ".exe", "", 1)
|
|
|
|
return strings.Trim(shell, " ")
|
2020-09-15 04:44:53 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
func cleanHostName(hostName string) string {
|
|
|
|
garbage := []string{
|
|
|
|
".lan",
|
|
|
|
".local",
|
2020-10-07 08:14:26 -07:00
|
|
|
".localdomain",
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
for _, g := range garbage {
|
2020-10-07 08:14:26 -07:00
|
|
|
if strings.HasSuffix(hostName, g) {
|
|
|
|
hostName = strings.Replace(hostName, g, "", 1)
|
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
return hostName
|
|
|
|
}
|