Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set log #34

Merged
merged 8 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
env:
DEBUG: true
steps:
- name: Check out source code
uses: actions/checkout@v3
Expand Down Expand Up @@ -68,7 +70,7 @@ jobs:
with:
repo: k1LoW/tbls
force: true
gh-setup-version: v0.7.0
gh-setup-version: v0.7.1

- name: Run setup as a action (2/2)
run: tbls version
Expand All @@ -80,7 +82,7 @@ jobs:
github-token: ''
repo: k1LoW/colr
force: true
gh-setup-version: v0.7.0
gh-setup-version: v0.7.1

- name: Run setup as a action (no credentials) (2/2)
run: colr -v
Expand Down
13 changes: 11 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"os"

"github.com/k1LoW/gh-setup/gh"
Expand Down Expand Up @@ -88,8 +90,15 @@ var rootCmd = &cobra.Command{
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
rootCmd.SetOut(os.Stdout)
rootCmd.SetErr(os.Stderr)

log.SetOutput(io.Discard)
if env := os.Getenv("DEBUG"); env != "" {
log.SetOutput(os.Stderr)
}

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
Expand Down
22 changes: 18 additions & 4 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
"regexp"
"runtime"
Expand Down Expand Up @@ -176,10 +177,12 @@ func makeFS(owner, repo string, a *github.ReleaseAsset) (fs.FS, error) {
if err != nil {
return nil, err
}
switch a.GetContentType() {
case "application/zip", "application/x-zip-compressed":
cts := []string{a.GetContentType(), http.DetectContentType(b)}
log.Println("asset content type:", cts)
switch {
case matchContentTypes([]string{"application/zip", "application/x-zip-compressed"}, cts):
return zip.NewReader(bytes.NewReader(b), int64(len(b)))
case "application/gzip":
case matchContentTypes([]string{"application/gzip", "application/x-gzip"}, cts):
gr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
Expand All @@ -204,7 +207,7 @@ func makeFS(owner, repo string, a *github.ReleaseAsset) (fs.FS, error) {
}
return fsys, nil
}
case "application/octet-stream":
case matchContentTypes([]string{"application/octet-stream"}, cts):
fsys := fstest.MapFS{}
fsys[repo] = &fstest.MapFile{
Data: b,
Expand Down Expand Up @@ -295,3 +298,14 @@ func httpClient() (*http.Client, error) {
}
return client, nil
}

func matchContentTypes(m, ct []string) bool {
for _, v := range m {
for _, vv := range ct {
if v == vv {
return true
}
}
}
return false
}
4 changes: 4 additions & 0 deletions setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
Expand All @@ -24,6 +25,7 @@ func Bin(fsys fs.FS, bd string, force bool) (map[string]string, error) {
}
}
if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
log.Println("extract file:", path)
if err != nil {
return err
}
Expand Down Expand Up @@ -158,13 +160,15 @@ func isBinary(b []byte) bool {
// FIXME: On Windows, it can't be detected at all.
const binaryContentType = "application/octet-stream"
contentType := http.DetectContentType(b)
log.Println("content type:", contentType)
if contentType == binaryContentType {
return true
}
typ, err := filetype.Match(b)
if err != nil {
return false
}
log.Printf("file type: %v\n", typ)
if typ == filetype.Unknown {
return true
}
Expand Down