Skip to content

Commit

Permalink
use the same old janky shell script logic that once worked
Browse files Browse the repository at this point in the history
  • Loading branch information
tstromberg committed Oct 8, 2024
1 parent 6f122e3 commit 3eba90e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pkg/programkind/programkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var supportedKind = map[string]string{
"pyc": "application/x-python-code",
"rb": "text/x-ruby",
"rs": "text/x-rust",
"script": "text/x-script",
"script": "text/x-generic-script",
"scpt": "application/x-applescript",
"scptd": "application/x-applescript",
"service": "text/x-systemd",
Expand Down Expand Up @@ -106,7 +106,7 @@ func File(path string) (*FileType, error) {
return mtype, nil
}

// read hdr content for future strategies
// read hdr s for future strategies
var hdr [32]byte
f, err := os.Open(path)
if err != nil {
Expand All @@ -120,24 +120,27 @@ func File(path string) (*FileType, error) {
}

// final strategy: DIY matching where mimetype is too strict.
content := string(hdr[:])
s := string(hdr[:])
switch {
case hdr[0] == '\x7f' && hdr[1] == 'E' || hdr[2] == 'L' || hdr[3] == 'F':
return Path(".elf"), nil
case strings.HasPrefix(content, "#!/bin/sh"):
return Path(".sh"), nil
case strings.HasPrefix(content, "#!/bin/bash"):
return Path(".bash"), nil
case strings.Contains(content, "<?php"):
case strings.Contains(s, "<?php"):
return Path(".php"), nil
case strings.HasPrefix(content, "import "):
case strings.HasPrefix(s, "import "):
return Path(".py"), nil
case strings.HasPrefix(content, "#!/"):
return Path(".script"), nil
case strings.Contains(content, "if ! /"):
return Path(".sh"), nil
case strings.Contains(content, "; then"):
case strings.HasPrefix(s, "#!/bin/ash") ||
strings.HasPrefix(s, "#!/bin/bash") ||
strings.HasPrefix(s, "#!/bin/fish") ||
strings.HasPrefix(s, "#!/bin/sh") ||
strings.HasPrefix(s, "#!/bin/zsh") ||
strings.Contains(s, `if [`) ||
strings.Contains(s, "if !") ||
strings.Contains(s, `echo "`) ||
strings.Contains(s, `grep `) ||
strings.Contains(s, "; then"):
return Path(".sh"), nil
case strings.HasPrefix(s, "#!/"):
return Path(".script"), nil
}
return nil, nil
}
Expand Down

0 comments on commit 3eba90e

Please sign in to comment.