2022-04-05 07:07:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-15 05:18:49 +02:00
|
|
|
"log"
|
2022-04-05 07:07:01 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-04-05 07:07:01 +02:00
|
|
|
"github.com/ddworken/hishtory/client/lib"
|
2022-04-07 03:18:46 +02:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-04-05 07:07:01 +02:00
|
|
|
)
|
|
|
|
|
2022-04-09 23:37:21 +02:00
|
|
|
var (
|
|
|
|
GitCommit string = "Unknown"
|
|
|
|
Version string = "Unknown"
|
|
|
|
)
|
2022-04-07 07:43:07 +02:00
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func main() {
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
fmt.Println("Must specify a command! Do you mean `hishtory query`?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "saveHistoryEntry":
|
|
|
|
saveHistoryEntry()
|
|
|
|
case "query":
|
|
|
|
query(strings.Join(os.Args[2:], " "))
|
|
|
|
case "export":
|
|
|
|
export()
|
|
|
|
case "init":
|
2022-04-07 03:18:46 +02:00
|
|
|
lib.CheckFatalError(lib.Setup(os.Args))
|
2022-04-05 07:07:01 +02:00
|
|
|
case "install":
|
|
|
|
lib.CheckFatalError(lib.Install())
|
|
|
|
case "enable":
|
|
|
|
lib.CheckFatalError(lib.Enable())
|
|
|
|
case "disable":
|
|
|
|
lib.CheckFatalError(lib.Disable())
|
2022-04-12 07:56:23 +02:00
|
|
|
case "version":
|
|
|
|
fallthrough
|
2022-04-05 07:07:01 +02:00
|
|
|
case "status":
|
|
|
|
config, err := lib.GetConfig()
|
|
|
|
lib.CheckFatalError(err)
|
2022-04-16 19:46:57 +02:00
|
|
|
fmt.Printf("Hishtory: v0.%s\nEnabled: %v\n", Version, config.IsEnabled)
|
|
|
|
fmt.Printf("Secret Key: %s\n", config.UserSecret)
|
|
|
|
if len(os.Args) == 3 && os.Args[2] == "-v" {
|
|
|
|
fmt.Printf("User ID: %s\n", data.UserId(config.UserSecret))
|
|
|
|
fmt.Printf("Device ID: %s\n", config.DeviceId)
|
|
|
|
}
|
|
|
|
fmt.Printf("Commit Hash: %s\n", GitCommit)
|
2022-04-05 07:07:01 +02:00
|
|
|
case "update":
|
2022-04-17 01:02:07 +02:00
|
|
|
lib.CheckFatalError(lib.Update())
|
2022-04-05 07:07:01 +02:00
|
|
|
default:
|
|
|
|
lib.CheckFatalError(fmt.Errorf("unknown command: %s", os.Args[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
func retrieveAdditionalEntriesFromRemote(db *gorm.DB) error {
|
|
|
|
config, err := lib.GetConfig()
|
|
|
|
if err != nil {
|
2022-04-07 03:18:46 +02:00
|
|
|
return err
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-04-16 20:37:43 +02:00
|
|
|
respBody, err := lib.ApiGet("/api/v1/equery?device_id=" + config.DeviceId + "&user_id=" + data.UserId(config.UserSecret))
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
2022-04-16 09:44:47 +02:00
|
|
|
return err
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
var retrievedEntries []*shared.EncHistoryEntry
|
2022-04-08 05:59:40 +02:00
|
|
|
err = json.Unmarshal(respBody, &retrievedEntries)
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load JSON response: %v", err)
|
|
|
|
}
|
|
|
|
for _, entry := range retrievedEntries {
|
2022-04-08 05:59:40 +02:00
|
|
|
decEntry, err := data.DecryptHistoryEntry(config.UserSecret, *entry)
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decrypt history entry from server: %v", err)
|
|
|
|
}
|
|
|
|
lib.AddToDbIfNew(db, decEntry)
|
|
|
|
}
|
2022-04-07 03:18:46 +02:00
|
|
|
return nil
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func query(query string) {
|
2022-04-07 07:43:07 +02:00
|
|
|
db, err := lib.OpenLocalSqliteDb()
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-06 08:31:24 +02:00
|
|
|
lib.CheckFatalError(retrieveAdditionalEntriesFromRemote(db))
|
2022-04-07 07:43:07 +02:00
|
|
|
lib.CheckFatalError(displayBannerIfSet())
|
2022-04-08 05:59:40 +02:00
|
|
|
data, err := data.Search(db, query, 25)
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-09 23:52:10 +02:00
|
|
|
lib.DisplayResults(data)
|
2022-04-05 07:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
func displayBannerIfSet() error {
|
|
|
|
config, err := lib.GetConfig()
|
|
|
|
if err != nil {
|
2022-04-07 07:44:10 +02:00
|
|
|
return fmt.Errorf("failed to get config: %v", err)
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
2022-04-16 09:44:47 +02:00
|
|
|
url := "/api/v1/banner?commit_hash=" + GitCommit + "&device_id=" + config.DeviceId + "&forced_banner=" + os.Getenv("FORCED_BANNER")
|
|
|
|
respBody, err := lib.ApiGet(url)
|
2022-04-07 07:43:07 +02:00
|
|
|
if err != nil {
|
2022-04-16 09:44:47 +02:00
|
|
|
return err
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
2022-04-16 09:44:47 +02:00
|
|
|
if len(respBody) > 0 {
|
|
|
|
fmt.Println(string(respBody))
|
2022-04-07 07:43:07 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func saveHistoryEntry() {
|
|
|
|
config, err := lib.GetConfig()
|
2022-04-15 05:18:49 +02:00
|
|
|
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)
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
if !config.IsEnabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry, err := lib.BuildHistoryEntry(os.Args)
|
|
|
|
lib.CheckFatalError(err)
|
2022-04-15 09:04:49 +02:00
|
|
|
if entry == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
|
|
|
|
// Persist it locally
|
2022-04-07 07:43:07 +02:00
|
|
|
db, err := lib.OpenLocalSqliteDb()
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
|
|
|
result := db.Create(entry)
|
|
|
|
lib.CheckFatalError(result.Error)
|
|
|
|
|
|
|
|
// Persist it remotely
|
2022-04-08 05:59:40 +02:00
|
|
|
encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry)
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-16 20:37:43 +02:00
|
|
|
encEntry.DeviceId = config.DeviceId
|
2022-04-06 08:31:24 +02:00
|
|
|
jsonValue, err := json.Marshal([]shared.EncHistoryEntry{encEntry})
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-16 09:44:47 +02:00
|
|
|
_, err = lib.ApiPost("/api/v1/esubmit", "application/json", jsonValue)
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func export() {
|
2022-04-07 07:43:07 +02:00
|
|
|
db, err := lib.OpenLocalSqliteDb()
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-06 08:31:24 +02:00
|
|
|
lib.CheckFatalError(retrieveAdditionalEntriesFromRemote(db))
|
2022-04-08 05:59:40 +02:00
|
|
|
data, err := data.Search(db, "", 0)
|
2022-04-05 07:07:01 +02:00
|
|
|
lib.CheckFatalError(err)
|
2022-04-07 08:17:58 +02:00
|
|
|
for i := len(data) - 1; i >= 0; i-- {
|
2022-04-07 08:05:30 +02:00
|
|
|
fmt.Println(data[i].Command)
|
2022-04-05 07:07:01 +02:00
|
|
|
}
|
|
|
|
}
|