diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/cmd.go | 74 | ||||
| -rw-r--r-- | cmd/init.go | 53 | ||||
| -rw-r--r-- | cmd/main.go | 5 |
3 files changed, 132 insertions, 0 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..86a1b92 --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "time" + + "github.com/tidwall/buntdb" + "github.com/urfave/cli/v2" + "golang.org/x/sync/errgroup" +) + +func runCmd(cCtx *cli.Context) error { + ctx, err := initCmd(cCtx) + if err != nil { + return err + } + + db, err := buntdb.Open(ctx.Value("database").(string)) + if err != nil { + return err + } + defer db.Close() + + ctx, stop := signal.NotifyContext(ctx, os.Interrupt) + defer stop() + + test := new(errgroup.Group) + test.SetLimit(3) + for n := 1; n < 10; n++ { + test.Go(func() error { + select { + case <-ctx.Done(): + return nil + default: + fmt.Println("test") + time.Sleep(3 * time.Second) + return nil + } + }) + } + _ = test.Wait() + fmt.Println("reached") + return cCtx.Err() +} + +func start() { + app := &cli.App{ + Name: "reencoder", + Usage: "reencodes and stores info", + UsageText: "reencoder /path/to/folder", + Description: "indexes files, checks for encoder and reencodes", + Args: true, + ArgsUsage: "specify amusic links to download", + Flags: []cli.Flag{ + &cli.PathFlag{ + Name: "path", + Usage: "Path to folder with files to reencode", + Value: ".", + Aliases: []string{"p"}, + }, + &cli.PathFlag{ + Name: "database", + Usage: "Path to database", + Aliases: []string{"d"}, + }, + }, + Action: runCmd, + } + if err := app.Run(os.Args); err != nil { + panic(err.Error()) + } +} 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 +} diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..6d4c482 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + start() +} |
