Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename log and journal #157

Merged
merged 9 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser/files/localassets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestLocalAssets(t *testing.T) {
}
ctx := context.Background()

b, err := files.NewLocalFiles(ctx, logger.NewJournal(logger.NoLogger{}), fsys)
b, err := files.NewLocalFiles(ctx, logger.NewJournal(logger.NoLog{}), fsys)
if err != nil {
t.Error(err)
}
Expand Down
6 changes: 3 additions & 3 deletions browser/gp/googlephotos.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var matchers = []matcherFn{
//

func (to *Takeout) solvePuzzle() {
to.jnl.OK("Associating JSON and assets...")
to.jnl.Log.OK("Associating JSON and assets...")
jsonKeys := gen.MapKeys(to.jsonByYear)
sort.Slice(jsonKeys, func(i, j int) bool {
yd := jsonKeys[i].year - jsonKeys[j].year
Expand Down Expand Up @@ -417,7 +417,7 @@ func (to *Takeout) Browse(ctx context.Context) chan *browser.LocalAssetFile {
}

func (to *Takeout) passTwoWalk(ctx context.Context, w fs.FS, assetChan chan *browser.LocalAssetFile) error {
to.jnl.OK("Ready to upload files")
to.jnl.Log.OK("Ready to upload files")
return fs.WalkDir(w, ".", func(name string, d fs.DirEntry, err error) error {
if err != nil {
return nil
Expand Down Expand Up @@ -449,7 +449,7 @@ func (to *Takeout) passTwoWalk(ctx context.Context, w fs.FS, assetChan chan *bro
}
finfo, err := d.Info()
if err != nil {
to.jnl.Error("can't browse: %s", err)
to.jnl.Log.Error("can't browse: %s", err)
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion browser/gp/testgp_bigread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func TestReadBigTakeout(t *testing.T) {
panic(err)
}

j := logger.NewJournal(logger.NewLogger(logger.Info, true, false).SetWriter(f))
l := logger.NewLogger(logger.Info, true, false)
l.SetWriter(f)
j := logger.NewJournal(l)
m, err := filepath.Glob("../../../test-data/full_takeout/*.zip")
if err != nil {
t.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions browser/gp/testgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestBrowse(t *testing.T) {
}
ctx := context.Background()

b, err := NewTakeout(ctx, logger.NewJournal(logger.NoLogger{}), fsys)
b, err := NewTakeout(ctx, logger.NewJournal(logger.NoLog{}), fsys)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestAlbums(t *testing.T) {
t.Error(fsys.err)
return
}
b, err := NewTakeout(ctx, logger.NewJournal(logger.NoLogger{}), fsys)
b, err := NewTakeout(ctx, logger.NewJournal(logger.NoLog{}), fsys)
if err != nil {
t.Error(err)
}
Expand Down
31 changes: 17 additions & 14 deletions cmd/album/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ import (
"sort"
"strconv"

"github.com/simulot/immich-go/immich"
"github.com/simulot/immich-go/cmd"
"github.com/simulot/immich-go/logger"
"github.com/simulot/immich-go/ui"
)

func AlbumCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
func AlbumCommand(ctx context.Context, common *cmd.SharedFlags, args []string) error {
if len(args) > 0 {
cmd := args[0]
args = args[1:]

if cmd == "delete" {
return deleteAlbum(ctx, ic, log, args)
return deleteAlbum(ctx, common, args)
}
}
return fmt.Errorf("tool album need a command: delete")
}

type DeleteAlbumCmd struct {
log *logger.Log
Immich *immich.ImmichClient // Immich client
pattern *regexp.Regexp // album pattern
*cmd.SharedFlags
pattern *regexp.Regexp // album pattern
AssumeYes bool
}

func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
func deleteAlbum(ctx context.Context, common *cmd.SharedFlags, args []string) error {
app := &DeleteAlbumCmd{
log: log,
Immich: ic,
SharedFlags: common,
}
cmd := flag.NewFlagSet("album delete", flag.ExitOnError)
app.SharedFlags.SetFlags(cmd)

cmd.BoolFunc("yes", "When true, assume Yes to all actions", func(s string) error {
var err error
app.AssumeYes, err = strconv.ParseBool(s)
Expand All @@ -47,7 +47,10 @@ func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log,
if err != nil {
return err
}

err = app.SharedFlags.Start(ctx)
if err != nil {
return err
}
args = cmd.Args()
if len(args) > 0 {
re, err := regexp.Compile(args[0])
Expand All @@ -59,7 +62,7 @@ func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log,
app.pattern = regexp.MustCompile(`.*`)
}

albums, err := ic.GetAllAlbums(ctx)
albums, err := app.Immich.GetAllAlbums(ctx)
if err != nil {
return fmt.Errorf("can't get the albums list: %w", err)
}
Expand All @@ -71,7 +74,7 @@ func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log,
if app.pattern.MatchString(al.AlbumName) {
yes := app.AssumeYes
if !yes {
app.log.OK("Delete album '%s'?", al.AlbumName)
app.Jnl.Log.OK("Delete album '%s'?", al.AlbumName)
r, err := ui.ConfirmYesNo(ctx, "Proceed?", "n")
if err != nil {
return err
Expand All @@ -81,12 +84,12 @@ func deleteAlbum(ctx context.Context, ic *immich.ImmichClient, log *logger.Log,
}
}
if yes {
app.log.MessageContinue(logger.OK, "Deleting album '%s'", al.AlbumName)
app.Jnl.Log.MessageContinue(logger.OK, "Deleting album '%s'", al.AlbumName)
err = app.Immich.DeleteAlbum(ctx, al.ID)
if err != nil {
return err
} else {
app.log.MessageTerminate(logger.OK, "done")
app.Jnl.Log.MessageTerminate(logger.OK, "done")
}
}
}
Expand Down
45 changes: 26 additions & 19 deletions cmd/duplicate/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/simulot/immich-go/cmd"
"github.com/simulot/immich-go/helpers/gen"
"github.com/simulot/immich-go/helpers/myflag"
"github.com/simulot/immich-go/immich"
Expand All @@ -19,9 +20,7 @@ import (
)

type DuplicateCmd struct {
logger *logger.Log
Immich *immich.ImmichClient // Immich client

*cmd.SharedFlags
AssumeYes bool // When true, doesn't ask to the user
DateRange immich.DateRange // Set capture date range
IgnoreTZErrors bool // Enable TZ error tolerance
Expand All @@ -35,33 +34,41 @@ type duplicateKey struct {
Name string
}

func NewDuplicateCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Log, args []string) (*DuplicateCmd, error) {
func NewDuplicateCmd(ctx context.Context, common *cmd.SharedFlags, args []string) (*DuplicateCmd, error) {
cmd := flag.NewFlagSet("duplicate", flag.ExitOnError)
validRange := immich.DateRange{}
_ = validRange.Set("1850-01-04,2030-01-01")
app := DuplicateCmd{
logger: logger,
Immich: ic,
SharedFlags: common,
DateRange: validRange,
assetsByID: map[string]*immich.Asset{},
assetsByBaseAndDate: map[duplicateKey][]*immich.Asset{},
}

app.SharedFlags.SetFlags(cmd)

cmd.BoolFunc("ignore-tz-errors", "Ignore timezone difference to check duplicates (default: FALSE).", myflag.BoolFlagFn(&app.IgnoreTZErrors, false))
cmd.BoolFunc("yes", "When true, assume Yes to all actions", myflag.BoolFlagFn(&app.AssumeYes, false))
cmd.Var(&app.DateRange, "date", "Process only documents having a capture date in that range.")
err := cmd.Parse(args)
if err != nil {
return nil, err
}
err = app.SharedFlags.Start(ctx)
if err != nil {
return nil, err
}
return &app, err
}

func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := NewDuplicateCmd(ctx, ic, log, args)
func DuplicateCommand(ctx context.Context, common *cmd.SharedFlags, args []string) error {
app, err := NewDuplicateCmd(ctx, common, args)
if err != nil {
return err
}

dupCount := 0
log.MessageContinue(logger.OK, "Get server's assets...")
app.Jnl.Log.MessageContinue(logger.OK, "Get server's assets...")
err = app.Immich.GetAllAssetsWithFilter(ctx, nil, func(a *immich.Asset) {
if a.IsTrashed {
return
Expand All @@ -88,8 +95,8 @@ func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.
if err != nil {
return err
}
log.MessageTerminate(logger.OK, "%d received", len(app.assetsByID))
log.MessageTerminate(logger.OK, "%d duplicate(s) determined.", dupCount)
app.Jnl.Log.MessageTerminate(logger.OK, "%d received", len(app.assetsByID))
app.Jnl.Log.MessageTerminate(logger.OK, "%d duplicate(s) determined.", dupCount)

keys := gen.MapFilterKeys(app.assetsByBaseAndDate, func(i []*immich.Asset) bool {
return len(i) > 1
Expand All @@ -113,22 +120,22 @@ func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.
return ctx.Err()
default:
l := app.assetsByBaseAndDate[k]
app.logger.OK("There are %d copies of the asset %s, taken on %s ", len(l), k.Name, l[0].ExifInfo.DateTimeOriginal.Format(time.RFC3339))
app.Jnl.Log.OK("There are %d copies of the asset %s, taken on %s ", len(l), k.Name, l[0].ExifInfo.DateTimeOriginal.Format(time.RFC3339))
albums := []immich.AlbumSimplified{}
assetsToDelete := []string{}
sort.Slice(l, func(i, j int) bool { return l[i].ExifInfo.FileSizeInByte < l[j].ExifInfo.FileSizeInByte })
for p, a := range l {
if p < len(l)-1 {
log.OK(" delete %s %dx%d, %s, %s", a.OriginalFileName, a.ExifInfo.ExifImageWidth, a.ExifInfo.ExifImageHeight, ui.FormatBytes(a.ExifInfo.FileSizeInByte), a.OriginalPath)
app.Jnl.Log.OK(" delete %s %dx%d, %s, %s", a.OriginalFileName, a.ExifInfo.ExifImageWidth, a.ExifInfo.ExifImageHeight, ui.FormatBytes(a.ExifInfo.FileSizeInByte), a.OriginalPath)
assetsToDelete = append(assetsToDelete, a.ID)
r, err := app.Immich.GetAssetAlbums(ctx, a.ID)
if err != nil {
log.Error("Can't get asset's albums: %s", err.Error())
app.Jnl.Log.Error("Can't get asset's albums: %s", err.Error())
} else {
albums = append(albums, r...)
}
} else {
log.OK(" keep %s %dx%d, %s, %s", a.OriginalFileName, a.ExifInfo.ExifImageWidth, a.ExifInfo.ExifImageHeight, ui.FormatBytes(a.ExifInfo.FileSizeInByte), a.OriginalPath)
app.Jnl.Log.OK(" keep %s %dx%d, %s, %s", a.OriginalFileName, a.ExifInfo.ExifImageWidth, a.ExifInfo.ExifImageHeight, ui.FormatBytes(a.ExifInfo.FileSizeInByte), a.OriginalPath)
yes := app.AssumeYes
if !app.AssumeYes {
r, err := ui.ConfirmYesNo(ctx, "Proceed?", "n")
Expand All @@ -142,14 +149,14 @@ func DuplicateCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.
if yes {
err = app.Immich.DeleteAssets(ctx, assetsToDelete, false)
if err != nil {
log.Error("Can't delete asset: %s", err.Error())
app.Jnl.Log.Error("Can't delete asset: %s", err.Error())
} else {
log.OK(" Asset removed")
app.Jnl.Log.OK(" Asset removed")
for _, al := range albums {
log.OK(" Update the album %s with the best copy", al.AlbumName)
app.Jnl.Log.OK(" Update the album %s with the best copy", al.AlbumName)
_, err = app.Immich.AddAssetToAlbum(ctx, al.ID, []string{a.ID})
if err != nil {
log.Error("Can't delete asset: %s", err.Error())
app.Jnl.Log.Error("Can't delete asset: %s", err.Error())
}
}
}
Expand Down
38 changes: 21 additions & 17 deletions cmd/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/simulot/immich-go/cmd"
"github.com/simulot/immich-go/helpers/docker"
"github.com/simulot/immich-go/helpers/myflag"
"github.com/simulot/immich-go/immich"
Expand All @@ -16,32 +17,35 @@ import (
)

type MetadataCmd struct {
Immich *immich.ImmichClient // Immich client
Log *logger.Log
*cmd.SharedFlags
DryRun bool
MissingDateDespiteName bool
MissingDate bool
DockerHost string
}

func NewMetadataCmd(ctx context.Context, ic *immich.ImmichClient, logger *logger.Log, args []string) (*MetadataCmd, error) {
func NewMetadataCmd(ctx context.Context, common *cmd.SharedFlags, args []string) (*MetadataCmd, error) {
var err error
cmd := flag.NewFlagSet("metadata", flag.ExitOnError)
app := MetadataCmd{
Immich: ic,
Log: logger,
SharedFlags: common,
}

app.SharedFlags.SetFlags(cmd)
cmd.BoolFunc("dry-run", "display actions, but don't touch the server assets", myflag.BoolFlagFn(&app.DryRun, false))
cmd.BoolFunc("missing-date", "select all assets where the date is missing", myflag.BoolFlagFn(&app.MissingDate, false))
cmd.BoolFunc("missing-date-with-name", "select all assets where the date is missing but the name contains a the date", myflag.BoolFlagFn(&app.MissingDateDespiteName, false))
cmd.StringVar(&app.DockerHost, "docker-host", "local", "Immich's docker host where to inject sidecar file as workaround for the issue #3888. 'local' for local connection, 'ssh://user:password@server' for remote host.")
err = cmd.Parse(args)
if err != nil {
return nil, err
}
err = app.SharedFlags.Start(ctx)
return &app, err
}

func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.Log, args []string) error {
app, err := NewMetadataCmd(ctx, ic, log, args)
func MetadataCommand(ctx context.Context, common *cmd.SharedFlags, args []string) error {
app, err := NewMetadataCmd(ctx, common, args)
if err != nil {
return err
}
Expand All @@ -53,15 +57,15 @@ func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.L
if err != nil {
return err
}
app.Log.OK("Connected to the immich's docker container at %q", app.DockerHost)
app.Jnl.Log.OK("Connected to the immich's docker container at %q", app.DockerHost)
}

app.Log.MessageContinue(logger.OK, "Get server's assets...")
app.Jnl.Log.MessageContinue(logger.OK, "Get server's assets...")
list, err := app.Immich.GetAllAssets(ctx, nil)
if err != nil {
return err
}
app.Log.MessageTerminate(logger.OK, " %d received", len(list))
app.Jnl.Log.MessageTerminate(logger.OK, " %d received", len(list))

type broken struct {
a *immich.Asset
Expand Down Expand Up @@ -110,18 +114,18 @@ func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.L
if b.fixable {
fixable++
}
app.Log.OK("%s, (%s %s): %s", b.a.OriginalPath, b.a.ExifInfo.Make, b.a.ExifInfo.Model, strings.Join(b.reason, ", "))
app.Jnl.Log.OK("%s, (%s %s): %s", b.a.OriginalPath, b.a.ExifInfo.Make, b.a.ExifInfo.Model, strings.Join(b.reason, ", "))
}
app.Log.OK("%d broken assets", len(brockenAssets))
app.Log.OK("Among them, %d can be fixed with current settings", fixable)
app.Jnl.Log.OK("%d broken assets", len(brockenAssets))
app.Jnl.Log.OK("Among them, %d can be fixed with current settings", fixable)

if fixable == 0 {
return nil
}

if app.DryRun {
log.OK("Dry-run mode. Exiting")
log.OK("use -dry-run=false after metadata command")
app.Jnl.Log.OK("Dry-run mode. Exiting")
app.Jnl.Log.OK("use -dry-run=false after metadata command")
return nil
}

Expand All @@ -137,7 +141,7 @@ func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.L
continue
}
a := b.a
app.Log.MessageContinue(logger.OK, "Uploading sidecar for %s... ", a.OriginalPath)
app.Jnl.Log.MessageContinue(logger.OK, "Uploading sidecar for %s... ", a.OriginalPath)
scContent, err := b.SideCar.Bytes()
if err != nil {
return err
Expand All @@ -146,7 +150,7 @@ func MetadataCommand(ctx context.Context, ic *immich.ImmichClient, log *logger.L
if err != nil {
return err
}
app.Log.MessageTerminate(logger.OK, "done")
app.Jnl.Log.MessageTerminate(logger.OK, "done")
}
return nil
}
Loading
Loading