summaryrefslogtreecommitdiff
path: root/cmd/init.go
diff options
context:
space:
mode:
authorjakka <jakkadoujin@gmail.com>2025-04-12 10:47:05 +0300
committerjakka <jakkadoujin@gmail.com>2025-04-12 10:47:05 +0300
commit6f3071a58211598ee23d1ef6ab17156e15b2e27b (patch)
treef148c730fdf104570da55c04ecb8117bdfa3f1e3 /cmd/init.go
parentd4fb354f5731e945a1eb0388985e504f08d58c22 (diff)
initial commit
Diffstat (limited to 'cmd/init.go')
-rw-r--r--cmd/init.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/cmd/init.go b/cmd/init.go
new file mode 100644
index 0000000..836a04a
--- /dev/null
+++ b/cmd/init.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "runtime"
+
+ "github.com/urfave/cli/v2"
+)
+
+func getLocalStorage() string {
+ switch runtime.GOOS {
+ case "windows":
+ return os.Getenv("APPDATA")
+ case "linux":
+ home, _ := os.UserHomeDir()
+ return filepath.Join(home, ".local", "share")
+ case "darwin":
+ home, _ := os.UserHomeDir()
+ return filepath.Join(home, "Library", "Application Support")
+ default:
+ return ""
+ }
+}
+
+func getDb(cCtx *cli.Context) (context.Context, error) {
+ if cCtx.Path("database") == "" {
+ localFolder := getLocalStorage()
+ if localFolder == "" {
+ return context.WithValue(cCtx.Context, "database", ""), errors.New("failed to locate application data folder")
+ }
+
+ return context.WithValue(cCtx.Context, "database", filepath.Join(localFolder, "reencoder.db")), nil
+ }
+ if _, err := os.Stat(cCtx.Path("database")); err != nil {
+ return context.WithValue(cCtx.Context, "database", ""), err
+ }
+ return context.WithValue(cCtx.Context, "database", cCtx.Path("database")), nil
+}
+
+func initCmd(cCtx *cli.Context) (context.Context, error) {
+ if _, err := os.Stat(cCtx.Path("path")); err != nil {
+ return nil, err
+ }
+
+ ctx, err := getDb(cCtx)
+ if err != nil {
+ return nil, err
+ }
+ return context.WithValue(ctx, "path", cCtx.Path("path")), nil
+}