diff --git a/.github/workflows/go-build.yml b/.github/workflows/go-build.yml new file mode 100644 index 0000000..1a40603 --- /dev/null +++ b/.github/workflows/go-build.yml @@ -0,0 +1,57 @@ +name: go-build + +on: [push, pull_request] + +env: + BINARY_PREFIX: "cloudpan189-go_" + BINARY_SUFFIX: "" + PR_PROMPT: "::warning:: Build artifact will not be uploaded due to the workflow is trigged by pull request." + LD_FLAGS: "-w -s" + +jobs: + build: + name: Build binary CI + runs-on: ubuntu-latest + strategy: + matrix: + # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm, arm64] + exclude: + - goos: darwin + goarch: arm + - goos: darwin + goarch: "386" + - goos: windows + goarch: arm64 + fail-fast: true + steps: + - uses: actions/checkout@v4 + - name: Setup Go environment + uses: actions/setup-go@v5 + with: + go-version: 1.22 + - name: Cache downloaded module + uses: actions/cache@v4 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ matrix.goos }}-${{ matrix.goarch }}-${{ hashFiles('**/go.sum') }} + - name: Build binary file + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + IS_PR: ${{ !!github.head_ref }} + run: | + if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi + if $IS_PR ; then echo $PR_PROMPT; fi + export BINARY_NAME="$BINARY_PREFIX$GOOS_$GOARCH$BINARY_SUFFIX" + export CGO_ENABLED=0 + go build -o "output/$BINARY_NAME" -trimpath -ldflags "$LD_FLAGS" . + - name: Upload artifact + uses: actions/upload-artifact@v4 + if: ${{ !github.head_ref }} + with: + name: ${{ matrix.goos }}_${{ matrix.goarch }} + path: output/ diff --git a/go.mod b/go.mod index 89c0dfb..666de49 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/tickstep/cloudpan189-go -go 1.20 +go 1.22 require ( github.com/GeertJohan/go.incremental v1.0.0 diff --git a/internal/command/ls_search.go b/internal/command/ls_search.go index 7222276..44bc3bd 100644 --- a/internal/command/ls_search.go +++ b/internal/command/ls_search.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -14,7 +14,11 @@ package command import ( + "encoding/json" "fmt" + "os" + "strconv" + "github.com/olekukonko/tablewriter" "github.com/tickstep/cloudpan189-api/cloudpan" "github.com/tickstep/cloudpan189-go/cmder" @@ -23,8 +27,6 @@ import ( "github.com/tickstep/library-go/converter" "github.com/tickstep/library-go/text" "github.com/urfave/cli" - "os" - "strconv" ) type ( @@ -67,6 +69,9 @@ func CmdLs() cli.Command { 按文件大小降序排序 cloudpan189-go ls -size -desc 我的资源 + + json 格式输出 + cloudpan189-go ls -json 我的资源 `, Category: "天翼云盘", Before: cmder.ReloadConfigFunc, @@ -99,10 +104,14 @@ func CmdLs() cli.Command { default: orderBy = cloudpan.OrderByTime } + var json_output bool = false + if c.IsSet("json") { + json_output = true + } RunLs(parseFamilyId(c), c.Args().Get(0), &LsOptions{ Total: c.Bool("l") || c.Parent().Args().Get(0) == "ll", - }, orderBy, orderSort) + }, orderBy, orderSort, json_output) return nil }, @@ -136,15 +145,19 @@ func CmdLs() cli.Command { Usage: "家庭云ID", Value: "", }, + cli.BoolFlag{ + Name: "json", + Usage: "JSON 格式输出", + }, }, } } -func RunLs(familyId int64, targetPath string, lsOptions *LsOptions, orderBy cloudpan.OrderBy, orderSort cloudpan.OrderSort) { +func RunLs(familyId int64, targetPath string, lsOptions *LsOptions, orderBy cloudpan.OrderBy, orderSort cloudpan.OrderSort, isJson bool) { activeUser := config.Config.ActiveUser() targetPath = activeUser.PathJoin(familyId, targetPath) - if targetPath[len(targetPath) - 1] == '/' { - targetPath = text.Substr(targetPath, 0, len(targetPath) - 1) + if targetPath[len(targetPath)-1] == '/' { + targetPath = text.Substr(targetPath, 0, len(targetPath)-1) } targetPathInfo, err := activeUser.PanClient().AppFileInfoByPath(familyId, targetPath) @@ -183,10 +196,18 @@ func RunLs(familyId int64, targetPath string, lsOptions *LsOptions, orderBy clou } else { fileList = append(fileList, targetPathInfo) } - renderTable(opLs, lsOptions.Total, targetPath, fileList) + if isJson { + data, err := json.Marshal(fileList) + if err != nil { + fmt.Print("{\"error\":\"", err.Error(), "\"}") + } else { + fmt.Print(string(data)) + } + } else { + renderTable(opLs, lsOptions.Total, targetPath, fileList) + } } - func renderTable(op int, isTotal bool, path string, files cloudpan.AppFileList) { tb := cmdtable.NewTable(os.Stdout) var (