Skip to content

Commit

Permalink
fix: macos bugss
Browse files Browse the repository at this point in the history
  • Loading branch information
just-hms committed May 6, 2024
1 parent ee81dbb commit 2dbadf8
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 77 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Go Test

on:
push:
branches:
- main

jobs:
test:
name: Test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Install dependencies
run: |
go mod tidy
go mod download
- name: Run tests
run: go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
71 changes: 37 additions & 34 deletions cmd/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"time"

"github.com/just-hms/dow/pkg/logx"
"github.com/just-hms/dow/pkg/osx"
"github.com/just-hms/dow/pkg/termx"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,6 +36,36 @@ func getLastFile(path string) (fs.FileInfo, error) {
return lastFile, nil
}

func waitForDownload(logger logx.Logger, downloadPath string) (fs.FileInfo, string, error) {
spinner := termx.NewSpin(logger)
defer spinner.Flush()

for {
lastFile, err := getLastFile(downloadPath)
if err != nil {
return nil, "", err
}

sourcePath := filepath.Join(downloadPath, lastFile.Name())

waited := false
for osx.IsLocked(sourcePath) {
lastFile, err = os.Stat(sourcePath)
if err != nil {
return nil, "", err
}
waited = true
spinner.Spin("Downloading " + osx.Size(lastFile))
time.Sleep(100 * time.Millisecond)
}
if !waited {
return lastFile, sourcePath, nil
}

time.Sleep(400 * time.Millisecond)
}
}

var mvCmd = &cobra.Command{
Use: "dow",
Short: "move the last downloaded file in the current (or the specified) folder",
Expand All @@ -52,51 +83,23 @@ var mvCmd = &cobra.Command{
return fmt.Errorf("failed to get the dowload path: %v", err)
}

var (
lastFile fs.FileInfo
sourcePath string
)

// TODO: this is basically a wait for folder
// - https://superuser.com/questions/860064/how-can-i-find-all-files-open-within-a-given-directory#:~:text=lsof%20has%20switches,open%20files%20recursively)
// check special names like unconfirmed...

spinner := termx.NewSpin()
for {
lastFile, err = getLastFile(downloadPath)
if err != nil {
return fmt.Errorf("failed to get latest file: %v", err)
}
logger := logx.Logger{}

sourcePath = filepath.Join(downloadPath, lastFile.Name())

waited := false
for osx.IsLocked(sourcePath) {
lastFile, err = os.Stat(sourcePath)
if err != nil {
return fmt.Errorf("failed to get latest file: %v", err)
}
waited = true
spinner.Spin("Downloading " + osx.Size(lastFile))
time.Sleep(100 * time.Millisecond)
}
if !waited {
break
}
time.Sleep(400 * time.Millisecond)
lastFile, sourcePath, err := waitForDownload(logger, downloadPath)
if err != nil {
return fmt.Errorf("failed to get the last downloaded file: %v", err)
}
spinner.Flush()

if time.Since(lastFile.ModTime()) > maxElapsedBeforeAsking && !yesFlag {
fmt.Printf(
logger.Printf(
"%q is older than %v. Proceed? (y/N) ",
lastFile.Name(),
maxElapsedBeforeAsking,
)

resp, _ := termx.Read()
if resp != 'y' {
fmt.Println("No")
logger.Println("No")
return nil
}
fmt.Println()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module github.com/just-hms/dow

go 1.22

require golang.org/x/term v0.18.0
require golang.org/x/term v0.20.0

require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.18.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
15 changes: 15 additions & 0 deletions pkg/logx/logx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package logx

import (
"fmt"
"os"
)

type Logger struct{}

func (l Logger) Printf(format string, v ...any) {
fmt.Fprintf(os.Stderr, format, v...)
}
func (l Logger) Println(a ...any) {
fmt.Fprintln(os.Stderr, a...)
}
7 changes: 4 additions & 3 deletions pkg/osx/osx_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package osx
package osx_test

import (
"os"
"testing"

"github.com/just-hms/dow/pkg/osx"
"github.com/stretchr/testify/require"
)

Expand All @@ -12,13 +13,13 @@ func TestIsLocked(t *testing.T) {

path := "testdata/test.bin"

locked := IsLocked(path)
locked := osx.IsLocked(path)
req.False(locked)

file, err := os.Open(path)
req.NoError(err)
defer file.Close()

locked = IsLocked(path)
locked = osx.IsLocked(path)
req.True(locked)
}
6 changes: 3 additions & 3 deletions pkg/osx/osx_unix.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//go:build linux
//go:build linux || darwin

package osx

import "os/exec"

func IsLocked(filePath string) bool {
cmd := exec.Command("lsof", filePath)
func IsLocked(path string) bool {
cmd := exec.Command("lsof", path)
out, err := cmd.Output()
return err == nil && len(out) != 0
}
30 changes: 0 additions & 30 deletions pkg/osx/osx_windows.go

This file was deleted.

16 changes: 11 additions & 5 deletions pkg/termx/termx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package termx

import (
"fmt"
"os"

"golang.org/x/term"
Expand All @@ -25,20 +24,27 @@ func Read() (rune, error) {

type spinner struct {
count int
l Logger
}

func NewSpin() *spinner {
return &spinner{}
type Logger interface {
Printf(format string, v ...any)
}

func NewSpin(logger Logger) *spinner {
return &spinner{
l: logger,
}
}

var frames = []string{"⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"}

// Spin displays a spinner animation until the context is canceled.
func (s *spinner) Spin(text string) {
fmt.Printf("\r%s %s ", text, frames[s.count])
s.l.Printf("\r%s %s ", text, frames[s.count])
s.count = (s.count + 1) % len(frames)
}

func (s *spinner) Flush() {
fmt.Print("\r")
s.l.Printf("\r")
}

0 comments on commit 2dbadf8

Please sign in to comment.