New command "search file". Update core.

This commit is contained in:
Kleissner
2021-12-13 04:18:07 +01:00
parent 1d4d6c185f
commit 2e8f1a57df
5 changed files with 41 additions and 10 deletions

12
API.go
View File

@@ -24,21 +24,23 @@ import (
// startAPI starts the API if enabled via command line parameter or if the settings are set in the config file.
// Using the command line option always ignores any API settings from the config (including timeout settings).
func startAPI(apiListen []string, apiKey uuid.UUID) {
func startAPI(backend *core.Backend, apiListen []string, apiKey uuid.UUID) {
var api *webapi.WebapiInstance
if len(apiListen) > 0 {
// API listen parameter via command line argument.
// Note that read and write timeouts are set to 0 which means they are not used. SSL is not enabled.
webapi.Start(apiListen, false, "", "", 0, 0, apiKey)
api = webapi.Start(backend, apiListen, false, "", "", 0, 0, apiKey)
} else if len(config.APIListen) != 0 {
// API settings via config file.
webapi.Start(config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite), config.APIKey)
api = webapi.Start(backend, config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite), config.APIKey)
} else {
return
}
webapi.Router.HandleFunc("/console", apiConsole).Methods("GET")
webapi.Router.HandleFunc("/shutdown", apiShutdown).Methods("GET")
api.Router.HandleFunc("/console", apiConsole).Methods("GET")
api.Router.HandleFunc("/shutdown", apiShutdown).Methods("GET")
}
// parseCmdParams parses the "-webapi", "-apikey", and "-watchpid" command line parameters.

View File

@@ -48,6 +48,7 @@ func showHelp(output io.Writer) {
"get block Get block from remote peer\n"+
"log error Set error log output\n"+
"exit Exit\n"+
"search file Search globally for files using the local search index\n"+
"\n")
}
@@ -445,6 +446,32 @@ func userCommands(input io.Reader, output io.Writer, terminateSignal chan struct
case "exit":
core.Filters.LogError("userCommands", "graceful exit via user terminal command\n")
os.Exit(core.ExitGraceful)
case "search file":
text, _, terminate := getUserOptionString(reader, terminateSignal)
if terminate {
return
}
results := backend.SearchIndex.Search(text)
if len(results) == 0 {
fmt.Printf("No results found.\n")
break
}
for _, result := range results {
fmt.Printf("- File ID %s\n", result.FileID.String())
fmt.Printf(" Public Key %s\n", hex.EncodeToString(result.PublicKey.SerializeCompressed()))
fmt.Printf(" Block Number %d\n", result.BlockNumber)
keywords := ""
for n, selector := range result.Selectors {
if n > 0 {
keywords += ", "
}
keywords += selector.Word
}
fmt.Printf(" Found via keywords %s\n", keywords)
}
}
}
}

View File

@@ -70,12 +70,14 @@ func init() {
userAgent := appName + "/" + core.Version
core.Init(userAgent)
backend = core.Init(userAgent)
}
var backend *core.Backend
func main() {
apiListen, apiKey, watchPID := parseCmdParams()
startAPI(apiListen, apiKey)
startAPI(backend, apiListen, apiKey)
go processExitMonitor(watchPID)

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/PeernetOfficial/Cmd
go 1.16
require (
github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8
github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7
)

4
go.sum
View File

@@ -1,5 +1,5 @@
github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8 h1:pzgjxUhS8+FlsOlETO57N9oUb4/+pPOWpxVj+NAAONo=
github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8/go.mod h1:tRL13e2o4/h55LDd+aM9QoGukc+3L3Zy0yK8jAh+PsE=
github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3 h1:DH1Q3pNE0cODEptmQRAUErTsrTdIK2Mifa/3b6bxWtk=
github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3/go.mod h1:tRL13e2o4/h55LDd+aM9QoGukc+3L3Zy0yK8jAh+PsE=
github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w=
github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI=
github.com/enfipy/locker v1.1.0 h1:2zVJ0ky7cS1Vjs0x6OQWFiT2dSEiHrI5/O2KCz1fgGc=