Improve portmaster-start update logging
This commit is contained in:
@@ -127,7 +127,7 @@ func initCobra() {
|
|||||||
|
|
||||||
// set up logging
|
// set up logging
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
|
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
|
||||||
log.SetPrefix("[control] ")
|
log.SetPrefix("[pmstart] ")
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
// not using portbase logger
|
// not using portbase logger
|
||||||
@@ -186,7 +186,7 @@ func ensureLoggingDir() error {
|
|||||||
|
|
||||||
func updateRegistryIndex(mustLoadIndex bool) error {
|
func updateRegistryIndex(mustLoadIndex bool) error {
|
||||||
// Set indexes based on the release channel.
|
// Set indexes based on the release channel.
|
||||||
warning := helper.SetIndexes(registry, "", false)
|
warning := helper.SetIndexes(registry, "", false, false, false)
|
||||||
if warning != nil {
|
if warning != nil {
|
||||||
log.Printf("WARNING: %s\n", warning)
|
log.Printf("WARNING: %s\n", warning)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
portlog "github.com/safing/portbase/log"
|
||||||
|
"github.com/safing/portbase/updater"
|
||||||
"github.com/safing/portmaster/updates/helper"
|
"github.com/safing/portmaster/updates/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -58,23 +60,16 @@ func downloadUpdates() error {
|
|||||||
helper.IntelOnly()
|
helper.IntelOnly()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set registry state notify callback.
|
||||||
|
registry.StateNotifyFunc = logProgress
|
||||||
|
|
||||||
// Set required updates.
|
// Set required updates.
|
||||||
registry.MandatoryUpdates = helper.MandatoryUpdates()
|
registry.MandatoryUpdates = helper.MandatoryUpdates()
|
||||||
registry.AutoUnpack = helper.AutoUnpackUpdates()
|
registry.AutoUnpack = helper.AutoUnpackUpdates()
|
||||||
|
|
||||||
// logging is configured as a persistent pre-run method inherited from
|
|
||||||
// the root command but since we don't use run.Run() we need to start
|
|
||||||
// logging ourself.
|
|
||||||
log.SetLogLevel(log.InfoLevel)
|
|
||||||
err := log.Start()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("failed to start logging: %s\n", err)
|
|
||||||
}
|
|
||||||
defer log.Shutdown()
|
|
||||||
|
|
||||||
if reset {
|
if reset {
|
||||||
// Delete storage.
|
// Delete storage.
|
||||||
err = os.RemoveAll(registry.StorageDir().Path)
|
err := os.RemoveAll(registry.StorageDir().Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to reset update dir: %w", err)
|
return fmt.Errorf("failed to reset update dir: %w", err)
|
||||||
}
|
}
|
||||||
@@ -88,11 +83,17 @@ func downloadUpdates() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update all indexes.
|
// Update all indexes.
|
||||||
err = registry.UpdateIndexes(context.TODO())
|
err := registry.UpdateIndexes(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if updates are available.
|
||||||
|
if len(registry.GetState().Updates.PendingDownload) == 0 {
|
||||||
|
log.Println("all resources are up to date")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Download all required updates.
|
// Download all required updates.
|
||||||
err = registry.DownloadUpdates(context.TODO(), false)
|
err = registry.DownloadUpdates(context.TODO(), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -116,17 +117,43 @@ func downloadUpdates() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logProgress(state *updater.RegistryState) {
|
||||||
|
switch state.ID {
|
||||||
|
case updater.StateChecking:
|
||||||
|
if state.Updates.LastCheckAt == nil {
|
||||||
|
log.Println("checking for new versions")
|
||||||
|
}
|
||||||
|
case updater.StateDownloading:
|
||||||
|
if state.Details == nil {
|
||||||
|
log.Printf("downloading %d updates\n", len(state.Updates.PendingDownload))
|
||||||
|
} else if downloadDetails, ok := state.Details.(*updater.StateDownloadingDetails); ok {
|
||||||
|
if downloadDetails.FinishedUpTo < len(downloadDetails.Resources) {
|
||||||
|
log.Printf(
|
||||||
|
"[%d/%d] downloading %s",
|
||||||
|
downloadDetails.FinishedUpTo+1,
|
||||||
|
len(downloadDetails.Resources),
|
||||||
|
downloadDetails.Resources[downloadDetails.FinishedUpTo],
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if state.Updates.LastDownloadAt == nil {
|
||||||
|
log.Println("finalizing downloads")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func purge() error {
|
func purge() error {
|
||||||
log.SetLogLevel(log.TraceLevel)
|
portlog.SetLogLevel(portlog.TraceLevel)
|
||||||
|
|
||||||
// logging is configured as a persistent pre-run method inherited from
|
// logging is configured as a persistent pre-run method inherited from
|
||||||
// the root command but since we don't use run.Run() we need to start
|
// the root command but since we don't use run.Run() we need to start
|
||||||
// logging ourself.
|
// logging ourself.
|
||||||
err := log.Start()
|
err := portlog.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to start logging: %s\n", err)
|
fmt.Printf("failed to start logging: %s\n", err)
|
||||||
}
|
}
|
||||||
defer log.Shutdown()
|
defer portlog.Shutdown()
|
||||||
|
|
||||||
registry.Purge(3)
|
registry.Purge(3)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user