Skip to content

Commit

Permalink
Merge pull request #41 from k1LoW/bin-match
Browse files Browse the repository at this point in the history
Fix match logic
  • Loading branch information
k1LoW authored Feb 14, 2023
2 parents 62ceb17 + e57abc5 commit 67ad94e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 27 deletions.
18 changes: 12 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ inputs:
repo:
description: "Target repository [OWNER/REPO]"
required: true
bin-dir:
description: "Executable path"
default: ""
required: false
version:
description: "Release version"
default: ""
Expand All @@ -28,7 +24,15 @@ inputs:
default: ""
required: false
match:
description: "Match string for asset"
description: "Regexp to match asset name"
default: ""
required: false
bin-dir:
description: "Executable path"
default: ""
required: false
bin-match:
description: "Regexp to match bin path in asset"
default: ""
required: false
force:
Expand All @@ -55,9 +59,11 @@ runs:
env:
GH_SETUP_GITHUB_TOKEN: ${{ inputs.github-token }}
GH_SETUP_REPO: ${{ inputs.repo }}
GH_SETUP_BIN_DIR: ${{ inputs.bin-dir }}
GH_SETUP_VERSION: ${{ inputs.version }}
GH_SETUP_OS: ${{ inputs.os }}
GH_SETUP_ARCH: ${{ inputs.arch }}
GH_SETUP_MATCH: ${{ inputs.match }}
GH_SETUP_BIN_DIR: ${{ inputs.bin-dir }}
GH_SETUP_BIN_MATCH: ${{ inputs.bin-match }}
GH_SETUP_FORCE: ${{ inputs.force }}
GH_SETUP_BIN: ${{ steps.install-gh-setup.outputs.bin }}
18 changes: 9 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ import (
"github.com/spf13/cobra"
)

var ownerrepo string

var (
ownerrepo string
binDir string
force bool
opt = &gh.AssetOption{}
sOpt = &setup.Option{}
)

var opt = &gh.AssetOption{}

var rootCmd = &cobra.Command{
Use: "gh-setup",
Short: "Setup asset of Github Releases",
Expand Down Expand Up @@ -74,7 +73,7 @@ var rootCmd = &cobra.Command{
return err
}
cmd.Printf("Use %s\n", a.GetName())
m, err := setup.Bin(fsys, binDir, force)
m, err := setup.Bin(fsys, sOpt)
if err != nil {
return err
}
Expand Down Expand Up @@ -105,10 +104,11 @@ func Execute() {

func init() {
rootCmd.Flags().StringVarP(&ownerrepo, "repo", "R", "", "repository using the [HOST/]OWNER/REPO format")
rootCmd.Flags().StringVarP(&binDir, "bin-dir", "", "", "bin directory for setup")
rootCmd.Flags().BoolVarP(&force, "force", "f", false, "enable force setup")
rootCmd.Flags().StringVarP(&opt.Version, "version", "v", "", "release version")
rootCmd.Flags().StringVarP(&opt.OS, "os", "O", "", "specify OS of asset")
rootCmd.Flags().StringVarP(&opt.Arch, "arch", "A", "", "specify arch of asset")
rootCmd.Flags().StringVarP(&opt.Match, "match", "", "", "match string for asset")
rootCmd.Flags().StringVarP(&opt.Match, "match", "", "", "regexp to match asset name")
rootCmd.Flags().StringVarP(&sOpt.BinDir, "bin-dir", "", "", "bin directory for setup")
rootCmd.Flags().StringVarP(&sOpt.BinMatch, "bin-match", "", "", "regexp to match bin path in asset")
rootCmd.Flags().BoolVarP(&sOpt.Force, "force", "f", false, "enable force setup")
}
6 changes: 5 additions & 1 deletion gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ func DetectHostOwnerRepo(ownerrepo string) (string, string, string, error) {
func detectAsset(assets []*github.ReleaseAsset, opt *AssetOption) (*github.ReleaseAsset, error) {
var (
od, ad, om *regexp.Regexp
err error
)
if opt != nil && opt.Match != "" {
om = regexp.MustCompile(opt.Match)
om, err = regexp.Compile(opt.Match)
if err != nil {
return nil, err
}
}
if opt != nil && opt.OS != "" {
od = getDictRegexp(opt.OS, osDict)
Expand Down
10 changes: 7 additions & 3 deletions scripts/run-gh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ fi
export GITHUB_TOKEN=${token}

repo=${GH_SETUP_REPO}
bindir=${GH_SETUP_BIN_DIR}
version=${GH_SETUP_VERSION}
os=${GH_SETUP_OS}
arch=${GH_SETUP_ARCH}
match=${GH_SETUP_MATCH}
bin_dir=${GH_SETUP_BIN_DIR}
bin_match=${GH_SETUP_BIN_MATCH}
force=${GH_SETUP_FORCE}

if [ -z "${force}" ]; then
${bin} --repo ${repo} --bin-dir=${bindir} --version=${version} --os=${os} --arch=${arch}
# ${bin} --repo ${repo} --version=${version} --os=${os} --arch=${arch} --bin-dir=${bin_dir} --bin-match=${bin_match}
${bin} --repo ${repo} --version=${version} --os=${os} --arch=${arch} --bin-dir=${bin_dir}
else
${bin} --repo ${repo} --bin-dir=${bindir} --version=${version} --os=${os} --arch=${arch} --force
# ${bin} --repo ${repo} --version=${version} --os=${os} --arch=${arch} --bin-dir=${bin_dir} --bin-match=${bin_match} --force
${bin} --repo ${repo} --version=${version} --os=${os} --arch=${arch} --bin-dir=${bin_dir} --force
fi
41 changes: 33 additions & 8 deletions setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,38 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"

"github.com/h2non/filetype"
)

func Bin(fsys fs.FS, bd string, force bool) (map[string]string, error) {
var err error
type Option struct {
BinDir string
BinMatch string
Force bool
}

func Bin(fsys fs.FS, opt *Option) (map[string]string, error) {
var (
bd string
bm *regexp.Regexp
force bool
err error
)
m := map[string]string{}
if opt != nil {
force = opt.Force
bd = opt.BinDir
if opt.BinMatch != "" {
bm, err = regexp.Compile(opt.BinMatch)
if err != nil {
return nil, err
}
}
}
if bd == "" {
bd, err = binDir()
if err != nil {
Expand All @@ -32,10 +54,16 @@ func Bin(fsys fs.FS, bd string, force bool) (map[string]string, error) {
if d.IsDir() {
return nil
}
for _, i := range ignoreBinnameKeywords {
if strings.Contains(filepath.ToSlash(strings.ToLower(path)), filepath.ToSlash(strings.ToLower(i))) {
if bm != nil {
if !bm.MatchString(path) {
return nil
}
} else {
for _, i := range ignoreBinnameKeywords {
if strings.Contains(filepath.ToSlash(strings.ToLower(path)), filepath.ToSlash(strings.ToLower(i))) {
return nil
}
}
}

b, err := fs.ReadFile(fsys, path)
Expand Down Expand Up @@ -169,8 +197,5 @@ func isBinary(b []byte) bool {
return false
}
log.Printf("file type: %v\n", typ)
if typ == filetype.Unknown {
return true
}
return false
return typ == filetype.Unknown
}

0 comments on commit 67ad94e

Please sign in to comment.