mirror of
https://github.com/ddworken/hishtory.git
synced 2025-01-23 06:38:52 +01:00
Part way through migrating to context
This commit is contained in:
parent
9a05dff2e3
commit
e47bcfc993
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/ctx"
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
@ -958,7 +959,7 @@ func testRequestAndReceiveDbDump(t *testing.T, tester shellTester) {
|
|||||||
secretKey := installHishtory(t, tester, "")
|
secretKey := installHishtory(t, tester, "")
|
||||||
|
|
||||||
// Confirm there are no pending dump requests
|
// Confirm there are no pending dump requests
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
165
client/ctx/ctx.go
Normal file
165
client/ctx/ctx.go
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package ctx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/data"
|
||||||
|
"github.com/ddworken/hishtory/shared"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
hishtoryLogger *log.Logger
|
||||||
|
getLoggerOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetLogger() *log.Logger {
|
||||||
|
getLoggerOnce.Do(func() {
|
||||||
|
homedir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to get user's home directory: %v", err))
|
||||||
|
}
|
||||||
|
f, err := os.OpenFile(path.Join(homedir, shared.HISHTORY_PATH, "hishtory.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o660)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to open hishtory.log: %v", err))
|
||||||
|
}
|
||||||
|
// Purposefully not closing the file. Yes, this is a dangling file handle. But hishtory is short lived so this is okay.
|
||||||
|
hishtoryLogger = log.New(f, "\n", log.LstdFlags|log.Lshortfile)
|
||||||
|
})
|
||||||
|
return hishtoryLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenLocalSqliteDb() (*gorm.DB, error) {
|
||||||
|
homedir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get user's home directory: %v", err)
|
||||||
|
}
|
||||||
|
err = os.MkdirAll(path.Join(homedir, shared.HISHTORY_PATH), 0o744)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create ~/.hishtory dir: %v", err)
|
||||||
|
}
|
||||||
|
hishtoryLogger := GetLogger()
|
||||||
|
newLogger := logger.New(
|
||||||
|
hishtoryLogger,
|
||||||
|
logger.Config{
|
||||||
|
SlowThreshold: 100 * time.Millisecond,
|
||||||
|
LogLevel: logger.Warn,
|
||||||
|
IgnoreRecordNotFoundError: false,
|
||||||
|
Colorful: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
db, err := gorm.Open(sqlite.Open(path.Join(homedir, shared.HISHTORY_PATH, shared.DB_PATH)), &gorm.Config{SkipDefaultTransaction: true, Logger: newLogger})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
|
||||||
|
}
|
||||||
|
tx, err := db.DB()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = tx.Ping()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
db.AutoMigrate(&data.HistoryEntry{})
|
||||||
|
db.Exec("PRAGMA journal_mode = WAL")
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type hishtoryContextKey string
|
||||||
|
|
||||||
|
func MakeContext() context.Context {
|
||||||
|
ctx := context.Background()
|
||||||
|
config, err := GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
GetLogger().Fatalf("failed to retrieve config: %v", err)
|
||||||
|
}
|
||||||
|
ctx = context.WithValue(ctx, hishtoryContextKey("config"), config)
|
||||||
|
db, err := OpenLocalSqliteDb()
|
||||||
|
if err != nil {
|
||||||
|
GetLogger().Fatalf("failed to open local DB: %v", err)
|
||||||
|
}
|
||||||
|
ctx = context.WithValue(ctx, hishtoryContextKey("db"), db)
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDbFromContext(ctx context.Context) *gorm.DB {
|
||||||
|
v := ctx.Value(hishtoryContextKey("db"))
|
||||||
|
if v != nil {
|
||||||
|
return v.(*gorm.DB)
|
||||||
|
}
|
||||||
|
GetLogger().Fatalf("failed to find db in ctx")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientConfig struct {
|
||||||
|
// The user secret that is used to derive encryption keys for syncing history entries
|
||||||
|
UserSecret string `json:"user_secret"`
|
||||||
|
// Whether hishtory recording is enabled
|
||||||
|
IsEnabled bool `json:"is_enabled"`
|
||||||
|
// A device ID used to track which history entry came from which device for remote syncing
|
||||||
|
DeviceId string `json:"device_id"`
|
||||||
|
// Used for skipping history entries prefixed with a space in bash
|
||||||
|
LastSavedHistoryLine string `json:"last_saved_history_line"`
|
||||||
|
// Used for uploading history entries that we failed to upload due to a missing network connection
|
||||||
|
HaveMissedUploads bool `json:"have_missed_uploads"`
|
||||||
|
MissedUploadTimestamp int64 `json:"missed_upload_timestamp"`
|
||||||
|
// Used for avoiding double imports of .bash_history
|
||||||
|
HaveCompletedInitialImport bool `json:"have_completed_initial_import"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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, shared.HISHTORY_PATH, shared.CONFIG_PATH))
|
||||||
|
if err != nil {
|
||||||
|
files, err := ioutil.ReadDir(path.Join(homedir, shared.HISHTORY_PATH))
|
||||||
|
if err != nil {
|
||||||
|
return ClientConfig{}, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
|
||||||
|
}
|
||||||
|
filenames := ""
|
||||||
|
for _, file := range files {
|
||||||
|
filenames += file.Name()
|
||||||
|
filenames += ", "
|
||||||
|
}
|
||||||
|
return ClientConfig{}, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, 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)
|
||||||
|
}
|
||||||
|
clientDir := path.Join(homedir, shared.HISHTORY_PATH)
|
||||||
|
err = os.MkdirAll(clientDir, 0o744)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create ~/.hishtory/ folder: %v", err)
|
||||||
|
}
|
||||||
|
err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write config: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -20,20 +20,19 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "embed" // for embedding config.sh
|
_ "embed" // for embedding config.sh
|
||||||
|
|
||||||
"github.com/glebarez/sqlite" // an alternate non-cgo-requiring sqlite driver
|
// an alternate non-cgo-requiring sqlite driver
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/rodaine/table"
|
"github.com/rodaine/table"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/ctx"
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
)
|
)
|
||||||
@ -75,7 +74,7 @@ func getCwd() (string, string, error) {
|
|||||||
|
|
||||||
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
||||||
if len(args) < 6 {
|
if len(args) < 6 {
|
||||||
GetLogger().Printf("BuildHistoryEntry called with args=%#v, which has too few entries! This can happen in specific edge cases for newly opened terminals and is likely not a problem.", args)
|
ctx.GetLogger().Printf("BuildHistoryEntry called with args=%#v, which has too few entries! This can happen in specific edge cases for newly opened terminals and is likely not a problem.", args)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
shell := args[2]
|
shell := args[2]
|
||||||
@ -152,7 +151,7 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
|||||||
entry.Hostname = hostname
|
entry.Hostname = hostname
|
||||||
|
|
||||||
// device ID
|
// device ID
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get device ID when building history entry: %v", err)
|
return nil, fmt.Errorf("failed to get device ID when building history entry: %v", err)
|
||||||
}
|
}
|
||||||
@ -265,7 +264,7 @@ func getLastCommand(history string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func shouldSkipHiddenCommand(historyLine string) (bool, error) {
|
func shouldSkipHiddenCommand(historyLine string) (bool, error) {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -273,7 +272,7 @@ func shouldSkipHiddenCommand(historyLine string) (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
config.LastSavedHistoryLine = historyLine
|
config.LastSavedHistoryLine = historyLine
|
||||||
err = SetConfig(config)
|
err = ctx.SetConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -281,7 +280,7 @@ func shouldSkipHiddenCommand(historyLine string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetUserSecret() (string, error) {
|
func GetUserSecret() (string, error) {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -296,17 +295,17 @@ func Setup(args []string) error {
|
|||||||
fmt.Println("Setting secret hishtory key to " + string(userSecret))
|
fmt.Println("Setting secret hishtory key to " + string(userSecret))
|
||||||
|
|
||||||
// Create and set the config
|
// Create and set the config
|
||||||
var config ClientConfig
|
var config ctx.ClientConfig
|
||||||
config.UserSecret = userSecret
|
config.UserSecret = userSecret
|
||||||
config.IsEnabled = true
|
config.IsEnabled = true
|
||||||
config.DeviceId = uuid.Must(uuid.NewRandom()).String()
|
config.DeviceId = uuid.Must(uuid.NewRandom()).String()
|
||||||
err := SetConfig(config)
|
err := ctx.SetConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to persist config to disk: %v", err)
|
return fmt.Errorf("failed to persist config to disk: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop all existing data
|
// Drop all existing data
|
||||||
db, err := OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open DB: %v", err)
|
return fmt.Errorf("failed to open DB: %v", err)
|
||||||
}
|
}
|
||||||
@ -368,71 +367,8 @@ func DisplayResults(results []*data.HistoryEntry) {
|
|||||||
tbl.Print()
|
tbl.Print()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConfig struct {
|
|
||||||
// The user secret that is used to derive encryption keys for syncing history entries
|
|
||||||
UserSecret string `json:"user_secret"`
|
|
||||||
// Whether hishtory recording is enabled
|
|
||||||
IsEnabled bool `json:"is_enabled"`
|
|
||||||
// A device ID used to track which history entry came from which device for remote syncing
|
|
||||||
DeviceId string `json:"device_id"`
|
|
||||||
// Used for skipping history entries prefixed with a space in bash
|
|
||||||
LastSavedHistoryLine string `json:"last_saved_history_line"`
|
|
||||||
// Used for uploading history entries that we failed to upload due to a missing network connection
|
|
||||||
HaveMissedUploads bool `json:"have_missed_uploads"`
|
|
||||||
MissedUploadTimestamp int64 `json:"missed_upload_timestamp"`
|
|
||||||
// Used for avoiding double imports of .bash_history
|
|
||||||
HaveCompletedInitialImport bool `json:"have_completed_initial_import"`
|
|
||||||
}
|
|
||||||
|
|
||||||
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, shared.HISHTORY_PATH, shared.CONFIG_PATH))
|
|
||||||
if err != nil {
|
|
||||||
files, err := ioutil.ReadDir(path.Join(homedir, shared.HISHTORY_PATH))
|
|
||||||
if err != nil {
|
|
||||||
return ClientConfig{}, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
|
|
||||||
}
|
|
||||||
filenames := ""
|
|
||||||
for _, file := range files {
|
|
||||||
filenames += file.Name()
|
|
||||||
filenames += ", "
|
|
||||||
}
|
|
||||||
return ClientConfig{}, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, 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)
|
|
||||||
}
|
|
||||||
clientDir := path.Join(homedir, shared.HISHTORY_PATH)
|
|
||||||
err = os.MkdirAll(clientDir, 0o744)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create ~/.hishtory/ folder: %v", err)
|
|
||||||
}
|
|
||||||
err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), serializedConfig, 0o600)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to write config: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsEnabled() (bool, error) {
|
func IsEnabled() (bool, error) {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -440,21 +376,21 @@ func IsEnabled() (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Enable() error {
|
func Enable() error {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config.IsEnabled = true
|
config.IsEnabled = true
|
||||||
return SetConfig(config)
|
return ctx.SetConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Disable() error {
|
func Disable() error {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config.IsEnabled = false
|
config.IsEnabled = false
|
||||||
return SetConfig(config)
|
return ctx.SetConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckFatalError(err error) {
|
func CheckFatalError(err error) {
|
||||||
@ -465,7 +401,7 @@ func CheckFatalError(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ImportHistory() (int, error) {
|
func ImportHistory() (int, error) {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -486,7 +422,7 @@ func ImportHistory() (int, error) {
|
|||||||
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
|
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
|
||||||
}
|
}
|
||||||
historyEntries = append(historyEntries, extraEntries...)
|
historyEntries = append(historyEntries, extraEntries...)
|
||||||
db, err := OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@ -517,7 +453,7 @@ func ImportHistory() (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
config.HaveCompletedInitialImport = true
|
config.HaveCompletedInitialImport = true
|
||||||
err = SetConfig(config)
|
err = ctx.SetConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("failed to mark initial import as completed, this may lead to duplicate history entries: %v", err)
|
return 0, fmt.Errorf("failed to mark initial import as completed, this may lead to duplicate history entries: %v", err)
|
||||||
}
|
}
|
||||||
@ -583,7 +519,7 @@ func Install() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = GetConfig()
|
_, err = ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No config, so set up a new installation
|
// No config, so set up a new installation
|
||||||
return Setup(os.Args)
|
return Setup(os.Args)
|
||||||
@ -862,7 +798,7 @@ func assertIdenticalBinaries(bin1Path, bin2Path string) error {
|
|||||||
differences = append(differences, fmt.Sprintf("diff at index %d: %s[%d]=%x, %s[%d]=%x", i, bin1Path, i, b1, bin2Path, i, b2))
|
differences = append(differences, fmt.Sprintf("diff at index %d: %s[%d]=%x, %s[%d]=%x", i, bin1Path, i, b1, bin2Path, i, b2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger := GetLogger()
|
logger := ctx.GetLogger()
|
||||||
for _, d := range differences {
|
for _, d := range differences {
|
||||||
logger.Printf("comparing binaries: %#v\n", d)
|
logger.Printf("comparing binaries: %#v\n", d)
|
||||||
}
|
}
|
||||||
@ -953,63 +889,6 @@ func getServerHostname() string {
|
|||||||
return "https://api.hishtory.dev"
|
return "https://api.hishtory.dev"
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
hishtoryLogger *log.Logger
|
|
||||||
getLoggerOnce sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetLogger() *log.Logger {
|
|
||||||
getLoggerOnce.Do(func() {
|
|
||||||
homedir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("failed to get user's home directory: %v", err))
|
|
||||||
}
|
|
||||||
f, err := os.OpenFile(path.Join(homedir, shared.HISHTORY_PATH, "hishtory.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o660)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("failed to open hishtory.log: %v", err))
|
|
||||||
}
|
|
||||||
// Purposefully not closing the file. Yes, this is a dangling file handle. But hishtory is short lived so this is okay.
|
|
||||||
hishtoryLogger = log.New(f, "\n", log.LstdFlags|log.Lshortfile)
|
|
||||||
})
|
|
||||||
return hishtoryLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenLocalSqliteDb() (*gorm.DB, error) {
|
|
||||||
homedir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get user's home directory: %v", err)
|
|
||||||
}
|
|
||||||
err = os.MkdirAll(path.Join(homedir, shared.HISHTORY_PATH), 0o744)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create ~/.hishtory dir: %v", err)
|
|
||||||
}
|
|
||||||
hishtoryLogger := GetLogger()
|
|
||||||
newLogger := logger.New(
|
|
||||||
hishtoryLogger,
|
|
||||||
logger.Config{
|
|
||||||
SlowThreshold: 100 * time.Millisecond,
|
|
||||||
LogLevel: logger.Warn,
|
|
||||||
IgnoreRecordNotFoundError: false,
|
|
||||||
Colorful: false,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
db, err := gorm.Open(sqlite.Open(path.Join(homedir, shared.HISHTORY_PATH, shared.DB_PATH)), &gorm.Config{SkipDefaultTransaction: true, Logger: newLogger})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
|
|
||||||
}
|
|
||||||
tx, err := db.DB()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = tx.Ping()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
db.AutoMigrate(&data.HistoryEntry{})
|
|
||||||
db.Exec("PRAGMA journal_mode = WAL")
|
|
||||||
return db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ApiGet(path string) ([]byte, error) {
|
func ApiGet(path string) ([]byte, error) {
|
||||||
if os.Getenv("HISHTORY_SIMULATE_NETWORK_ERROR") != "" {
|
if os.Getenv("HISHTORY_SIMULATE_NETWORK_ERROR") != "" {
|
||||||
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
|
return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev")
|
||||||
@ -1028,7 +907,7 @@ func ApiGet(path string) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("failed to read response body from GET %s%s: %v", getServerHostname(), path, err)
|
return nil, fmt.Errorf("failed to read response body from GET %s%s: %v", getServerHostname(), path, err)
|
||||||
}
|
}
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
GetLogger().Printf("ApiGet(%#v): %s\n", path, duration.String())
|
ctx.GetLogger().Printf("ApiGet(%#v): %s\n", path, duration.String())
|
||||||
return respBody, nil
|
return respBody, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1050,7 +929,7 @@ func ApiPost(path, contentType string, data []byte) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("failed to read response body from POST %s: %v", path, err)
|
return nil, fmt.Errorf("failed to read response body from POST %s: %v", path, err)
|
||||||
}
|
}
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
GetLogger().Printf("ApiPost(%#v): %s\n", path, duration.String())
|
ctx.GetLogger().Printf("ApiPost(%#v): %s\n", path, duration.String())
|
||||||
return respBody, nil
|
return respBody, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1089,7 +968,7 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error {
|
|||||||
return fmt.Errorf("failed to create DB entry even with %d retries: %v", i, err)
|
return fmt.Errorf("failed to create DB entry even with %d retries: %v", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncryptAndMarshal(config ClientConfig, entry *data.HistoryEntry) ([]byte, error) {
|
func EncryptAndMarshal(config ctx.ClientConfig, entry *data.HistoryEntry) ([]byte, error) {
|
||||||
encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry)
|
encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to encrypt history entry")
|
return nil, fmt.Errorf("failed to encrypt history entry")
|
||||||
@ -1146,7 +1025,7 @@ func Redact(db *gorm.DB, query string, force bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deleteOnRemoteInstances(historyEntries []*data.HistoryEntry) error {
|
func deleteOnRemoteInstances(historyEntries []*data.HistoryEntry) error {
|
||||||
config, err := GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/ctx"
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
)
|
)
|
||||||
@ -112,7 +113,7 @@ func TestGetUserSecret(t *testing.T) {
|
|||||||
|
|
||||||
func TestPersist(t *testing.T) {
|
func TestPersist(t *testing.T) {
|
||||||
defer shared.BackupAndRestore(t)()
|
defer shared.BackupAndRestore(t)()
|
||||||
db, err := OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
shared.Check(t, err)
|
shared.Check(t, err)
|
||||||
|
|
||||||
entry := data.MakeFakeHistoryEntry("ls ~/")
|
entry := data.MakeFakeHistoryEntry("ls ~/")
|
||||||
@ -131,7 +132,7 @@ func TestPersist(t *testing.T) {
|
|||||||
|
|
||||||
func TestSearch(t *testing.T) {
|
func TestSearch(t *testing.T) {
|
||||||
defer shared.BackupAndRestore(t)()
|
defer shared.BackupAndRestore(t)()
|
||||||
db, err := OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
shared.Check(t, err)
|
shared.Check(t, err)
|
||||||
|
|
||||||
// Insert data
|
// Insert data
|
||||||
@ -157,7 +158,7 @@ func TestSearch(t *testing.T) {
|
|||||||
func TestAddToDbIfNew(t *testing.T) {
|
func TestAddToDbIfNew(t *testing.T) {
|
||||||
// Set up
|
// Set up
|
||||||
defer shared.BackupAndRestore(t)()
|
defer shared.BackupAndRestore(t)()
|
||||||
db, err := OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
shared.Check(t, err)
|
shared.Check(t, err)
|
||||||
|
|
||||||
// Add duplicate entries
|
// Add duplicate entries
|
||||||
|
45
hishtory.go
45
hishtory.go
@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/ctx"
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
@ -36,7 +37,7 @@ func main() {
|
|||||||
case "delete":
|
case "delete":
|
||||||
lib.CheckFatalError(retrieveAdditionalEntriesFromRemote())
|
lib.CheckFatalError(retrieveAdditionalEntriesFromRemote())
|
||||||
lib.CheckFatalError(processDeletionRequests())
|
lib.CheckFatalError(processDeletionRequests())
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
query := strings.Join(os.Args[2:], " ")
|
query := strings.Join(os.Args[2:], " ")
|
||||||
force := false
|
force := false
|
||||||
@ -72,7 +73,7 @@ func main() {
|
|||||||
case "version":
|
case "version":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "status":
|
case "status":
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
fmt.Printf("Hishtory: v0.%s\nEnabled: %v\n", lib.Version, config.IsEnabled)
|
fmt.Printf("Hishtory: v0.%s\nEnabled: %v\n", lib.Version, config.IsEnabled)
|
||||||
fmt.Printf("Secret Key: %s\n", config.UserSecret)
|
fmt.Printf("Secret Key: %s\n", config.UserSecret)
|
||||||
@ -116,7 +117,7 @@ Supported commands:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printDumpStatus(config lib.ClientConfig) {
|
func printDumpStatus(config ctx.ClientConfig) {
|
||||||
dumpRequests, err := getDumpRequests(config)
|
dumpRequests, err := getDumpRequests(config)
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
fmt.Printf("Dump Requests: ")
|
fmt.Printf("Dump Requests: ")
|
||||||
@ -126,7 +127,7 @@ func printDumpStatus(config lib.ClientConfig) {
|
|||||||
fmt.Print("\n")
|
fmt.Print("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDumpRequests(config lib.ClientConfig) ([]*shared.DumpRequest, error) {
|
func getDumpRequests(config ctx.ClientConfig) ([]*shared.DumpRequest, error) {
|
||||||
resp, err := lib.ApiGet("/api/v1/get-dump-requests?user_id=" + data.UserId(config.UserSecret) + "&device_id=" + config.DeviceId)
|
resp, err := lib.ApiGet("/api/v1/get-dump-requests?user_id=" + data.UserId(config.UserSecret) + "&device_id=" + config.DeviceId)
|
||||||
if lib.IsOfflineError(err) {
|
if lib.IsOfflineError(err) {
|
||||||
return []*shared.DumpRequest{}, nil
|
return []*shared.DumpRequest{}, nil
|
||||||
@ -140,7 +141,7 @@ func getDumpRequests(config lib.ClientConfig) ([]*shared.DumpRequest, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processDeletionRequests() error {
|
func processDeletionRequests() error {
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -157,7 +158,7 @@ func processDeletionRequests() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -173,11 +174,11 @@ func processDeletionRequests() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func retrieveAdditionalEntriesFromRemote() error {
|
func retrieveAdditionalEntriesFromRemote() error {
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -204,7 +205,7 @@ func retrieveAdditionalEntriesFromRemote() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func query(query string) {
|
func query(query string) {
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
err = retrieveAdditionalEntriesFromRemote()
|
err = retrieveAdditionalEntriesFromRemote()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -221,7 +222,7 @@ func query(query string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func displayBannerIfSet() error {
|
func displayBannerIfSet() error {
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get config: %v", err)
|
return fmt.Errorf("failed to get config: %v", err)
|
||||||
}
|
}
|
||||||
@ -240,7 +241,7 @@ func displayBannerIfSet() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func maybeUploadSkippedHistoryEntries() error {
|
func maybeUploadSkippedHistoryEntries() error {
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -249,7 +250,7 @@ func maybeUploadSkippedHistoryEntries() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Upload the missing entries
|
// Upload the missing entries
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -258,7 +259,7 @@ func maybeUploadSkippedHistoryEntries() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to retrieve history entries that haven't been uploaded yet: %v", err)
|
return fmt.Errorf("failed to retrieve history entries that haven't been uploaded yet: %v", err)
|
||||||
}
|
}
|
||||||
lib.GetLogger().Printf("Uploading %d history entries that previously failed to upload (query=%#v)\n", len(entries), query)
|
ctx.GetLogger().Printf("Uploading %d history entries that previously failed to upload (query=%#v)\n", len(entries), query)
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
jsonValue, err := lib.EncryptAndMarshal(config, entry)
|
jsonValue, err := lib.EncryptAndMarshal(config, entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -274,7 +275,7 @@ func maybeUploadSkippedHistoryEntries() error {
|
|||||||
// Mark down that we persisted it
|
// Mark down that we persisted it
|
||||||
config.HaveMissedUploads = false
|
config.HaveMissedUploads = false
|
||||||
config.MissedUploadTimestamp = 0
|
config.MissedUploadTimestamp = 0
|
||||||
err = lib.SetConfig(config)
|
err = ctx.SetConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to mark a history entry as uploaded: %v", err)
|
return fmt.Errorf("failed to mark a history entry as uploaded: %v", err)
|
||||||
}
|
}
|
||||||
@ -282,23 +283,23 @@ func maybeUploadSkippedHistoryEntries() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func saveHistoryEntry() {
|
func saveHistoryEntry() {
|
||||||
config, err := lib.GetConfig()
|
config, err := ctx.GetConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("hishtory cannot save an entry because the hishtory config file does not exist, try running `hishtory init` (err=%v)", err)
|
log.Fatalf("hishtory cannot save an entry because the hishtory config file does not exist, try running `hishtory init` (err=%v)", err)
|
||||||
}
|
}
|
||||||
if !config.IsEnabled {
|
if !config.IsEnabled {
|
||||||
lib.GetLogger().Printf("Skipping saving a history entry because hishtory is disabled\n")
|
ctx.GetLogger().Printf("Skipping saving a history entry because hishtory is disabled\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
entry, err := lib.BuildHistoryEntry(os.Args)
|
entry, err := lib.BuildHistoryEntry(os.Args)
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
lib.GetLogger().Printf("Skipping saving a history entry because we failed to build a history entry (was the command prefixed with a space?)\n")
|
ctx.GetLogger().Printf("Skipping saving a history entry because we failed to build a history entry (was the command prefixed with a space?)\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Persist it locally
|
// Persist it locally
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
err = lib.ReliableDbCreate(db, entry)
|
err = lib.ReliableDbCreate(db, entry)
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
@ -309,11 +310,11 @@ func saveHistoryEntry() {
|
|||||||
_, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue)
|
_, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if lib.IsOfflineError(err) {
|
if lib.IsOfflineError(err) {
|
||||||
lib.GetLogger().Printf("Failed to remotely persist hishtory entry because the device is offline!")
|
ctx.GetLogger().Printf("Failed to remotely persist hishtory entry because the device is offline!")
|
||||||
if !config.HaveMissedUploads {
|
if !config.HaveMissedUploads {
|
||||||
config.HaveMissedUploads = true
|
config.HaveMissedUploads = true
|
||||||
config.MissedUploadTimestamp = time.Now().Unix()
|
config.MissedUploadTimestamp = time.Now().Unix()
|
||||||
lib.CheckFatalError(lib.SetConfig(config))
|
lib.CheckFatalError(ctx.SetConfig(config))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
@ -326,7 +327,7 @@ func saveHistoryEntry() {
|
|||||||
if lib.IsOfflineError(err) {
|
if lib.IsOfflineError(err) {
|
||||||
// It is fine to just ignore this, the next command will retry the API and eventually we will respond to any pending dump requests
|
// It is fine to just ignore this, the next command will retry the API and eventually we will respond to any pending dump requests
|
||||||
dumpRequests = []*shared.DumpRequest{}
|
dumpRequests = []*shared.DumpRequest{}
|
||||||
lib.GetLogger().Printf("Failed to check for dump requests because the device is offline!")
|
ctx.GetLogger().Printf("Failed to check for dump requests because the device is offline!")
|
||||||
} else {
|
} else {
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
}
|
}
|
||||||
@ -351,7 +352,7 @@ func saveHistoryEntry() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func export(query string) {
|
func export(query string) {
|
||||||
db, err := lib.OpenLocalSqliteDb()
|
db, err := ctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
err = retrieveAdditionalEntriesFromRemote()
|
err = retrieveAdditionalEntriesFromRemote()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user