hishtory/shared/client.go

204 lines
4.6 KiB
Go
Raw Normal View History

package shared
import (
2022-01-09 20:00:53 +01:00
"encoding/json"
"fmt"
2022-01-09 20:00:53 +01:00
"log"
"os"
"os/user"
"path"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/rodaine/table"
)
const (
2022-01-09 20:00:53 +01:00
CONFIG_PATH = ".hishtory.config"
)
func getCwd() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get cwd for last command: %v", err)
}
homedir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user's home directory: %v", err)
}
if strings.HasPrefix(cwd, homedir) {
return strings.Replace(cwd, homedir, "~", 1), nil
}
return cwd, nil
}
func BuildHistoryEntry(args []string) (*HistoryEntry, error) {
var entry HistoryEntry
// exitCode
exitCode, err := strconv.Atoi(args[2])
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.ExitCode = exitCode
// user
user, err := user.Current()
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.LocalUsername = user.Username
// cwd
cwd, err := getCwd()
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.CurrentWorkingDirectory = cwd
// TODO(ddworken): start time
// end time
entry.EndTime = time.Now()
// command
cmd, err := getLastCommand(args[3])
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.Command = cmd
// hostname
hostname, err := os.Hostname()
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.Hostname = hostname
// user secret
userSecret, err := GetUserSecret()
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.UserSecret = userSecret
return &entry, nil
}
func getLastCommand(history string) (string, error) {
return strings.TrimSpace(strings.SplitN(strings.TrimSpace(history), " ", 2)[1]), nil
}
func GetUserSecret() (string, error) {
2022-01-09 20:00:53 +01:00
config, err := GetConfig()
if err != nil {
2022-01-09 20:00:53 +01:00
return "", err
}
2022-01-09 20:00:53 +01:00
return config.UserSecret, nil
}
func Setup(args []string) error {
userSecret := uuid.Must(uuid.NewRandom()).String()
if len(args) > 2 && args[2] != "" {
userSecret = args[2]
}
fmt.Println("Setting secret hishtory key to " + string(userSecret))
2022-01-09 20:00:53 +01:00
var config ClientConfig
config.UserSecret = userSecret
config.IsEnabled = true
return SetConfig(config)
}
2022-01-09 23:34:59 +01:00
func DisplayResults(results []*HistoryEntry, displayHostname bool) {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
2022-01-09 23:34:59 +01:00
tbl := table.New("CWD", "Timestamp", "Exit Code", "Command")
if displayHostname {
tbl = table.New("Hostname", "CWD", "Timestamp", "Exit Code", "Command")
}
tbl.WithHeaderFormatter(headerFmt)
for _, result := range results {
2022-01-09 23:34:59 +01:00
if displayHostname {
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, result.EndTime.Format("Jan 2 2006 15:04:05 MST"), result.ExitCode, result.Command)
} else {
tbl.AddRow(result.CurrentWorkingDirectory, result.EndTime.Format("Jan 2 2006 15:04:05 MST"), result.ExitCode, result.Command)
}
}
tbl.Print()
}
2022-01-09 20:00:53 +01:00
type ClientConfig struct {
UserSecret string `json:"user_secret"`
IsEnabled bool `json:"is_enabled"`
}
func GetConfig() (ClientConfig, error) {
homedir, err := os.UserHomeDir()
if err != nil {
return ClientConfig{}, fmt.Errorf("failed to retrieve homedir: %v", err)
}
data, err := os.ReadFile(path.Join(homedir, CONFIG_PATH))
if err != nil {
return ClientConfig{}, fmt.Errorf("failed to read config file: %v", err)
}
var config ClientConfig
err = json.Unmarshal(data, &config)
if err != nil {
return ClientConfig{}, fmt.Errorf("failed to parse config file: %v", err)
}
return config, nil
}
func SetConfig(config ClientConfig) error {
serializedConfig, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("failed to serialize config: %v", err)
}
homedir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to retrieve homedir: %v", err)
}
err = os.WriteFile(path.Join(homedir, CONFIG_PATH), serializedConfig, 0600)
if err != nil {
return fmt.Errorf("failed to write config: %v", err)
}
return nil
}
func IsEnabled() (bool, error) {
config, err := GetConfig()
if err != nil {
return false, err
}
return config.IsEnabled, nil
}
func Enable() error {
config, err := GetConfig()
if err != nil {
return err
}
config.IsEnabled = true
return SetConfig(config)
}
func Disable() error {
config, err := GetConfig()
if err != nil {
return err
}
config.IsEnabled = false
return SetConfig(config)
}
func CheckFatalError(err error) {
if err != nil {
log.Fatalf("hishtory fatal error: %v", err)
}
}