mirror of
https://git.um-react.app/um/cli.git
synced 2025-11-28 19:53:01 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bfb5ffddf | ||
|
|
2c9de7c56c | ||
|
|
b374c11c86 | ||
|
|
6493b2c5fc | ||
|
|
f753b9c67d | ||
|
|
8829a3b3ba | ||
|
|
b2ef27761f | ||
|
|
7f7cb66fe5 | ||
|
|
77729cf653 | ||
|
|
b317b89ae9 | ||
|
|
89b629304e | ||
|
|
c0c3bda9ce | ||
|
|
fa8f7a1565 | ||
|
|
d4d5e5ddf4 | ||
|
|
57c1aa3e54 | ||
|
|
b0998d8c8a | ||
|
|
f819726f3e | ||
|
|
fbad7ec450 | ||
|
|
19bc6c466e | ||
|
|
b9e2a38f82 | ||
|
|
673ea8491f | ||
|
|
0d071a82be | ||
|
|
b8e6196248 | ||
|
|
1323fb9e1a | ||
|
|
36df203bdd | ||
|
|
2afc232eb1 | ||
|
|
2abdd47c9c | ||
|
|
8b59bc026d | ||
|
|
91855f8f5b | ||
|
|
7edd326b95 |
@@ -8,7 +8,10 @@ local CreateRelease() = {
|
|||||||
settings: {
|
settings: {
|
||||||
api_key: { from_secret: 'GITEA_API_KEY' },
|
api_key: { from_secret: 'GITEA_API_KEY' },
|
||||||
base_url: 'https://git.unlock-music.dev',
|
base_url: 'https://git.unlock-music.dev',
|
||||||
files: 'dist/*',
|
files: [
|
||||||
|
'um-*.tar.gz',
|
||||||
|
'um-*.zip',
|
||||||
|
],
|
||||||
checksum: 'sha256',
|
checksum: 'sha256',
|
||||||
draft: true,
|
draft: true,
|
||||||
title: '${DRONE_TAG}',
|
title: '${DRONE_TAG}',
|
||||||
@@ -17,25 +20,36 @@ local CreateRelease() = {
|
|||||||
|
|
||||||
|
|
||||||
local StepGoBuild(GOOS, GOARCH) = {
|
local StepGoBuild(GOOS, GOARCH) = {
|
||||||
local filepath = 'dist/um-%s-%s.tar.gz' % [GOOS, GOARCH],
|
local windows = GOOS == 'windows',
|
||||||
|
local archiveExt = if windows then 'zip' else 'tar.gz',
|
||||||
|
local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
|
||||||
|
|
||||||
|
local archive = if windows then [
|
||||||
|
// Ensure zip is installed
|
||||||
|
'command -v zip >/dev/null || (apt update && apt install -y zip)',
|
||||||
|
'zip -9 -j -r "%s" $DIST_DIR' % filepath,
|
||||||
|
] else [
|
||||||
|
'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
|
||||||
|
],
|
||||||
|
|
||||||
name: 'go build %s/%s' % [GOOS, GOARCH],
|
name: 'go build %s/%s' % [GOOS, GOARCH],
|
||||||
image: 'golang:1.22',
|
image: 'golang:1.22',
|
||||||
environment: {
|
environment: {
|
||||||
GOOS: GOOS,
|
GOOS: GOOS,
|
||||||
GOARCH: GOARCH,
|
GOARCH: GOARCH,
|
||||||
GOPROXY: "https://goproxy.io,direct",
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
},
|
},
|
||||||
commands: [
|
commands: [
|
||||||
'DIST_DIR=$(mktemp -d)',
|
'DIST_DIR=$(mktemp -d)',
|
||||||
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
|
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
|
||||||
'mkdir -p dist',
|
'mkdir -p dist',
|
||||||
'tar cz -f %s -C $DIST_DIR .' % filepath,
|
] + archive,
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
local StepUploadArtifact(GOOS, GOARCH) = {
|
local StepUploadArtifact(GOOS, GOARCH) = {
|
||||||
local filename = 'um-%s-%s.tar.gz' % [GOOS, GOARCH],
|
local windows = GOOS == 'windows',
|
||||||
|
local archiveExt = if windows then 'zip' else 'tar.gz',
|
||||||
|
local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
|
||||||
local filepath = 'dist/%s' % filename,
|
local filepath = 'dist/%s' % filename,
|
||||||
local pkgname = '${DRONE_REPO_NAME}-build',
|
local pkgname = '${DRONE_REPO_NAME}-build',
|
||||||
|
|
||||||
@@ -71,7 +85,7 @@ local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
|
|||||||
name: 'go test',
|
name: 'go test',
|
||||||
image: 'golang:1.22',
|
image: 'golang:1.22',
|
||||||
environment: {
|
environment: {
|
||||||
GOPROXY: "https://goproxy.io,direct",
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
},
|
},
|
||||||
commands: ['go test -v ./...'],
|
commands: ['go test -v ./...'],
|
||||||
}] else []
|
}] else []
|
||||||
@@ -100,7 +114,7 @@ local PipelineRelease() = {
|
|||||||
name: 'go test',
|
name: 'go test',
|
||||||
image: 'golang:1.22',
|
image: 'golang:1.22',
|
||||||
environment: {
|
environment: {
|
||||||
GOPROXY: "https://goproxy.io,direct",
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
},
|
},
|
||||||
commands: ['go test -v ./...'],
|
commands: ['go test -v ./...'],
|
||||||
},
|
},
|
||||||
@@ -108,9 +122,17 @@ local PipelineRelease() = {
|
|||||||
StepGoBuild('linux', 'arm64'),
|
StepGoBuild('linux', 'arm64'),
|
||||||
StepGoBuild('linux', '386'),
|
StepGoBuild('linux', '386'),
|
||||||
StepGoBuild('windows', 'amd64'),
|
StepGoBuild('windows', 'amd64'),
|
||||||
|
StepGoBuild('windows', 'arm64'),
|
||||||
StepGoBuild('windows', '386'),
|
StepGoBuild('windows', '386'),
|
||||||
StepGoBuild('darwin', 'amd64'),
|
StepGoBuild('darwin', 'amd64'),
|
||||||
StepGoBuild('darwin', 'arm64'),
|
StepGoBuild('darwin', 'arm64'),
|
||||||
|
{
|
||||||
|
name: 'prepare root',
|
||||||
|
image: 'golang:1.22',
|
||||||
|
commands: [
|
||||||
|
'mv dist/*.tar.gz dist/*.zip ./',
|
||||||
|
],
|
||||||
|
},
|
||||||
CreateRelease(),
|
CreateRelease(),
|
||||||
],
|
],
|
||||||
trigger: {
|
trigger: {
|
||||||
|
|||||||
71
.drone.yml
71
.drone.yml
@@ -17,7 +17,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
@@ -25,9 +26,10 @@ steps:
|
|||||||
image: golang:1.22
|
image: golang:1.22
|
||||||
name: go build linux/amd64
|
name: go build linux/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64.tar.gz"
|
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64-$(git
|
||||||
- sha256sum dist/um-linux-amd64.tar.gz
|
describe --tags --always).tar.gz"
|
||||||
|
- sha256sum dist/um-linux-amd64-$(git describe --tags --always).tar.gz
|
||||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
@@ -53,7 +55,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
|
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||||
|
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
@@ -61,9 +64,10 @@ steps:
|
|||||||
image: golang:1.22
|
image: golang:1.22
|
||||||
name: go build windows/amd64
|
name: go build windows/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64.tar.gz"
|
describe --tags --always).zip" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64-$(git
|
||||||
- sha256sum dist/um-windows-amd64.tar.gz
|
describe --tags --always).zip"
|
||||||
|
- sha256sum dist/um-windows-amd64-$(git describe --tags --always).zip
|
||||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
@@ -89,7 +93,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
@@ -97,9 +102,10 @@ steps:
|
|||||||
image: golang:1.22
|
image: golang:1.22
|
||||||
name: go build darwin/amd64
|
name: go build darwin/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64.tar.gz"
|
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64-$(git
|
||||||
- sha256sum dist/um-darwin-amd64.tar.gz
|
describe --tags --always).tar.gz"
|
||||||
|
- sha256sum dist/um-darwin-amd64-$(git describe --tags --always).tar.gz
|
||||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
@@ -131,7 +137,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
@@ -143,7 +150,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-linux-arm64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-arm64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: arm64
|
GOARCH: arm64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
@@ -155,7 +163,7 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-linux-386.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-386-$(git describe --tags --always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: "386"
|
GOARCH: "386"
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
@@ -167,7 +175,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
|
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||||
|
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
@@ -179,7 +188,21 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-windows-386.tar.gz -C $DIST_DIR .
|
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||||
|
- zip -9 -j -r "dist/um-windows-arm64-$(git describe --tags --always).zip" $DIST_DIR
|
||||||
|
environment:
|
||||||
|
GOARCH: arm64
|
||||||
|
GOOS: windows
|
||||||
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.22
|
||||||
|
name: go build windows/arm64
|
||||||
|
- commands:
|
||||||
|
- DIST_DIR=$(mktemp -d)
|
||||||
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
|
- mkdir -p dist
|
||||||
|
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||||
|
- zip -9 -j -r "dist/um-windows-386-$(git describe --tags --always).zip" $DIST_DIR
|
||||||
environment:
|
environment:
|
||||||
GOARCH: "386"
|
GOARCH: "386"
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
@@ -191,7 +214,8 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
@@ -203,13 +227,18 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- mkdir -p dist
|
||||||
- tar cz -f dist/um-darwin-arm64.tar.gz -C $DIST_DIR .
|
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-arm64-$(git describe --tags
|
||||||
|
--always).tar.gz"
|
||||||
environment:
|
environment:
|
||||||
GOARCH: arm64
|
GOARCH: arm64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
GOPROXY: https://goproxy.io,direct
|
GOPROXY: https://goproxy.io,direct
|
||||||
image: golang:1.22
|
image: golang:1.22
|
||||||
name: go build darwin/arm64
|
name: go build darwin/arm64
|
||||||
|
- commands:
|
||||||
|
- mv dist/*.tar.gz dist/*.zip ./
|
||||||
|
image: golang:1.22
|
||||||
|
name: prepare root
|
||||||
- image: plugins/gitea-release
|
- image: plugins/gitea-release
|
||||||
name: create release
|
name: create release
|
||||||
settings:
|
settings:
|
||||||
@@ -218,7 +247,9 @@ steps:
|
|||||||
base_url: https://git.unlock-music.dev
|
base_url: https://git.unlock-music.dev
|
||||||
checksum: sha256
|
checksum: sha256
|
||||||
draft: true
|
draft: true
|
||||||
files: dist/*
|
files:
|
||||||
|
- um-*.tar.gz
|
||||||
|
- um-*.zip
|
||||||
title: ${DRONE_TAG}
|
title: ${DRONE_TAG}
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,7 @@
|
|||||||
.idea
|
.idea
|
||||||
|
|
||||||
/dist
|
/dist
|
||||||
|
*.exe
|
||||||
|
|
||||||
|
/um-*.tar.gz
|
||||||
|
/um-*.zip
|
||||||
|
|||||||
13
README.md
13
README.md
@@ -6,6 +6,9 @@ Original: Web Edition https://git.unlock-music.dev/um/web
|
|||||||
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
|
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
|
||||||
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
|
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
|
||||||
|
|
||||||
|
> **WARNING**
|
||||||
|
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- [x] All Algorithm Supported By `unlock-music/web`
|
- [x] All Algorithm Supported By `unlock-music/web`
|
||||||
@@ -22,3 +25,13 @@ Original: Web Edition https://git.unlock-music.dev/um/web
|
|||||||
- Drag the encrypted file to `um.exe` (Tested on Windows)
|
- Drag the encrypted file to `um.exe` (Tested on Windows)
|
||||||
- Run: `./um [-o <output dir>] [-i] <input dir/file>`
|
- Run: `./um [-o <output dir>] [-i] <input dir/file>`
|
||||||
- Use `./um -h` to show help menu
|
- Use `./um -h` to show help menu
|
||||||
|
|
||||||
|
## Update CI pipeline
|
||||||
|
|
||||||
|
1. Install [Drone CI binary](https://docs.drone.io/cli/install/)
|
||||||
|
2. Edit `.drone.jsonnet`
|
||||||
|
3. Update drone CI pipeline:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
drone jsonnet --format --stream
|
||||||
|
```
|
||||||
|
|||||||
@@ -18,25 +18,32 @@ type DecoderParams struct {
|
|||||||
}
|
}
|
||||||
type NewDecoderFunc func(p *DecoderParams) Decoder
|
type NewDecoderFunc func(p *DecoderParams) Decoder
|
||||||
|
|
||||||
type decoderItem struct {
|
type DecoderFactory struct {
|
||||||
noop bool
|
noop bool
|
||||||
decoder NewDecoderFunc
|
Suffix string
|
||||||
|
Create NewDecoderFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
var DecoderRegistry = make(map[string][]decoderItem)
|
var DecoderRegistry []DecoderFactory
|
||||||
|
|
||||||
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
||||||
DecoderRegistry[ext] = append(DecoderRegistry[ext],
|
DecoderRegistry = append(DecoderRegistry,
|
||||||
decoderItem{noop: noop, decoder: dispatchFunc})
|
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
|
func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
|
||||||
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
|
var result []DecoderFactory
|
||||||
for _, dec := range DecoderRegistry[ext] {
|
// Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate
|
||||||
|
// all decoders for each extension.
|
||||||
|
name := strings.ToLower(filepath.Base(filename))
|
||||||
|
for _, dec := range DecoderRegistry {
|
||||||
|
if !strings.HasSuffix(name, dec.Suffix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if skipNoop && dec.noop {
|
if skipNoop && dec.noop {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rs = append(rs, dec.decoder)
|
result = append(result, dec)
|
||||||
}
|
}
|
||||||
return
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,4 +61,7 @@ func init() {
|
|||||||
common.RegisterDecoder("kgma", false, NewDecoder)
|
common.RegisterDecoder("kgma", false, NewDecoder)
|
||||||
// Viper
|
// Viper
|
||||||
common.RegisterDecoder("vpr", false, NewDecoder)
|
common.RegisterDecoder("vpr", false, NewDecoder)
|
||||||
|
// Kugou Android
|
||||||
|
common.RegisterDecoder("kgm.flac", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("vpr.flac", false, NewDecoder)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package ncm
|
package ncm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go.uber.org/zap"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"unlock-music.dev/cli/algo/common"
|
"unlock-music.dev/cli/algo/common"
|
||||||
@@ -17,33 +18,49 @@ type ncmMeta interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ncmMetaMusic struct {
|
type ncmMetaMusic struct {
|
||||||
Format string `json:"format"`
|
logger *zap.Logger
|
||||||
MusicName string `json:"musicName"`
|
|
||||||
Artist [][]interface{} `json:"artist"`
|
Format string `json:"format"`
|
||||||
Album string `json:"album"`
|
MusicName string `json:"musicName"`
|
||||||
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
Artist interface{} `json:"artist"`
|
||||||
AlbumPic string `json:"albumPic"`
|
Album string `json:"album"`
|
||||||
Flag int `json:"flag"`
|
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
||||||
Bitrate int `json:"bitrate"`
|
AlbumPic string `json:"albumPic"`
|
||||||
Duration int `json:"duration"`
|
Flag int `json:"flag"`
|
||||||
Alias []interface{} `json:"alias"`
|
Bitrate int `json:"bitrate"`
|
||||||
TransNames []interface{} `json:"transNames"`
|
Duration int `json:"duration"`
|
||||||
|
Alias []interface{} `json:"alias"`
|
||||||
|
TransNames []interface{} `json:"transNames"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
|
||||||
|
ncm := new(ncmMetaMusic)
|
||||||
|
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
|
||||||
|
return ncm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ncmMetaMusic) GetAlbumImageURL() string {
|
func (m *ncmMetaMusic) GetAlbumImageURL() string {
|
||||||
return m.AlbumPic
|
return m.AlbumPic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ncmMetaMusic) GetArtists() (artists []string) {
|
func (m *ncmMetaMusic) GetArtists() []string {
|
||||||
for _, artist := range m.Artist {
|
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
|
||||||
for _, item := range artist {
|
|
||||||
name, ok := item.(string)
|
var artists []string = nil
|
||||||
if ok {
|
if jsonArtists, ok := m.Artist.([][]string); ok {
|
||||||
|
for _, artist := range jsonArtists {
|
||||||
|
for _, name := range artist {
|
||||||
artists = append(artists, name)
|
artists = append(artists, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if artist, ok := m.Artist.(string); ok {
|
||||||
|
// #78: artist is a string type.
|
||||||
|
// https://git.unlock-music.dev/um/cli/issues/78
|
||||||
|
artists = []string{artist}
|
||||||
|
} else {
|
||||||
|
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
|
||||||
}
|
}
|
||||||
return
|
return artists
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ncmMetaMusic) GetTitle() string {
|
func (m *ncmMetaMusic) GetTitle() string {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -30,11 +31,12 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{rd: p.Reader}
|
return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
rd io.ReadSeeker // rd is the original file reader
|
logger *zap.Logger
|
||||||
|
rd io.ReadSeeker // rd is the original file reader
|
||||||
|
|
||||||
offset int
|
offset int
|
||||||
cipher common.StreamDecoder
|
cipher common.StreamDecoder
|
||||||
@@ -74,7 +76,7 @@ func (d *Decoder) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := d.parseMeta(); err != nil {
|
if err := d.parseMeta(); err != nil {
|
||||||
return fmt.Errorf("parse meta failed: %w", err)
|
return fmt.Errorf("parse meta failed: %w (raw json=%s)", err, string(d.metaRaw))
|
||||||
}
|
}
|
||||||
|
|
||||||
d.cipher = newNcmCipher(keyData)
|
d.cipher = newNcmCipher(keyData)
|
||||||
@@ -181,7 +183,7 @@ func (d *Decoder) readCoverData() error {
|
|||||||
func (d *Decoder) parseMeta() error {
|
func (d *Decoder) parseMeta() error {
|
||||||
switch d.metaType {
|
switch d.metaType {
|
||||||
case "music":
|
case "music":
|
||||||
d.meta = new(ncmMetaMusic)
|
d.meta = newNcmMetaMusic(d.logger)
|
||||||
return json.Unmarshal(d.metaRaw, d.meta)
|
return json.Unmarshal(d.metaRaw, d.meta)
|
||||||
case "dj":
|
case "dj":
|
||||||
d.meta = new(ncmMetaDJ)
|
d.meta = new(ncmMetaDJ)
|
||||||
|
|||||||
167
cmd/um/main.go
167
cmd/um/main.go
@@ -5,20 +5,20 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
|
|
||||||
"unlock-music.dev/cli/algo/common"
|
"unlock-music.dev/cli/algo/common"
|
||||||
_ "unlock-music.dev/cli/algo/kgm"
|
_ "unlock-music.dev/cli/algo/kgm"
|
||||||
_ "unlock-music.dev/cli/algo/kwm"
|
_ "unlock-music.dev/cli/algo/kwm"
|
||||||
@@ -28,14 +28,13 @@ import (
|
|||||||
_ "unlock-music.dev/cli/algo/xiami"
|
_ "unlock-music.dev/cli/algo/xiami"
|
||||||
_ "unlock-music.dev/cli/algo/ximalaya"
|
_ "unlock-music.dev/cli/algo/ximalaya"
|
||||||
"unlock-music.dev/cli/internal/ffmpeg"
|
"unlock-music.dev/cli/internal/ffmpeg"
|
||||||
"unlock-music.dev/cli/internal/logging"
|
|
||||||
"unlock-music.dev/cli/internal/sniff"
|
"unlock-music.dev/cli/internal/sniff"
|
||||||
"unlock-music.dev/cli/internal/utils"
|
"unlock-music.dev/cli/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AppVersion = "v0.2.2"
|
var AppVersion = "v0.2.10"
|
||||||
|
|
||||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
var logger = setupLogger(false) // TODO: inject logger to application, instead of using global logger
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
module, ok := debug.ReadBuildInfo()
|
module, ok := debug.ReadBuildInfo()
|
||||||
@@ -54,6 +53,7 @@ func main() {
|
|||||||
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
|
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
|
||||||
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
|
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
|
||||||
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||||
|
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
|
||||||
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
|
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
|
||||||
&cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false},
|
&cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false},
|
||||||
&cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false},
|
&cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false},
|
||||||
@@ -66,6 +66,7 @@ func main() {
|
|||||||
HideHelpCommand: true,
|
HideHelpCommand: true,
|
||||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("run app failed", zap.Error(err))
|
logger.Fatal("run app failed", zap.Error(err))
|
||||||
@@ -74,16 +75,50 @@ func main() {
|
|||||||
|
|
||||||
func printSupportedExtensions() {
|
func printSupportedExtensions() {
|
||||||
var exts []string
|
var exts []string
|
||||||
for ext := range common.DecoderRegistry {
|
extSet := make(map[string]int)
|
||||||
|
for _, factory := range common.DecoderRegistry {
|
||||||
|
ext := strings.TrimPrefix(factory.Suffix, ".")
|
||||||
|
if n, ok := extSet[ext]; ok {
|
||||||
|
extSet[ext] = n + 1
|
||||||
|
} else {
|
||||||
|
extSet[ext] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for ext := range extSet {
|
||||||
exts = append(exts, ext)
|
exts = append(exts, ext)
|
||||||
}
|
}
|
||||||
sort.Strings(exts)
|
sort.Strings(exts)
|
||||||
for _, ext := range exts {
|
for _, ext := range exts {
|
||||||
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
|
fmt.Printf("%s: %d\n", ext, extSet[ext])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupLogger(verbose bool) *zap.Logger {
|
||||||
|
logConfig := zap.NewProductionEncoderConfig()
|
||||||
|
logConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
logConfig.EncodeTime = zapcore.RFC3339TimeEncoder
|
||||||
|
enabler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
|
||||||
|
if verbose {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return level >= zapcore.InfoLevel
|
||||||
|
})
|
||||||
|
|
||||||
|
return zap.New(zapcore.NewCore(
|
||||||
|
zapcore.NewConsoleEncoder(logConfig),
|
||||||
|
os.Stdout,
|
||||||
|
enabler,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
func appMain(c *cli.Context) (err error) {
|
func appMain(c *cli.Context) (err error) {
|
||||||
|
logger = setupLogger(c.Bool("verbose"))
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if c.Bool("supported-ext") {
|
if c.Bool("supported-ext") {
|
||||||
printSupportedExtensions()
|
printSupportedExtensions()
|
||||||
return nil
|
return nil
|
||||||
@@ -92,10 +127,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
if input == "" {
|
if input == "" {
|
||||||
switch c.Args().Len() {
|
switch c.Args().Len() {
|
||||||
case 0:
|
case 0:
|
||||||
input, err = os.Getwd()
|
input = cwd
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
input = c.Args().Get(0)
|
input = c.Args().Get(0)
|
||||||
default:
|
default:
|
||||||
@@ -103,23 +135,33 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output := c.String("output")
|
input, absErr := filepath.Abs(input)
|
||||||
if output == "" {
|
if absErr != nil {
|
||||||
var err error
|
return fmt.Errorf("get abs path failed: %w", absErr)
|
||||||
output, err = os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if input == output {
|
|
||||||
return errors.New("input and output path are same")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output := c.String("output")
|
||||||
inputStat, err := os.Stat(input)
|
inputStat, err := os.Stat(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var inputDir string
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
inputDir = input
|
||||||
|
} else {
|
||||||
|
inputDir = path.Dir(input)
|
||||||
|
}
|
||||||
|
inputDir, absErr = filepath.Abs(inputDir)
|
||||||
|
if absErr != nil {
|
||||||
|
return fmt.Errorf("get abs path (inputDir) failed: %w", absErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if output == "" {
|
||||||
|
// Default to where the input dir is
|
||||||
|
output = inputDir
|
||||||
|
}
|
||||||
|
|
||||||
outputStat, err := os.Stat(output)
|
outputStat, err := os.Stat(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -142,6 +184,8 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proc := &processor{
|
proc := &processor{
|
||||||
|
logger: logger,
|
||||||
|
inputDir: inputDir,
|
||||||
outputDir: output,
|
outputDir: output,
|
||||||
skipNoopDecoder: c.Bool("skip-noop"),
|
skipNoopDecoder: c.Bool("skip-noop"),
|
||||||
removeSource: c.Bool("remove-source"),
|
removeSource: c.Bool("remove-source"),
|
||||||
@@ -150,8 +194,8 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inputStat.IsDir() {
|
if inputStat.IsDir() {
|
||||||
wacthDir := c.Bool("watch")
|
watchDir := c.Bool("watch")
|
||||||
if !wacthDir {
|
if !watchDir {
|
||||||
return proc.processDir(input)
|
return proc.processDir(input)
|
||||||
} else {
|
} else {
|
||||||
return proc.watchDir(input)
|
return proc.watchDir(input)
|
||||||
@@ -163,6 +207,8 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
inputDir string
|
||||||
outputDir string
|
outputDir string
|
||||||
|
|
||||||
skipNoopDecoder bool
|
skipNoopDecoder bool
|
||||||
@@ -230,29 +276,32 @@ func (p *processor) processDir(inputDir string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastError error = nil
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if item.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath := filepath.Join(inputDir, item.Name())
|
filePath := filepath.Join(inputDir, item.Name())
|
||||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
if item.IsDir() {
|
||||||
if len(allDec) == 0 {
|
if err = p.processDir(filePath); err != nil {
|
||||||
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name()))
|
lastError = err
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.process(filePath, allDec); err != nil {
|
if err := p.processFile(filePath); err != nil {
|
||||||
|
lastError = err
|
||||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if lastError != nil {
|
||||||
|
return fmt.Errorf("last error: %w", lastError)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) processFile(filePath string) error {
|
func (p *processor) processFile(filePath string) error {
|
||||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||||
if len(allDec) == 0 {
|
if len(allDec) == 0 {
|
||||||
logger.Fatal("skipping while no suitable decoder")
|
return errors.New("skipping while no suitable decoder")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.process(filePath, allDec); err != nil {
|
if err := p.process(filePath, allDec); err != nil {
|
||||||
@@ -270,7 +319,19 @@ func (p *processor) processFile(filePath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error {
|
func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) {
|
||||||
|
for _, factory := range decoders {
|
||||||
|
dec := factory.Create(params)
|
||||||
|
err := dec.Validate()
|
||||||
|
if err == nil {
|
||||||
|
return &dec, &factory, nil
|
||||||
|
}
|
||||||
|
logger.Warn("try decode failed", zap.Error(err))
|
||||||
|
}
|
||||||
|
return nil, nil, errors.New("no any decoder can resolve the file")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) process(inputFile string, allDec []common.DecoderFactory) error {
|
||||||
file, err := os.Open(inputFile)
|
file, err := os.Open(inputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -278,26 +339,16 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
logger := logger.With(zap.String("source", inputFile))
|
logger := logger.With(zap.String("source", inputFile))
|
||||||
|
|
||||||
decParams := &common.DecoderParams{
|
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
|
||||||
Reader: file,
|
Reader: file,
|
||||||
Extension: filepath.Ext(inputFile),
|
Extension: filepath.Ext(inputFile),
|
||||||
FilePath: inputFile,
|
FilePath: inputFile,
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
dec := *pDec
|
||||||
var dec common.Decoder
|
|
||||||
for _, decFunc := range allDec {
|
|
||||||
dec = decFunc(decParams)
|
|
||||||
if err := dec.Validate(); err == nil {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
logger.Warn("try decode failed", zap.Error(err))
|
|
||||||
dec = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if dec == nil {
|
|
||||||
return errors.New("no any decoder can resolve the file")
|
|
||||||
}
|
|
||||||
|
|
||||||
params := &ffmpeg.UpdateMetadataParams{}
|
params := &ffmpeg.UpdateMetadataParams{}
|
||||||
|
|
||||||
@@ -354,13 +405,19 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
|
||||||
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt)
|
if err != nil {
|
||||||
|
return fmt.Errorf("get relative dir failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
|
||||||
|
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
|
||||||
|
|
||||||
if !p.overwriteOutput {
|
if !p.overwriteOutput {
|
||||||
_, err := os.Stat(outPath)
|
_, err := os.Stat(outPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return fmt.Errorf("output file %s is already exist", outPath)
|
logger.Warn("output file already exist, skip", zap.String("destination", outPath))
|
||||||
|
return nil
|
||||||
} else if !errors.Is(err, os.ErrNotExist) {
|
} else if !errors.Is(err, os.ErrNotExist) {
|
||||||
return fmt.Errorf("stat output file failed: %w", err)
|
return fmt.Errorf("stat output file failed: %w", err)
|
||||||
}
|
}
|
||||||
@@ -380,7 +437,7 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := ffmpeg.UpdateMeta(ctx, outPath, params); err != nil {
|
if err := ffmpeg.UpdateMeta(ctx, outPath, params, logger); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -43,9 +44,9 @@ type UpdateMetadataParams struct {
|
|||||||
AlbumArtExt string // required if AlbumArt is not nil
|
AlbumArtExt string // required if AlbumArt is not nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams) error {
|
func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
if params.AudioExt == ".flac" {
|
if params.AudioExt == ".flac" {
|
||||||
return updateMetaFlac(ctx, outPath, params)
|
return updateMetaFlac(ctx, outPath, params, logger.With(zap.String("module", "updateMetaFlac")))
|
||||||
} else {
|
} else {
|
||||||
return updateMetaFFmpeg(ctx, outPath, params)
|
return updateMetaFFmpeg(ctx, outPath, params)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ffmpeg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"go.uber.org/zap"
|
||||||
"mime"
|
"mime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ import (
|
|||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams) error {
|
func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
f, err := flac.ParseFile(m.Audio)
|
f, err := flac.ParseFile(m.Audio)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -62,27 +63,30 @@ func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if m.AlbumArt != nil {
|
if m.AlbumArt != nil {
|
||||||
|
coverMime := mime.TypeByExtension(m.AlbumArtExt)
|
||||||
|
logger.Debug("cover image mime detect", zap.String("mime", coverMime))
|
||||||
cover, err := flacpicture.NewFromImageData(
|
cover, err := flacpicture.NewFromImageData(
|
||||||
flacpicture.PictureTypeFrontCover,
|
flacpicture.PictureTypeFrontCover,
|
||||||
"Front cover",
|
"Front cover",
|
||||||
m.AlbumArt,
|
m.AlbumArt,
|
||||||
mime.TypeByExtension(m.AlbumArtExt),
|
coverMime,
|
||||||
)
|
)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
coverBlock := cover.Marshal()
|
|
||||||
f.Meta = append(f.Meta, &coverBlock)
|
|
||||||
|
|
||||||
// add / replace flac cover
|
if err != nil {
|
||||||
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
logger.Warn("failed to create flac cover", zap.Error(err))
|
||||||
return b.Type == flac.Picture
|
|
||||||
})
|
|
||||||
if coverIdx < 0 {
|
|
||||||
f.Meta = append(f.Meta, &coverBlock)
|
|
||||||
} else {
|
} else {
|
||||||
f.Meta[coverIdx] = &coverBlock
|
coverBlock := cover.Marshal()
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
|
||||||
|
// add / replace flac cover
|
||||||
|
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
||||||
|
return b.Type == flac.Picture
|
||||||
|
})
|
||||||
|
if coverIdx < 0 {
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
} else {
|
||||||
|
f.Meta[coverIdx] = &coverBlock
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user