-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
145 lines (126 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime/debug"
"github.com/simulot/immich-go/cmd"
"github.com/simulot/immich-go/cmd/duplicate"
"github.com/simulot/immich-go/cmd/metadata"
"github.com/simulot/immich-go/cmd/stack"
"github.com/simulot/immich-go/cmd/tool"
"github.com/simulot/immich-go/cmd/upload"
"github.com/simulot/immich-go/ui"
"github.com/telemachus/humane"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func getCommitInfo() string {
dirty := false
buildvcs := false
buildinfo, _ := debug.ReadBuildInfo()
for _, s := range buildinfo.Settings {
switch s.Key {
case "vcs.revision":
buildvcs = true
commit = s.Value
case "vcs.modified":
if s.Value == "true" {
dirty = true
}
case "vcs.time":
date = s.Value
}
}
if buildvcs && dirty {
commit += "-dirty"
}
return commit
}
func printVersion() {
fmt.Printf("immich-go %s, commit %s, built at %s\n", version, getCommitInfo(), date)
}
func main() {
var err error
// Create a context with cancel function to gracefully handle Ctrl+C events
ctx, cancel := context.WithCancelCause(context.Background())
// Handle Ctrl+C signal (SIGINT)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)
go func() {
<-signalChannel
fmt.Println("\nCtrl+C received. Shutting down...")
cancel(errors.New("Ctrl+C received")) // Cancel the context when Ctrl+C is received
}()
select {
case <-ctx.Done():
err = ctx.Err()
default:
err = Run(ctx)
}
if err != nil {
if e := context.Cause(ctx); e != nil {
err = e
}
fmt.Println(err.Error())
os.Exit(1)
}
}
func Run(ctx context.Context) error {
app := cmd.SharedFlags{
Log: slog.New(humane.NewHandler(os.Stdout, &humane.Options{Level: slog.LevelInfo})),
Banner: ui.NewBanner(version, commit, date),
}
fs := flag.NewFlagSet("main", flag.ExitOnError)
fs.BoolFunc("version", "Get immich-go version", func(s string) error {
printVersion()
os.Exit(0)
return nil
})
app.InitSharedFlags()
app.SetFlags(fs)
err := fs.Parse(os.Args[1:])
if err != nil {
app.Log.Error(err.Error())
return err
}
printVersion()
fmt.Println(app.Banner.String())
if len(fs.Args()) == 0 {
err = errors.New("missing command upload|duplicate|stack|tool")
}
if err != nil {
app.Log.Error(err.Error())
return err
}
cmd := fs.Args()[0]
switch cmd {
case "upload":
err = upload.UploadCommand(ctx, &app, fs.Args()[1:])
case "duplicate":
err = duplicate.DuplicateCommand(ctx, &app, fs.Args()[1:])
case "metadata":
err = metadata.MetadataCommand(ctx, &app, fs.Args()[1:])
case "stack":
err = stack.NewStackCommand(ctx, &app, fs.Args()[1:])
case "tool":
err = tool.CommandTool(ctx, &app, fs.Args()[1:])
default:
err = fmt.Errorf("unknown command: %q", cmd)
}
if err != nil {
app.Log.Error(err.Error())
}
fmt.Println("Check the log file: ", app.LogFile)
if app.APITraceWriter != nil {
fmt.Println("Check the trace file: ", app.APITraceWriterName)
}
return err
}